LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781

题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一个字符,问整个矩阵变成相同的最小次数

思路:对所有连通块编号,根据是否与其他联通块相连建边,最后得到图后枚举以某起点(代表某连通块)所能到的最大距离求它们的最小值即可。

关键还是在于模型的转换,这方面我好像有点欠缺orz

/** @Date    : 2017-03-30-15.09
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int dir[4][2] = {0,1,0,-1,1,0,-1,0};
char mp[50][50];
bool vis[50][50];
int vt[50][50];//num
bool mr[2000][2000];
int dic[2000]; vectoredg[2000]; int cnt;
int n, m;
void dfs(int x, int y, char k)
{
if(vis[x][y])
{
if(mp[x][y] != k && vt[x][y] != -1 && !mr[vt[x][y]][cnt])
{
mr[vt[x][y]][cnt] = 1;
mr[cnt][vt[x][y]] = 1; edg[cnt].PB(vt[x][y]);
edg[vt[x][y]].PB(cnt);
}
return ;
}
if(mp[x][y] == k)
{
vis[x][y] = 1;
vt[x][y] = cnt;
for(int i = 0; i < 4; i++)
{
int a = x + dir[i][0];
int b = y + dir[i][1];
if(a > 0 && b > 0 && a <= n && b <= m)
dfs(a, b, k);
}
}
} void spfa(int x)
{
bool e[2000] = {0};
MMI(dic);
queue q;
e[x] = 1;
dic[x] = 0;
q.push(x);
while(!q.empty())
{
int nw = q.front();
q.pop();
e[nw] = 0;
for(int i = 0; i < edg[nw].size(); i++)
{
int np = edg[nw][i];
int ds = 1 + dic[nw];
if(dic[np] > ds)
{
dic[np] = ds;
if(!e[np])
e[np] = 1, q.push(np);
}
}
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
MMF(vis);
MMF(vt); scanf("%d%d", &n, &m);
for(int i = n*m + 1; i >= 0; i--)
edg[i].clear(), MMF(mr[i]);
getchar();
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
scanf("%c", &mp[i][j]);
}
getchar();
} /*for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
printf("%c", mp[i][j]);
printf("\n");
}*/
cnt = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(!vis[i][j])
{
dfs(i, j, mp[i][j]);
cnt++;
}
}
}
int ans = INF;
for(int i = 0; i < cnt; i++)
{
spfa(i);
int ma = 0;
for(int j = 0; j < cnt; j++)
ma = max(ma, dic[j]);
ans = min(ans, ma);
}
printf("%d\n", ans);
}
return 0;
}
/*
1
4 4
OXOX
XOXO
OXOX
XOXO
*/

ZOJ 3781 Paint the Grid Reloaded 连通块的更多相关文章

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

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

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

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

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

  5. ZOJ 3781 Paint the Grid Reloaded

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

  6. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

  7. 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS

    原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...

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

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

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

随机推荐

  1. 【每日scrum】NO.9

    (1)这是我们冲刺的最后一天,晚上我们的团队进行了收尾工作:第一阶段的任务基本完成,软件主要实现了校园景点照片以及对应的介绍,查询最短路径,查询涉及相关景点的查询,查询全部路径,基本界面的设计,导航功 ...

  2. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  3. C语言的调查

    1.你对自己的未来有什么规划?做了哪些准备?从事跟本专业相关的工作.认真学习好书本的知识,并能很好的运用它. 2.你认为什么是学习?学习有什么用?现在学习动力如何?为什么?学习可以让自己懂得更多,完善 ...

  4. 在Asp.Net中使用Redis【本文摘自智车芯官网】

    Redis安装 在安装之前需要获取Redis安装包.在这里我们就不详细介绍安装包的获取了.这里Redis-x64-3.2.100.zip安装包为例通过dos命令取安装.通过dos命令找到安装目录. 在 ...

  5. lintcode-223-回文链表

    223-回文链表 设计一种方式检查一个链表是否为回文链表. 样例 1->2->1 就是一个回文链表. 挑战 O(n)的时间和O(1)的额外空间. 标签 链表 思路 找到链表中点后,翻转链表 ...

  6. div跟随鼠标移动

    1.目标是实现div跟随鼠标而移动,分三种情况进行实现 a)首先获取div,进行绑定鼠标移动事件,给div开启定位功能 第一种实现方式,假如body的大小跟页面大小一样,则可以用这个方法. 1)获取鼠 ...

  7. python实现进制之间的转换

    十进制转36进制: #36位映射模板 loop = '0123456789abcdefghijklmnopqrstuvwxyz' # 测试用例输入 n = a = [] : a.append( loo ...

  8. libcurl底层调用逻辑

    libcurl就不多介绍了,一个支持HTTP,FTP,SMTP等协议的网络库 只涉及multi部分,easy部分就不提了. 两个线程,一个负责添加HTTP请求,另一个轮询,负责处理每一个请求 Thre ...

  9. 10个linux网络和监控命令

    我下面列出来的10个基础的每个linux用户都应该知道的网络和监控命令.网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nsloo ...

  10. 【Python】Python基础教程系列目录

    Python是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. 在现在的工作及开发当中,Python的使用越来越广泛,为了方便大家的学习,Linux大学 特推出了 <Python基 ...