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 ...
随机推荐
- Android屏幕适配-安卓切图
一.Android中的单位 1.dp(dip):density-independent pixels,这并不是一个绝对的单位,而只是一个相对的概念,代表的是屏幕写对角线上每inch上像素点的个数. 2 ...
- 为RecyclerView打造通用Adapter 让RecyclerView更加好用
原文出处: 张鸿洋 (Granker,@鸿洋_ ) 一.概述 记得好久以前针对ListView类控件写过一篇打造万能的ListView GridView 适配器,如今RecyclerView异军突起, ...
- poj3204Ikki's Story I - Road Reconstruction(最大流求割边)
链接 最大流=最小割 这题是求割边集 dinic求出残余网络 两边dfs分别以源点d找到可达点 再以汇点进行d找到可达汇点的点 如果u,v为割边 那么s->u可达 v->t可达 并且为饱 ...
- vue在使用ajax获取数据时,两种方法(jquery和vue_resource)
@{ Layout = null;} <!DOCTYPE html> <html><head> <meta name="viewport ...
- PHP Deprecated: Function split() is deprecated in /var/www/html/cacti/cmd.php on line 61
[root@localhost cacti]# php cmd.php PHP Deprecated: Function split() is deprecated in /var/www/html/ ...
- array_keys
<?php$array = array(0 => 100, "color" => "red");print_r(array_keys($arr ...
- java实现排序的几种方法
package com.ywx.count; import java.util.Scanner; /** * 题目:排序的几种方式(汇总及重构) * @author Vashon(yangwenxue ...
- CentOS 7下安装配置proftpd搭建ftp服务器
proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone.xinetd模式运 ...
- Architecture:架构 元素与关系
http://www.iso-architecture.org/42010/cm/ Systems have architectures. In the Standard, the architect ...
- CAD交互绘制矩形框(网页版)
主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...