ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
来源:互联网 发布:jsp网上选课系统源码 编辑:程序博客网 时间:2024/06/04 19:53
题目地址:点击打开链接
题意:
给一个n*m的由X或O构成的图,对一个点操作可以使与它相连通(上下左右)的所有一样颜色的格子翻转颜色(X—>O
或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色。
思路:
因为每次翻转都可以把与该点所在连通块相邻的连通块变成同一颜色,要都变成一种颜色,也就是遍历完最远的那个连
通块。现在可以对每个连通块进行缩点,相邻的连通块建
边,所以枚举每个连通块求到最远的步数,找到最远的步数中最小的即为答案。
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn = 50;vector<int> g[maxn*maxn];char pic[maxn][maxn];int num[maxn][maxn];bool vis[maxn][maxn], book[maxn*maxn];int row, col, k, nex[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};struct node{ int x, y; node() {} node(int xx, int yy): x(xx), y(yy) {}};void bfs1(int x, int y, int f){ queue<node> q; q.push(node(x, y)); vis[x][y] = 1; num[x][y] = f; while(!q.empty()) { node u = q.front(); q.pop(); for(int i = 0; i < 4; i++) { int tx = u.x+nex[i][0]; int ty = u.y+nex[i][1]; if(tx >= 1 && tx <= row && ty >= 1 && ty <= col && pic[tx][ty] == pic[x][y] && !vis[tx][ty]) { vis[tx][ty] = 1; num[tx][ty] = f; q.push(node(tx, ty)); } } }}int bfs2(int x){ memset(book, 0, sizeof(book)); queue<node> q; q.push(node(x, 0)); book[x] = 1; int u, s; while(!q.empty()) { u = q.front().x; s = q.front().y; q.pop(); for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if(!book[v]) { book[v] = 1; q.push(node(v, s+1)); } } } return s;}int main(void){ int t; cin >> t; while(t--) { for(int i = 0; i < maxn*maxn; i++) g[i].clear(); scanf("%d%d", &row, &col); for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) scanf(" %c", &pic[i][j]); k = 1; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) if(!vis[i][j]) bfs1(i, j, k++); for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) { if(i-1 >= 1 && num[i-1][j] != num[i][j]) { g[num[i-1][j]].push_back(num[i][j]); g[num[i][j]].push_back(num[i-1][j]); } if(j-1 >= 1&& num[i][j-1] != num[i][j]) { g[num[i][j-1]].push_back(num[i][j]); g[num[i][j]].push_back(num[i][j-1]); } } int ans = row*col; for(int i = 1; i < k; i++) ans = min(ans, bfs2(i)); printf("%d\n", ans); } return 0;}
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
- ZOJ 3781 Paint the Grid Reloaded(缩点+bfs)
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- ZOJ 3781 - Paint the Grid Reloaded(缩点dfs+bfs)
- ZOJ 3781Paint the Grid Reloaded 缩点+bfs
- ZOJ 3781 Paint the Grid Reloaded 题解 (dfs缩点+BFS)
- ZOJ 3781 Paint the Grid Reloaded BFS
- zoj 3781 Paint the Grid Reloaded (bfs)
- ZOJ-3781-Paint the Grid Reloaded 缩点+建图+spfa
- ZOJ3781-Paint the Grid Reloaded(缩点+bfs)
- ZOJ 3781 Paint the Grid Reloaded 图论 bfs
- zoj 3781 Paint the Grid Reloaded(前向星+缩点+最短路)
- zoj3781 Paint the Grid Reloaded --- 缩点 bfs
- Paint the Grid Reloaded zoj3781(缩点+dfs + bfs 染色)
- ZOJ 3781 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
- <历届试题>蓝桥杯之格子刷油漆
- M 数值分解
- [LeetCode]543. Diameter of Binary Tree
- Servlet学习笔记《一》
- 练:python爬取小说
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)
- Android Lint 检查规则列表
- 红米3报Installation failed with message Failed to establish session错误
- Pattern和Matcher具体的用法
- Android业务组件化之子模块SubModule的拆分以及它们之间的路由Router实现
- 关于Android的https通讯安全
- centos7虚拟机设置网络
- C++11之std--future和std--promise
- Halcon和MFC的数据的转换并插入List control控件中