【ZOJ - 3780】 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
如果某一行存在x,则这一行必然会被横着涂一遍,同时上一次可以为竖着涂这一列;如果是o也可以得到相似的结论,所以这样可以建立起一个有向图,用拓扑排序判断即可。
BFS拓扑排序
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=505;
vector<int>G[N<<1];
int n;
char a[N];
queue<int>que;
bool del[N<<1],vis[N<<1];
int in[N<<1];
bool bfs(){
memset(del,false,sizeof(del));
memset(vis,false,sizeof(vis));
priority_queue<int,vector<int>,greater<int> >q;
for(int i=1;i<=(n<<1);i++) if(in[i]==0)
q.push(i),vis[i]=true;
if(q.size()==0) return false;
for (int i=1;i<=(n<<1);i++){
int cur = q.top();
q.pop();
if(del[cur]) return false;
del[cur]=true;que.push(cur);
for (int j=0;j<G[cur].size();j++){
int nxt=G[cur][j];
if(del[nxt]) return false;
in[nxt]--;
if(in[nxt]==0)
q.push(nxt);
}
}
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(in,0,sizeof(in));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[j].push_back(i+n);
in[i+n]++;
}
else{
G[i+n].push_back(j);
in[j]++;
}
}
}
if(!bfs()||que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
if(vis[x]) continue;
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(!que.empty()) printf(" ");
}
puts("");
}
return 0;
}
dfs
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=515;
vector<int>G[N<<1];
int id[N<<1],n;
char a[N];
int vis[N<<1];
queue<int>que;
bool dfs(int u)
{
vis[u]=-1;
sort(G[u].begin(),G[u].end());
for(int i=0; i<(int)G[u].size(); i++){
int v=G[u][i];
if(vis[v]<0) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=1;
if(id[u]!=n) que.push(u);
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[i+n].push_back(j); //横着,大于n的为横着的标号
id[j]++;
}
else{
G[j].push_back(i+n); //竖着,小于n为竖着的标号
id[i+n]++;
}
}
}
for(int i=1; i<=(n<<1); i++) if(!id[i]&&!vis[i]) if(!dfs(i)){
//无前驱的节点可以作为开始节点
while(!que.empty()) que.pop();
break;
}
if(que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(que.empty()) puts("");
else printf(" ");
}
}
return 0;
}