ZOJ 3781 Paint the Grid Reloaded 缩点+bfs
2015-08-24 09:32
405 查看
Paint the Grid Reloaded
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
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.
Step 1. flip (2, 2)
Step 2. flip (1, 2)
相当于每次从一个联通块只能到达相邻的不同颜色的联通块,由于棋盘是二维的,对于每个联通块跑一遍bfs就可以找到离他最远的联通块要几次操作才能得到,为了加速bfs要缩点。水题..
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
2 2 2 OX OX 3 3 XOX OXO XOX
Sample Output
1 2
Hint
For the second sample, one optimal solution is:Step 1. flip (2, 2)
XOX OOO XOX
Step 2. flip (1, 2)
XXX XXX XXX
相当于每次从一个联通块只能到达相邻的不同颜色的联通块,由于棋盘是二维的,对于每个联通块跑一遍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 ; int n, m; int vis ; 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; }
相关文章推荐
- Raising Modulo Numbers
- Open-Drain与Push-Pull
- chfn,chsh,last,login,mail ,mesg ,talk,wall,write,nice ,pstree ,renice,skill ,expr ,reset,tset,compress ,lpd ,lpq ,lpr ,lprm,fdformat ,mformat ,mkdosf
- chfn,chsh,last,login,mail ,mesg ,talk,wall,write,nice ,pstree ,renice,skill ,expr ,reset,tset,compress ,lpd ,lpq ,lpr ,lprm,fdformat ,mformat ,mkdosf
- Frick'ing Terrain Mesh!
- Training JTAG Interface
- ZOJ 3780 Paint the Grid Again 模拟
- 2015 Multi-University Training Contest 10 CRB and String
- 2015 Multi-University Training Contest 7 Clock
- 2015 Multi-University Training Contest 7 Gray code
- 解题报告 之 HDU5319 Painter
- See LCS again 最长递增子序列到最长公共子序列的转化
- TurboMail邮件系统优质售后创造产品价值
- MyBatis批量操作报错:Parameter 'xxxList' not found. Avail
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Minimization dp
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Order Book 模拟
- POJ 1995 Raising Modulo Numbers(快速幂取余)
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Array 模拟
- NMAKE : fatal error U1045: spawn failed : Permission denied
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进