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 ...
随机推荐
- 马士兵老师hadoop讲解总结博客地址记录(啊啊啊啊啊,自己没有保存写好的博客...)
http://www.cnblogs.com/yucongblog/p/6650822.html
- lintcode-402-连续子数组求和
[402-连续子数组求和(http://www.lintcode.com/zh-cn/problem/continuous-subarray-sum/) 给定一个整数数组,请找出一个连续子数组,使得该 ...
- 操作系统之实验二Step1-有序顺序表
实验二Step1-有序顺序表 专业:商业软件工程 班级:商软2班 姓名:甘佳萍 学号:201406114207 实验要求:初始化 输入数组元素个数. 输入n个数,排序输出. 存 ...
- 多tab点击切换
现在来一个小练习,就是用js实现多tab之间的切换: <body> <ul id="tab"> <li id="tab1"> ...
- 201621123037 《Java程序设计》第9周学习总结
作业09-集合与泛型z 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 这次改一个方式,就不用思维导图了,用图文结合方式来总结 1. Map三视图 键值: S ...
- 第142天:Size Marks下载安装和使用方法
Size Marks下载安装使用方法 一.下载安装 1.下载Size marks:链接: https://pan.baidu.com/s/1breyMf1 密码: fjsn 2. 复制 Size Ma ...
- mvc4中使用angularjs实现一个投票系统
数据库是用EF操作,数据表都很简单中,从代码中也能猜出表的结构,所以关于数据库表就不列出了 投票系统实现还是比较简单,投票部分使用ajax实现,评论部分是使用angularjs实现,并且页面每隔几秒( ...
- Java Servlet异步处理、非阻塞I/O和文件上传
异步处理 应用服务器中的 web容器通常对各个客户端情求分别使用一个服务器线程.在工作负载很繁重的情况下,容器常要大量线程来为所有客户端请求服务.可扩展性限制包括内存用尽,或容器线程池耗尽.为了创建可 ...
- 【Java】编程技术经典书籍列表
这个列表包括了 100 多本经典技术书籍,涵盖:计算机系统与网络.系统架构.算法与数据结构.前端开发.后端开发.移动开发.数据库.测试.项目与团队.程序员职业修炼.求职面试 和 编程相关的经典书籍. ...
- C 程序结构——Day01
C Hello World 实例 C 程序主要包括以下部分: 预处理器指令 函数 变量 语句 & 表达式 注释 让我们看一段简单的代码,可以输出单词 "Hello World&quo ...