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 ...
随机推荐
- 不错的判断 UITextView 内容不超过20个字符串的方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...
- MySQL连接池
1. using System; using System.Collections; using MySql.Data.MySqlClient; namespace Helper { /// < ...
- 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)
华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...
- Cento 安装配置FastFDS
unzip -x libfastcommon-master.zip ./make.sh ./make.sh install /usr/include/fastcommon cd FastDFS ./m ...
- Linux下安装JDK和tomcat
1.新建用户 2.解压 jdk-7u67-linux-x64.tar.gz 到本地 3.配置环境变量 编辑.bash_profile文件 4.生效 5.安装tomcat 6.验证tomcat是否安装成 ...
- #define is unsafe——I
I. #define is unsafe Have you used #define in C/C++ code like the code below? #include <stdio.h&g ...
- JQuery小结
一.选择网页元素 jQuery的基本设计和主要用法,就是"选择某个网页元素,然后对其进行某种操作".这是它区别于其他函数库的根本特点. 使用jQuery的第一步,往往就是将一个选择 ...
- 创建删除元素appendChild,removeChild,createElement,insertBefore
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- spring security remember me实现自动登录
1 默认策略 在我们自定义的login中增加一个选择框 <input type="submit" value="Login" /> <br/& ...
- asp.net 修改/删除站内目录操作后会导致Session丢失
http://www.jb51.net/article/21770.htm http://blog.chinaunix.net/uid-7425507-id-134216.html 在Web项目中使用 ...