E - Paint the Grid Again
Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white).
Leo has a magical brush which can paint any row with black color, or any column with white color. Each time he uses the brush, the previous color of cells will be covered by the new color. Since the magic of the brush is limited, each row and each column can only be painted at most once. The cells were painted in some other color (neither black nor white) initially.
Please write a program to find out the way to paint the grid.
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 an integer N (1 <= N <= 500). Then N lines follow. Each line contains a string with N characters. Each character is either 'X' (black) or 'O' (white) indicates the color of the cells should be painted to, after Leo finished his painting.
Output
For each test case, output "No solution" if it is impossible to find a way to paint the grid.
Otherwise, output the solution with minimum number of painting operations. Each operation is either "R#" (paint in a row) or "C#" (paint in a column), "#" is the index (1-based) of the row/column. Use exactly one space to separate each operation.
Among all possible solutions, you should choose the lexicographically smallest one. A solution X is lexicographically smaller than Y if there exists an integer k, the first k - 1 operations of X and Y are the same. The k-th operation of X is smaller than the k-th in Y. The operation in a column is always smaller than the operation in a row. If two operations have the same type, the one with smaller index of row/column is the lexicographically smaller one.
Sample Input
2
2
XX
OX
2
XO
OX
Sample Output
R2 C1 R1
No solution
![]()

#include <bits/stdc++.h>
using namespace std;
const int mn = 505 * 505;
int in[mn], cnt, to[mn], nx[mn], fr[mn];
void addedge(int u, int v)
{
++cnt;
to[cnt] = v;
nx[cnt] = fr[u];
fr[u] = cnt;
in[v]++;
}
bool vis[1010];
int ans[1010];
char mp[505][505];
priority_queue<int, vector<int>, greater<int> > que;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
cnt = 0;
memset(in, 0, sizeof in);
memset(fr, -1, sizeof fr);
memset(vis, 0, sizeof vis);
while (!que.empty())
que.pop();
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%s", mp[i] + 1);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (mp[j][i] == 'O')
vis[i] = 1;
if (!vis[i]) /// 略去此列涂色
continue;
for (int j = 1; j <= n; j++)
if (mp[j][i] == 'X')
addedge(i, n + j);
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (mp[i][j] == 'X')
vis[n + i] = 1;
if (!vis[n + i]) /// 略去此行涂色
continue;
for (int j = 1; j <= n; j++)
if (mp[i][j] == 'O')
addedge(n + i, j);
}
int you = 0, num = 0;
for (int i = 1; i <= 2 * n; i++)
if (vis[i])
{
you++; // 需要的操作数
if (in[i] == 0)
que.push(i);
}
while (!que.empty())
{
int u = que.top();
que.pop();
ans[++num] = u;
for (int i = fr[u]; i != -1; i = nx[i])
{
int v = to[i];
in[v]--;
if (in[v] == 0)
que.push(v);
}
}
if (num != you) /// 成环
{
printf("No solution\n");
continue;
}
for (int i = 1; i < num; i++)
{
if (ans[i] <= n)
printf("C%d ", ans[i]);
else
printf("R%d ", ans[i] - n);
}
if (ans[num] <= n)
printf("C%d\n", ans[num]);
else
printf("R%d\n", ans[num] - n);
}
}

本文介绍了一种解决网格涂色问题的算法,该问题要求在有限的行和列操作次数下,将N×N的网格按照指定的颜色模式进行黑白涂色。文章详细解释了算法的实现过程,包括如何构建图模型,使用优先队列进行拓扑排序,以及如何选择字典序最小的解决方案。
326

被折叠的 条评论
为什么被折叠?



