ZOJ 3781Paint the Grid Reloaded 缩点+bfs
来源:互联网 发布:云计算与电子政务 编辑:程序博客网 时间:2024/06/05 08:06
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.
Sample Input
22 2OXOX3 3XOXOXOXOX
Sample Output
12
Hint
For the second sample, one optimal solution is:
Step 1. flip (2, 2)
XOXOOOXOX
Step 2. flip (1, 2)
XXXXXXXXX
相当于每次从一个联通块只能到达相邻的不同颜色的联通块,由于棋盘是二维的,对于每个联通块跑一遍bfs就可以找到离他最远的联通块要几次操作才能得到,为了加速bfs要缩点。水题..
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <set>using namespace std;typedef long long LL;const int N = 40 + 10;char g[N][N];int n, m;int vis[N][N];int dx[] = {0, 0, 1, -1};int dy[] = {1, -1, 0, 0};set<int>Map[1600 + 10];struct Edge{int to, nxt;}e[(1610 * 4) << 1];int head[1610];int tot;void initGraph(){ tot = 0; memset(head, 0, sizeof head);}void add(int u, int v){ e[++tot].to = v; e[tot].nxt = head[u]; head[u] = tot; e[++tot].to = u; e[tot].nxt = head[v]; head[v] = tot;}int sz;void dfs(int i, int j, char t){ int x, y; for(int k = 0; k < 4; k++){ x = i + dx[k]; y = j + dy[k]; int u = sz, v = vis[x][y]; if (v && g[x][y] != t && !Map[u].count(v)){ Map[u].insert(v); Map[v].insert(u); add(u, v); }else if (!v && g[x][y] == t){ vis[x][y] = sz; dfs(x, y, t); } }}int d[1600 + 10];int bfs(int k){ int re = 0; queue<int>q; int x, y; memset(d, 0, sizeof d); q.push(k); d[k] = 1; while(!q.empty()){ x = q.front(); q.pop(); for(int i = head[x]; i; i = e[i].nxt){ y = e[i].to; if (d[y]) continue; q.push(y); d[y] = d[x]+1; re = max(re, d[y] - 1); } } return re;}int main(){// freopen("data.in", "r", stdin);// freopen("data.out", "w", stdout); int T; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ scanf("%s", g[i]+1); } for(int i = 1; i <= n; i++){ g[i][0] = g[i][m+1] = '#'; } for(int i = 1; i <= m; i++){ g[0][i] = g[n+1][i] = '#'; } sz = 0; memset(vis, 0, sizeof vis); for(int i = 0; i < 1610; i++) Map[i].clear(); initGraph(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if (vis[i][j]) continue; vis[i][j] = ++sz; dfs(i, j, g[i][j]); } } int ans = sz; for(int i = 1; i <= sz; i++){ ans = min(ans, bfs(i)); } printf("%d\n", ans); } return 0;}
- ZOJ 3781 - Paint the Grid Reloaded(缩点dfs+bfs)
- ZOJ 3781Paint the Grid Reloaded 缩点+bfs
- ZOJ 3781 Paint the Grid Reloaded(缩点+bfs)
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- ZOJ 3781 Paint the Grid Reloaded BFS
- zoj 3781 Paint the Grid Reloaded (bfs)
- ZOJ 3781 Paint the Grid Reloaded 题解 (dfs缩点+BFS)
- ZOJ-3781-Paint the Grid Reloaded 缩点+建图+spfa
- ZOJ 3781 Paint the Grid Reloaded 图论 bfs
- zoj3781 Paint the Grid Reloaded --- 缩点 bfs
- Paint the Grid Reloaded zoj3781(缩点+dfs + bfs 染色)
- ZOJ3781-Paint the Grid Reloaded(缩点+bfs)
- zoj 3781 Paint the Grid Reloaded
- zoj 3781 Paint the Grid Reloaded
- ZOJ 3781 Paint the Grid Reloaded
- Paint the Grid Reloaded ZOJ - 3781
- zoj 3781 Paint the Grid Reloaded(前向星+缩点+最短路)
- ZOJ Paint the Grid Reloaded
- A1097. Deduplication on a Linked List (25)
- 控制器操作方法后缀
- Ogre 1.8.1源代码编译和错误修改
- C语言:const关键字、结构体
- BASH第七课第二题
- ZOJ 3781Paint the Grid Reloaded 缩点+bfs
- 压缩跟踪Compressive Tracking
- 《七夕情书项目》
- POJ3264 Balanced Lineup 线段树||RMQ
- 黑马程序员—学习笔记—各种排序法
- 压缩感知(Compressive Sensing)学习之(一)
- 天声人語 20150824
- 压缩感知(Compressive Sensing)学习之(二)
- 黑马程序员-[OC]学习之旅-NSString使用和方法