题目大意:

  给一个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. Linux下用matplotlib画决策树

    1.trees = {'no surfacing': { 0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}} 2.从我的文件trees.txt里读的决策树, ...

  2. H5图片预览功能

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. python工具之exccel模板生成报表

    from Db import Db from log import log import xlwt import xlrd from xlutils.copy import copy import s ...

  4. 1. UI Tests简介

    (1) User Interface Testing UI Testing库主要提供了与App中的UI元素进行查找和交互的能力,这使得我们可以通过验证UI元素的状态来测试App是否正常运行.     ...

  5. poj1862 Stripies

    思路: 简单贪心. 实现: #include <iostream> #include <cstdio> #include <algorithm> #include ...

  6. match,location,history

    哇,平常写路由时基本就是简单的按照组件给的示例写,从来没有考虑为什么,又遇见了路由相关的问题,先记录一下问题,好好捋一下,哎,好香要个大佬来带带我呀,每次遇到问题要解决好久 问题: 判断是否登录之后跳 ...

  7. Kotlin学习的一些笔记

    Introduction 写在前面 关于本书 这本书适合你吗? 关于作者 介绍 什么是Kotlin? 我们通过Kotlin得到什么 准备工作 Android Studio 安装Kotlin插件 创建一 ...

  8. [Python筆記] 將 Pandas 的 Dataframe 寫入 Sqlite3

    使用 pandas.io 寫入 Sqlite import sqlite3 as lite from pandas.io import sql import pandas as pd 依照 if_ex ...

  9. (转)SpringMVC学习(九)——SpringMVC中实现文件上传

    http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...

  10. java 随机数 <%=System.currentTimeMillis() %>

    java 随机数<c:set var="version" value="<%=System.currentTimeMillis() %>"/& ...