ZOJ 3781 Paint the Grid Reloaded 连通块
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 连通块的更多相关文章
- 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+缩点思想)
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求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- 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
枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- 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构成的格子,对 ...
- 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 ...
随机推荐
- Alpha 冲刺(6/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...
- 实现Spring管理struts的Action
struts2和spring的整合,关键点在于struts2中的action要纳入spring容器的管理中成为一个bean. 可以在struts2中配置: <struts> ...
- Dsyy的第一篇博文~
2017-08-07 周一 晴热热热热热 咳咳,很多人看到dsyy第一反应是什么意思?当然是大神媛媛!显然不是些(diao)(si)yy.(da)(si)yy...的别义,咋有点此地无银三百两的感 ...
- url传带有汉字的参数乱码解决
url传带有汉字的参数乱码解决 var reg = new RegExp("(^|&)createName=([^&]*)(&|$)"); var r = ...
- hadoop和spark搭建记录
因玩票需要,使用三台搭建spark(192.168.1.10,192.168.1.11,192.168.1.12),又因spark构建在hadoop之上,那么就需要先搭建hadoop.历经一个两个下午 ...
- Apache Hadoop YARN – NodeManager--转载
原文地址:http://zh.hortonworks.com/blog/apache-hadoop-yarn-nodemanager/ The NodeManager (NM) is YARN’s p ...
- 【poj2409】Let it Bead Polya定理
题目描述 用 $c$ 种颜色去染 $r$ 个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $r·c\le 32$ . 题解 Polya定理 Burnside引理 ...
- day 008 文件操作
08. 万恶之源-⽂文件操作本节主要内容:1. 初识⽂文件操作2. 只读(r, rb)3. 只写(w, wb)4. 追加(a, ab)5. r+读写6. w+写读7. a+写读(追加写读)8. 其他操 ...
- BZOJ4977 跳伞求生(贪心)
如果现在选定了一些要求消灭的敌人而不考虑积分,显然应该让每个敌人被刚好能消灭他的人消灭.再考虑最大化积分,显然我们应该优先消灭ci-bi大的敌人,所选用的a也应尽量大.于是按ci-bi从大到小排序,用 ...
- 洛谷 P2055 [ZJOI2009]假期的宿舍
洛谷 P2055 题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C ...