题目大意:

  给一个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 题解的更多相关文章

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

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

  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. ZOJ 3781 Paint the Grid Reloaded 连通块

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

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

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

  6. ZOJ 3781 Paint the Grid Reloaded

    枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...

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

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

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

  9. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

随机推荐

  1. Suricata的性能

    不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/performance/index.html Docs » 7. Performanc ...

  2. 微信环境支付宝服务窗环境app手机浏览器pc端混合判断

    //微信环境 if(userAgent.match(/micromessenger/) == 'micromessenger'){ }//支付宝服务窗环境else if(userAgent.match ...

  3. ios学习笔记 UITableView(纯代码) (二)

    头文件 --------------------------------------------- #import <UIKit/UIKit.h> /** UITableViewDataS ...

  4. [转]IntelliJ IDEA 自定义方法注解模板

    IntelliJ IDEA 自定义方法注解模板 置顶2017年08月02日 18:04:36 阅读数:32592 最近没啥事开始正式用Eclipse 转入 idea工具阵营,毕竟有70%的开发者在使用 ...

  5. SQL异常为"当IDENTITY_INSERT设置为OFF时" 的解决

    误删数据库时,可以利用insert插入删除的数据,但是有时表可能有自增字段如id.这是插入数据如果包含自增字段就会出现错误,提示"IDENTITY_INSERT设置为OFF,插入失败&quo ...

  6. sprintf使用时需要注意的问题

  7. codevs 5438 zbd之难题(水题日常)

     时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...

  8. 写的一个HttpClient类

    package com.ca.test.cainterface.common.util.http; import com.ca.test.cainterface.common.util.data.Da ...

  9. swift学习——枚举

    swift枚举 1. 枚举基本语法 enum Method { case Add case Sub case Mul case Div } 也可以使用一种更简单的写法 enum Method1{ ca ...

  10. Node + Express + MySQL 接口开发完整案例

    https://blog.csdn.net/u013216976/article/details/85273770 https://github.com/Apache-Ra/node-express- ...