思路:

先用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. SQL记录-PLSQL数组

    PL/SQL数组 PL/SQL程序设计语言提供叫一种叫做VARRAY的数据结构,其可存储相同类型元素的一个固定大小的连续集合.VARRAY用于存储数据的有序集合,但它往往是更加有用认为数组作为相同类型 ...

  2. 数据库类型与JDBC TYPE 和Java类型对应关系

    https://blog.csdn.net/seelye/article/details/40105969

  3. Linux命令(二)关机重启

  4. H5 Day1 练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 20155203 2016-2017-4 《Java程序设计》第6周学习总结

    20155203 2016-2017-4 <Java程序设计>第6周学习总结 教材学习内容总结 重载和覆盖:重载是将函数的名称和方法不变(名称可以有一些变动),只改变参数列表,相当于和原来 ...

  6. iOS 处理缓存的三种方法

    缓存处理是个相当头疼的事情,要根据需要综合应用不同的策略.总的来说有以下几种情况: 1.URL缓存,例如社交应用的帖子浏览,要在viewDidAppear:里面进行URL缓存.简单来说就是用NSURL ...

  7. Python人工智能之路 - 第一篇 : 你得会点儿Python基础

    Python 号称是最接近人工智能的语言,因为它的动态便捷性和灵活的三方扩展,成就了它在人工智能领域的丰碑 走进Python,靠近人工智能 一.编程语言Python的基础 之 "浅入浅出&q ...

  8. Aho-Corasick 多模式匹配算法、AC自动机详解

    Aho-Corasick算法是多模式匹配中的经典算法,目前在实际应用中较多. Aho-Corasick算法对应的数据结构是Aho-Corasick自动机,简称AC自动机. 搞编程的一般都应该知道自动机 ...

  9. 常用 Git 命令清单【转】

    转自:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 作者: 阮一峰 日期: 2015年12月 9日 我每天使用 Git ,但是 ...

  10. tomcat启动报错:Injection of autowired dependencies failed

    tomcat启动报错:Injectjion of autowired dependencies failed 环境: 操作系统:centos6.5 tomcat: 7.0.52 jdk:openjdk ...