ZOJ 3781 Paint the Grid Reloaded(BFS)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially.
Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B.
Leo wants to paint the grid with the same color. He can make it done in multiple steps. At each step Leo can choose a cell and flip the color (from black to white or from white to black) of all cells connected to it. Leo wants to know the minimum number of steps he needs to make all cells in the same color.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N and M (1 <= N, M <= 40). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the initial color of the cells.
Output
For each test case, output the minimum steps needed to make all cells in the same color.
题目大意:给一个N*M的矩阵,每个位置是O或X,每次点击可以使一片连通的O/X全部变成X/O,问全部变成O或X要点多少次。
思路:首先如果把所有连通块看成一个点,相邻的连通块连一条边,可以得到一个平面图。先把所有边当成虚边,然后每次点击,可以把一个点和与其有实边连通的点的所有连接的虚边变成实边。
结论:存在最优解,每次点击的是同一个点。那么只需求出每个点x的到其他点的最短路径的最大值p[x](即最远点),答案为min{p[x]}。
证明:假设存在一个最优解ans,包含k个点。若k=1,证明结束。否则,k>1:
现定义集合S(x, v) = {y, dis(x, y) ≤ v},称为x的v-连通分量。其中dis(x,y)为x、y的最短路径长度。
假设最优解中x的点击次数为cnt[x]。
先选择k个点中任意两个相邻的cnt-连通分量,设为a、b。
假设最优解中a点击了cnt[a]下,b点击了cnt[b]下,那么有d=dis(a,b)≤cnt[a]+cnt[b]。
假设a到b的最短路径为p[0]=a、p[1]、p[2]……p[d]=b。
那么若我们只点击p[cnt[b]],点击cnt[a]+cnt[b]下,那么首先点击cnt[b]下必然可以到达点a,之后cnt[a]下可以覆盖原来的S(a, cnt[a])。
又因为d-cnt[b]≤cnt[a],那么点击cnt[a]下必然可以到达点b,之后的cnt[b]下可以覆盖原来的S(b, cnt[b])。
那么可以把a、b替换成p[cnt[b]],解的步数不变,依然是最优解。
不断重复上述过程直到剩下一个点,证毕。
代码(100ms):
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std; const int MAXN = ;
const int MAXV = ; vector<int> adj[MAXV];
char mat[MAXN][MAXN];
int a[MAXN][MAXN];
int n, m, T, id_cnt; int fx[] = {-, , , };
int fy[] = {, , , -}; void dfs(int r, int c, int col) {
if(a[r][c]) return ;
a[r][c] = col;
for(int f = ; f < ; ++f) {
int new_r = r + fx[f], new_c = c + fy[f];
if(mat[new_r][new_c] == mat[r][c])
dfs(new_r, new_c, col);
}
} void add_edge(int u, int v) {
adj[u].push_back(v);
} int dis[MAXV];
int bfs(int u) {
memset(dis + , 0x3f, id_cnt * sizeof(dis[]));
dis[u] = ;
queue<int> que; que.push(u);
while(!que.empty()) {
int u = que.front(); que.pop();
for(auto v : adj[u]) {
if(dis[v] > dis[u] + ) {
dis[v] = dis[u] + ;
que.push(v);
}
}
}
return *max_element(dis + , dis + id_cnt + );
} int main() {
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
memset(mat, , sizeof(mat));
memset(a, , sizeof(a));
for(int i = ; i <= n; ++i) scanf("%s", mat[i] + ); id_cnt = ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) if(!a[i][j])
dfs(i, j, ++id_cnt); for(int i = ; i <= id_cnt; ++i) adj[i].clear();
for(int r = ; r <= n; ++r)
for(int c = ; c <= m; ++c) {
for(int f = ; f < ; ++f) {
int new_r = r + fx[f], new_c = c + fy[f];
if(mat[new_r][new_c] != && mat[new_r][new_c] != mat[r][c])
add_edge(a[r][c], a[new_r][new_c]);
}
} for(int i = ; i <= id_cnt; ++i) {
sort(adj[i].begin(), adj[i].end());
adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());
}
int res = ;
for(int i = ; i <= id_cnt; ++i)
res = min(res, bfs(i));
printf("%d\n", res);
}
}
ZOJ 3781 Paint the Grid Reloaded(BFS)的更多相关文章
- 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 - [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 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- ZOJ 3781 Paint the Grid Reloaded
枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...
- Paint the Grid Reloaded(缩点,DFS+BFS)
Leo has a grid with N rows and M columns. All cells are painted with either black or white initially ...
- ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)
Paint the Grid Again Time Limit: 2 Seconds Memory Limit: 65536 KB Leo has a grid with N × N cel ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
随机推荐
- UIButton在Disabled状态下标题混乱的问题
最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...
- 转 Datatables中文API——基本参数
鉴于自己一直在使用datatables,发现这是个很不错的表格插件,但是好的东西都是英文的,所以我结合自己的使用经验,把官网的英文api做下简单的翻译,同时也希望大家把自己的使用经验一起分享出来,让我 ...
- 简述unix时间戳
unix时间戳是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒. Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix ti ...
- HTTP协议及其请求头分析
HTTP协议及其请求头分析 HTTP协议及其请求头分析 众所周知,Internet的基本协议是TCP/IP协议,目前广泛采用的FTP.Archie Gopher等是建立在TCP/IP协议之上的应用 ...
- CentOS安装开发组相关的包
yum groupinstall "Development Tools" yum groupremove "Development Tools"
- 创建删除元素appendChild,removeChild,createElement,insertBefore
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- SQL server 测试
设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表 ...
- Rocky4.2下安装达梦(DM)6数据库
1.准备操作系统 1.1 系统登录界面 1.2 操作系统版本信息 jdbh:~ # uname -ra Linux jdbh -x86_64 # SMP Fri Dec :: CST x86_64 G ...
- vbox下Oracle Enterprise liunx5.4虚拟机安装10G RAC实验(四)
接第3篇 http://www.cnblogs.com/myrunning/p/4003527.html 5.安装配置数据库 5.1安装数据库软件 5.2配置监听 5.3创建ASM磁盘 5.4创建服务 ...
- 2016HUAS暑假集训训练题 D - Find a way
F ...