思路:

先用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. JavaScript实现ajax发送表单数据

    知识点: 1.重置表单数据 2.获取表单数据(纯JavaScript) 3.设置表单数据(纯JavaScript) 4.ajax发送数据到客户端 (1)设置请求头,自己组合数据 (2)实例化表单对象, ...

  2. RESTful记录-RESTful服务

    按照REST架构,一个RESTful Web服务不应该继续服务器的客户端的状态.这种限制被称为无状态.它负责客户以它的上下文传递给服务器,然后服务器可以存储这样的上下文,以处理客户端的进一步请求.例如 ...

  3. Spark记录-spark编程介绍

    Spark核心编程 Spark 核心是整个项目的基础.它提供了分布式任务调度,调度和基本的 I/O 功能.Spark 使用一种称为RDD(弹性分布式数据集)一个专门的基础数据结构,是整个机器分区数据的 ...

  4. js 正则学习小记之匹配字符串字面量

    今天看了第5章几个例子,有点收获,记录下来当作回顾也当作分享. 关于匹配字符串问题,有很多种类型,今天讨论 js 代码里的字符串匹配.(因为我想学完之后写个语法高亮练手,所以用js代码当作例子) va ...

  5. [整理]Win下好用的Markdown工具

    用过haroopad,MarkPad,Sublime + markdown插件,前2款勉强能用,都处于继续开发中,haroopad支持的语法相对较少,提示也不明显,MarkPad还是有不少bug. 后 ...

  6. int,char指针探究

    #include<iostream> using namespace std; int main() { /* 思路: 1.关于int指针,不可以直接往指针里传值 例:int *a = 4 ...

  7. Dream_Spark定制第二课

    Spark版本定制第2天:通过案例对SparkStreaming透彻理解之二 本期内容: 1 解密Spark Streaming运行机制 2 解密Spark Streaming架构 一切不能进行实时流 ...

  8. 【转载】maven pom详解(2)

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  9. camera驱动框架分析(上)

    前言 camera驱动框架涉及到的知识点比较多,特别是camera本身的接口就有很多,有些是直接连接到soc的camif口上的,有些是通过usb接口导出的,如usb camera.我这里主要讨论前者, ...

  10. CefSharp.OffScreen.Example

    namespace CefSharp.OffScreen.Example { public class Program { private static ChromiumWebBrowser brow ...