思路:

先用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. python---django中STATIC_ROOT和STATIC_URL以及STATICFILES_DIRS

    先引入两篇相关文章,从中了解更为详细 django 静态资源配置详解 django静态文件配置 Django的STATIC_ROOT和STATIC_URL以及STATICFILES_DIRS(先看) ...

  2. Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收

    前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...

  3. js拾遗: replace 替换参数

    先来看一个简单的例子吧. var str = "123X321".replace("X", "$'"); 替换后的 str 是什么呢?是 & ...

  4. BAT及各大互联网公司2014前端笔试面试题--Html,Css篇(昨天有个群友表示写的简单了点,然后我无情的把他的抄了一遍)

    某个群友 http://www.cnblogs.com/coco1s/   很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习前端基础 ...

  5. [整理]html5 WebApp 01

    在正式进入WebApp开发之前,有几个问题要解决: 1.我是产品策划:UI风格,功能设计,产品预期效果(如访问量等各类指标) 2.我是UI设计:图片图标制作,我该按怎样的大小来设计? 3.我是前端开发 ...

  6. AngularJs入门篇-控制器的加深理解基础篇

    下面做的是一个更新时间的效果,每一秒钟就会更新一下,视图中会显示出当前的时间   下面的这个例子中,SceondController函数将接受两个参数,既该DOM元素的$scope和$timeout. ...

  7. Python 装饰器入门(下)

    继续上次的进度:https://www.cnblogs.com/flashBoxer/p/9847521.html 正文: 装饰类 在类中有两种不通的方式使用装饰器,第一个和我们之前做过的函数非常相似 ...

  8. json 删除、添加对象

    1. 定义json对象    var entryJson = []; 2. 删除.添加对象 entryJson.pop();     //删除最后一个对象   entryJson.push({ //往 ...

  9. 爬虫笔记之刷小怪练级:yymp3爬虫(音乐类爬虫)

    一.目标 爬取http://www.yymp3.com网站歌曲相关信息,包括歌曲名字.作者相关信息.歌曲的音频数据.歌曲的歌词数据. 二.分析 2.1 歌曲信息.歌曲音频数据下载地址的获取 随便打开一 ...

  10. blockchain 名词解释

    1.UTXO UTXO是比特币交易的基本单位UTXO(Unspent Transaction Outputs)是未花费的交易输出,它是比特币交易生成及验证的一个核心概念.交易构成了一组链式结构,所有合 ...