题目地址:点击打开链接
题意:
给一个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
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