[ZOJ3781]Paint the Grid Reloaded
思路:
先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$。
注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上。
一开始和网上的题解对拍$T=1$一直对不出错,后来测了大数据才发现是初始化的问题。
#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
const int inf=0x7fffffff;
const int dx[]={-,,,},dy[]={,,-,};
int n,m;
const int N=,V=;
bool a[N][N],b[N][N];
int g[N][N];
bool map[V][V];
int cnt,ans;
std::vector<int> e[V];
inline void init() {
cnt=,ans=inf;
memset(a,,sizeof a);
memset(g,,sizeof g);
memset(map,,sizeof map);
memset(b,,sizeof b);
for(int i=;i<=n;i++) {
b[i][]=b[i][m+]=true;
}
for(int j=;j<=m;j++) {
b[][j]=b[n+][j]=true;
}
for(int i=;i<V;i++) e[i].clear();
}
inline void add_edge(const int u,const int v) {
if(!map[u][v]) {
e[u].push_back(v);
map[u][v]=true;
}
}
void dfs(const int x,const int y) {
b[x][y]=true;
for(int i=;i<;i++) {
int xx=x+dx[i],yy=y+dy[i];
if(g[xx][yy]&&g[xx][yy]!=g[x][y]) {
add_edge(g[x][y],g[xx][yy]);
add_edge(g[xx][yy],g[x][y]);
}
if(b[xx][yy]) continue;
if(a[x][y]==a[xx][yy]) {
g[xx][yy]=g[x][y];
dfs(xx,yy);
}
}
}
bool v[V];
void bfs(const int s) {
memset(v,,sizeof v);
std::queue<std::pair<int,int> > q;
q.push(std::make_pair(s,));
v[s]=true;
int max=;
while(!q.empty()) {
int x=q.front().first,d=q.front().second;
q.pop();
for(unsigned i=;i<e[x].size();i++) {
int &y=e[x][i];
if(v[y]) continue;
q.push(std::make_pair(y,d+));
v[y]=true;
}
max=std::max(max,d);
}
ans=std::min(ans,max);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int T;
std::cin>>T;
while(T--) {
std::cin>>n>>m;
init();
for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
char ch;
std::cin>>ch;
a[i][j]=ch=='O';
}
}
for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
if(b[i][j]) continue;
g[i][j]=++cnt;
dfs(i,j);
}
}
for(int i=;i<=cnt;i++) bfs(i);
std::cout<<ans<<std::endl;
}
return ;
}
[ZOJ3781]Paint the Grid Reloaded的更多相关文章
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)
Paint the Grid Reloaded Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N rows ...
- ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781
题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...
- 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS
原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
随机推荐
- 在Ubuntu14.04 64bit上搭建单机Spark环境,IDE为Intelli IDEA
在Ubuntu14.04 64bit上搭建单机Spark环境,IDE为Intelli IDEA 一. 环境 Ubuntu14.04 64位 JDK 1.8.0_73 scala-2.10. ...
- [转载]Frontend Knowledge Structure
https://github.com/JacksonTian/fks http://code.csdn.net/news/2819224 本文为大家整理了一系列关于JavaScript的常用工具,包括 ...
- HTML的文档类型:<!DOCTYPE >
<!DOCTYPE> 声明:它不是 HTML 标签而且对大小写不敏感,而是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令.而且 声明必须是 HTML 文档的第一行,位于 ...
- mysql先排序在分组
– 表的结构 `test`– CREATE TABLE IF NOT EXISTS `test` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varcha ...
- vsftp服务器部署
环境:CentOS 6.6 目标:个人虚机部署vsftp服务器,供测试使用. 说明:步骤已改写为脚本,直接添加用户与对应的密码列表,调用函数名即可 ########################## ...
- 洛谷 P3916 【图的遍历】反向加边+dfs
前言: 对于这类带环的图,一般记忆化搜索不能很好的对所有遍历的边进行更新取值.因为环上的点可以相互到达,所以他们的答案因当是同步更新的,而dfs一旦你回溯完环上某个点就不会在更新这个点的答案了,做不到 ...
- Hive笔记之Fetch Task
在使用Hive的时候,有时候只是想取表中某个分区的前几条的记录看下数据格式,比如一个很常用的查询: select * from foo where partition_column=bar limit ...
- 对package.json的理解和学习
一.初步理解 1. npm安装package.json时 直接转到当前项目目录下用命令npm install 或npm install --save-dev安装即可,自动将package.json中 ...
- 【干货】Windows内存获取和分析---查找恶意进程,端口
来源:Unit 5: Windows Acquisition 5.1 Windows Acquisition Windows Memory Acquisition and Analysis 调查人员检 ...
- Linux使用一个定时器实现设置任意数量定时器功能【转】
转自:https://www.jb51.net/article/120748.htm 为什么需要这个功能,因为大多数计算机软件时钟系统通常只能有一个时钟触发一次中断.当运行多个任务时,我们会想要多个定 ...