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. Hash开散列 拉链法

    #include<iostream> #include<cstdio> using namespace std; const int maxn=1000007; struct ...

  2. KMP的原理和代码实现(详细注释|参考多个博客总结|可作为模板)

    KMP算法解决的问题是字符匹配,是由Knuth–Morris–Pratt共同开发出来的,这个算法把字符匹配的时间复杂度缩小到O(m+n),而空间复杂度也只有O(m),n是target的长度,m是pat ...

  3. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

  4. IT小小鸟读后感言

    有感 读了我是一只IT小小鸟之后, 我发现上大学得靠自己自学,确定自己的目标和方向,多去参与实验和自己多锻炼编写程序.我现在大一,还有很多时间来让自己变得更好,虽然要补考两门课程,但是还是不要失去信心 ...

  5. Ubuntu系统升级内核方法

    一.查看内核版本 $ uname-sr //查看内核版本 二.去Ubuntu网站http://kernel.ubuntu.com/~kernel-ppa/mainline/下载所需版本的deb文件 w ...

  6. gridview 第一行编辑

    <%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.c ...

  7. mvc4中使用部分视图局部刷新实例

    如上效果图,网页中有主视图(上)和部分视图(下),点击提交会把文本框中的值发送到服务器,再返回所有添加的信息,在下方局部更新(只更新部分视图),实现如下: 1.网页主视图代码: @model MvcA ...

  8. 【bzoj2600】[Ioi2011]ricehub 双指针法

    题目描述 给出数轴上坐标从小到大的 $R$ 个点,坐标范围在 $1\sim L$ 之间.选出一段连续的点,满足:存在一个点,使得所有选出的点到其距离和不超过 $B$ .求最多能够选出多少点. $R\l ...

  9. TCP的拥塞控制 (二)

    TCP Reno TCP  Reno引入了ssthresh(Slow Start threshold)变量,作为TCP的Slow Start和Congestion Avoidance两个阶段的分界线. ...

  10. hive 分区表和分桶表

    1.创建分区表 hive> create table weather_list(year int,data int) partitioned by (createtime string,area ...