思路:

先用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的更多相关文章

  1. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 【最短路+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 ...

  7. 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构成的格子,对 ...

  8. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  9. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

随机推荐

  1. JAVA-Servlet高级应用

    会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...

  2. jeesite快速开发平台---数据库各表一览

    jeesite中一共有55张表,如下 其中以act_*开头的是Activity工作流的表,cms_*开头的是内容管理系统的表,oa_*开头的是办公自动化,sys_*开头的是系统表,test_*开头的是 ...

  3. bzoj千题计划294:bzoj3139: [Hnoi2013]比赛

    http://www.lydsy.com/JudgeOnline/problem.php?id=3139 队伍的顺序不会影响结果 将队伍的得分情况作为状态,记忆化搜索 就是先搜索第一只队伍的得分情况, ...

  4. Linux命令(六)Linux超级用户和管理组

    修改文件目录的所属组

  5. [转载]NodeJS优缺点及适用场景讨论

    http://www.xprogrammer.com/159.html 概述:NodeJS宣称其目标是“旨在提供一种简单的构建可伸缩网络程序的方法”,那么它的出现是为了解决什么问题呢,它有什么优缺点以 ...

  6. java学习第04天(语句、函数、数组)

    (3)循环结构 格式: for(初始化表达式,循环条件表达式,循环后的操作变大时){ 执行语句,循环体: } 注: a. for循环里面的连个表达式运行的顺序,初始化表达式只读一次,判断循环条件,为真 ...

  7. SDWebImage源码阅读-第二篇

    一  SDWebImageManager的downloadImageWithURL的方法 上一篇,我们刚开了个头,分析了一下开始加载图片之前如何取消其他正在下载的任务,接着,我们回到 - (void) ...

  8. [译]How To Use the Linux Auditing System on CentOS 7

    本文是How To Use the Linux Auditing System on CentOS 7的中文版,翻译不到之处,还请指出和多多包涵.本文并不会完全遵从原文的一些格式,而是加入自己学习的理 ...

  9. Java初转型-SSM配置文件

    文章来源:http://www.cnblogs.com/wxisme/p/4924561.html web.xml的配置                                        ...

  10. WPF使用DataGridComboBoxColumn完成绑定

    在使用DataGrid的时候,有时候需要使某些列为ComboBox,这时自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource数据绑定后台的对象,就会发现,这 ...