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. MySQL的课堂的实践

    MySQL的课堂的实践 基本认识 如今的数据库有几种是主流,分别是:Oracle Database.Informix.SQL Server.PostgreSQL.MySQL等,我们现在学习的MySQL ...

  2. C#高级编程 (第六版) 学习 第四章:继承

    第四章 继承 1,继承的类型 实现继承: 一个类派生于一个基类型,拥有该基类型所有成员字段和函数. 接口继承 一个类型只继承了函数的签名,没有继承任何实现代码.   2,实现继承 class MyDe ...

  3. IOC与DI(xml 配置)

    Spring可以帮助我们管理软件开发过程中的对象,以及如何创建和维护对象之间的关系. Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,可以将组建的耦合度降至最低,即实现解耦 ...

  4. 结对编程——paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...

  5. Java实现的词频统计

    要求: 1.读取文件: 2.记录出现的词汇及出现频率: 3.按照频率降序排列: 4.输出结果. 概要: 1.读取的文件路径是默认的,为了方便调试,将要统计的文章.段落复制到文本中即可:2.只支持英文: ...

  6. (转) Elasticsearch 5.0 安装 Search Guard 5 插件

    一.Search Guard 简介 Search Guard  是 Elasticsearch 的安全插件.它为后端系统(如LDAP或Kerberos)提供身份验证和授权,并向Elasticsearc ...

  7. sphinx配置 + php

    1.    为什么要使用Sphinx   假设你现在运营着一个论坛,论坛数据已经超过100W,很多用户都反映论坛搜索的速度非常慢,那么这时你就可以考虑使用Sphinx了(当然其他的全文检索程序或方法也 ...

  8. 解决Qt creator无法输入中文

    详细的方法来自以下网址: http://my.oschina.net/lieefu/blog/505363?p={{currentPage+1}} 需要说明的几点: 设置qmake 的路径使用自身的路 ...

  9. Vue父组件与子组件传递事件/调用事件

    1.Vue父组件向子组件传递事件/调用事件 <div id="app"> <hello list="list" ref="child ...

  10. poll() 与 select()比较

    比较poll() 与select() 尽管poll()和select()所做的是相同的工作,不过poll()优于select(),原因:    1.poll()不需要用户计算并传递作为参数的最高编号的 ...