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 ...
随机推荐
- About_MySQL Select--来自copy_03
MySQL Select 查询分类 单表查询:简单查询 多表查询:连接查询 联合查询:多个查询结果汇总 查询的组成 投影查询:挑选要显示的字段 select array1,array2,... f ...
- checkbox全选与反选
用原生js跟jquery实现checkbox全选反选的一个例子 原生js: <!DOCTYPE html> <html lang="en"> <hea ...
- 【转】Selenium WebDriver + Python 环境
转自:http://www.myext.cn/webkf/a_11878.html 1. 下载必要工具及安装包 1.1 [Python开发环境] 下载并安装Python 2.7.x版本 下载地址:ht ...
- intellij idea 插件 ideaVim
像Eclipse一样,idea这个公认最好的javaIDE也有Vim插件. 安装方法 File>Settings>Plugins>Install JetBrains plugin.. ...
- 读《深入php面向对象、模式与实践》有感(三)
命令模式: 第一次接触到这个命令模式的时候,感觉它很像一个简化的mvc框架.从不同的路径访问,再由控制器来判断所要调用的具体php文件. <?php class CommandContext{ ...
- 注入语句详解(get注入,cookie注入,搜索型注入等)
注意:对于普通的get注入,如果是字符型,前加' 后加 and ''=' 拆半法 ###################################### and exists (select ...
- CommonUtils.java
package com.vcredit.framework.utils; import java.lang.reflect.InvocationTargetException;import java. ...
- Kmeans方法
基本Kmeans算法介绍及其实现 http://blog.csdn.net/qll125596718/article/details/8243404/ kmeans++ http://www.52ml ...
- Linux 安装pip
参考:为Linux 系统安装pip pip: "A tool for installing and managing Python packages.",也就是说pip是pytho ...
- python随机服务器的双线出口ip发送邮件
#-*- coding:utf-8 -*-import smtplibimport sysimport random import socketfrom email.mime.text import ...