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 ...
随机推荐
- 学好Mac常用命令,助力iOS开发
原文出处: Jack_lin(@Jack_Lin_IOS ) 厚重·技术 序言 在iOS开发的过程中,更多地注重iOS开发的效率,熟练使用Mac终端操作的常用命令,可以让你更好的游刃于iOS繁重的开发 ...
- logging模块基础
很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...
- PKU_campus_2017_K Lying Island
思路: 题目链接http://poj.openjudge.cn/practice/C17K/ 状压dp.dp[i][j]表示第i - k人到第i人的状态为j的情况下前i人中最多有多少好人. 实现: # ...
- EventBus 报“Subscriber class already registered to event class”错误
这句子的话意思也很容易理解,“接收者类已经被注册为事件类了”. 之前我是这么写: 事件注册是写在onStart()里面的 @Override protected void onStart() { su ...
- 磁盘格式化mke2fs
-b 设置每个块的大小,当前支持1024,2048,40963种字节 -i 给一个inode多少容量 -c 检查磁盘错误,仅执行一次-c时候,会进行快速读取测试:-c -c会测试读写,会很慢 -L 后 ...
- Recyclerview设置间距
首先自定义一个RecyclerViewDivider 继承 RecyclerView.ItemDecoration,实现自定义. public class RecyclerViewDivider ex ...
- ajax请求的时候get 和post方式的区别
ajax请求的时候get 和post方式的区别?一个在url后面一个放在虚拟载体里面有大小限制安全问题应用不同一个是论坛等只需要请求的,一个是类似修改密码的
- 制作JPEGImages出现的bug
我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...
- Elasticsearch 索引管理和内核探秘
1. 创建索引,修改索引,删除索引 //创建索引 PUT /my_index { "settings": { , }, "mappings": { " ...
- luogu 2709小b的询问--莫队
https://www.luogu.org/problemnew/show/P2709 无修改的莫队几乎没有什么太高深的套路,比较模板吧,大多都是在那两个函数上动手脚. 这题询问每一种数字数量的平方和 ...