Paint the Grid Again(拓扑排序)

题目链接:https://cn.vjudge.net/problem/ZOJ-3780

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
题意:一次涂色能把一行涂成X,或把一列涂成O,在用最少次数涂色的前提下,输出字典序最小的涂色方案(列涂色比行涂色小,涂色下标小的较小)。

根据当前的涂色状态,是可以反推上一个涂色状态的。对于在i行、j列,当前状态为X的格子,我们可以想到,对于i行的涂色必定发生在对于j列的涂色之后。

也就是说,可以推出若干组涂色的两两相对顺序。

因此,将各种涂色情况设为点,从Cj向Ri引有向边(先发生的涂色->后发生的涂色),对于这个有向图求出的拓扑序列就可以满足最终达到输入涂色状态的要求。
---------------------
原文:https://blog.csdn.net/qq_34845082/article/details/70217605

 

注意:最开始的入度为零的点,是不能操作的,因为这些点并没有状态转化过来

 

#include<bits/stdc++.h>
using namespace std;
vector<int>edge[1005];
int ind[1005];
char mp[505][505];
int first[1005];
vector<int> v;
int n;
void TP(){
	priority_queue<int,vector<int>,greater<int> >q;
	for(int i=1;i<=n*2;i++){
		if(ind[i]==0){
			q.push(i);
			first[i]=true;
		}
	}
	while(!q.empty()){
		int x=q.top();
		q.pop();
		v.push_back(x);
		for(int i=0;i<edge[x].size();i++){
			int y=edge[x][i];
			ind[y]--;
			if(ind[y]==0) q.push(y);
		}
	}
	for(int i=1;i<=2*n;i++){
		if(ind[i]){     //如果最后还有点的入度不为0,那么No solution 
			v.clear();  
			break;
		}
	}
}
void init(){
	for(int i=1;i<=1000;i++){
		edge[i].clear();
	}
	memset(ind,0,sizeof(ind));
	memset(first,0,sizeof(first));
	v.clear();
} 
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%s",mp[i]+1);
			for(int j=1;j<=n;j++){
				if(mp[i][j]=='O'){//如果最后是白色,那么肯定是先行染黑,再列染白 
					edge[i].push_back(n+j);
					ind[n+j]++;
				}
				else{
					edge[n+j].push_back(i);
					ind[i]++;
				} 
			}
		}
		TP();
		if(v.size()==0) printf("No solution\n");
		else{
			for(int i=0;i<v.size();i++){
				if(first[v[i]]) continue;
				if(v[i]>n) printf("C%d%c",v[i]-n," \n"[i==v.size()-1]);
				else printf("R%d%c",v[i]," \n"[i==v.size()-1]);
 			}
		}
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值