ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意:
给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色。
思路:
1.每次操作都将本身所在的连通块与和自己相邻的不同颜色的连通块变成同一种颜色,也就是变成一个连通块了,那么要使n次操作后全部变成一样的颜色,也就是从某点出发到达其余所有点。
2.因此dfs把连通块缩成点,然后相邻的连通块之间建边,枚举以每个点为根的情况,bfs求出每种情况的深度,取最小的即为答案。
反思:
1.hea数组初始赋-1会死循,要赋0。
2.M要开60,50的话要WA。
代码:
#include<cstdio>
const int dx[]={-,,,},dy[]={,-,,},M=,N=;
int cnt,num,b[M][M],d[N],q[N],v[N<<],hea[N],nex[N<<];
bool f,a[M][M],c[N][N],vis[N];
char s[M]; void sd(int x,int y)
{
b[x][y]=cnt;
for (int i=,u,v;i<;++i)
if (b[u=x+dx[i]][v=y+dy[i]]== && a[u][v]==a[x][y]) sd(u,v);
} void add(int x,int y) { v[++num]=y,nex[num]=hea[x],hea[x]=num; } int dep(int x)
{
int l=,r=,y,i;
for (vis[q[]=x]=f,d[x]=;l^r;)
for (i=hea[x=q[++l]];i;i=nex[i])
if (vis[y=v[i]]^f) vis[q[++r]=y]=f,d[y]=d[x]+;
return d[q[r]];
} int main()
{
int T,n,m,i,j,k,x,y,u,v;
for (scanf("%d",&T);T;--T)
{
scanf("%d%d",&n,&m);
for (i=;i<=n;++i)
for (scanf("%s",s),j=;j<=m;++j)
if (s[j-]=='O') a[i][j]=; else a[i][j]=;
for (i=cnt=num=;i<n+;++i) b[i][]=b[i][m+]=-;
for (i=;i<m+;++i) b[][i]=b[n+][i]=-;
for (i=;i<=n;++i)
for (j=;j<=m;++j) b[i][j]=;
for (i=;i<=n;++i)
for (j=;j<=m;++j)
if (!b[i][j]) ++cnt,sd(i,j);
for (i=;i<=cnt;++i)
for (j=;j<=cnt;++j) c[i][j]=;
for (i=;i<=cnt;++i) hea[i]=;
for (i=;i<=n;++i)
for (j=;j<=m;++j)
for (u=b[i][j],k=;k<;++k)
if (~(v=b[x=i+dx[k]][y=j+dy[k]]) && c[u][v] && u^v)
add(u,v),add(v,u),c[u][v]=c[v][u]=;
for (i=;i<=cnt;++i) vis[i]=f;
for (y=cnt,i=;i<=cnt;++i)
if (f=!f,(x=dep(i))<y) y=x;
printf("%d\n",y);
}
return ;
}
ZOJ - 3781 Paint the Grid Reloaded 题解的更多相关文章
- 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 - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- 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 ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- 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
枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...
- Paint the Grid Reloaded ZOJ - 3781 图论变形
Paint the Grid Reloaded Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %ll ...
- 【最短路+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 ...
- ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds Me ...
随机推荐
- C#中Json的简单处理
命名空间:Windows.Data.Json在Windows Runtime中,可以使用Json类对获取的Json字符串进行操作,相比DataContractJsonSerializer类操作更加直观 ...
- re正则表达式公式讲解1
常用的表达式一些规则 1.“.” 匹配出了\n之外的任意一个字符,包括特殊字符 有几个·就匹配几个字符. import re print(re.search("."," ...
- CF758C Unfair Poll
题意: On the Literature lesson Sergei noticed an awful injustice, it seems that some students are aske ...
- iOS infoq资料架构设计漫谈
http://www.infoq.com/cn/ios/?utm_source=infoq&utm_medium=header_graybar&utm_campaign=topic_c ...
- 031_spark架构原理
spark核心组件 driver master worker executor task(只有task是线程) 核心组件的原理图解
- git push时报错filename too long的解决
命令行输入:git config core.longpaths true 之后再进行 git 的push命令
- VCS filelist 文件格式
VCS在运行仿真一般都会加仿真参数 –f filelist,filelist 是包含其他的仿真参数和整个工程的文件列表.具体格式如下: //file list format, just for exa ...
- Java Script 学习笔记(一)
示例如下: JavaScript-警告(alert 消息对话框) 我们在访问网站的时候,有时会突然弹出一个小窗口,上面写着一段提示信息文字.如果你不点击“确定”,就不能对网页做任何操作,这个小窗口就是 ...
- 两个已排序数组的合并-C语言
最近在纸上写一个已排序数组的合并时,花了超过预期的时间.仔细想想,这种要放到毕业找工作那会两下就出来了,原因还在于工作后对基础没有重视,疏于练习. 说开一点,现在搜索引擎的发达确实给问题的解决带来了便 ...
- win7创建无线(WIFI)cmd命令
1.创建无限热点:netsh wlan set hostednetwork mode=allow ssid=name key=12345678. 2.承载网络:netsh wlan start(关闭s ...