您的位置:首页 > 大数据 > 人工智能

zoj 3780 Paint the Grid Again(拓扑排序)

2015-04-16 23:03 393 查看
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 thanY 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
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5267

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
int stack[1005];
int yes[1005];
char x[501][501];
int h[1005];
int n;
vector<int> ans;
int top;
int cmp(int x,int y){
	return x>y;}
void work(){
	queue<int> q;
	sort(stack,stack+top+1,cmp);
	for(int i=0;i<=top;++i){
		q.push(stack[i]);
		ans.push_back(stack[i]);
		yes[stack[i]]=-1;}
	while(!q.empty()){
		top=-1;
		int fr=q.front();
		q.pop();
		if(fr<n){
			for(int i=0;i<n;++i){
				h[i+n]++;
				if(yes[i+n]!=-1&&h[i+n]==n)
					stack[++top]=i+n;}}
		else{
			for(int i=0;i<n;++i){
				h[i]++;
				if(yes[i]!=-1&&h[i]==n)
					stack[++top]=i;}}
		sort(stack,stack+top+1,cmp);  //每次排序的原因是题目要求按序输出
		for(int i=0;i<=top;++i){
			q.push(stack[i]);
			ans.push_back(stack[i]);
			yes[stack[i]]=-1;}}
	for(int i=0;i<2*n;++i){
		if(yes[i]==0){
			cout<<"No solution"<<endl;
			return;}}
	for(int i=ans.size()-1;i>0;--i){
		int u=ans[i];
		if(u<n)
			cout<<"C"<<u+1<<" ";
		else
			cout<<"R"<<u-n+1<<" ";}
	    int u=ans[0];
		if(u<n)
			cout<<"C"<<u+1;
		else
			cout<<"R"<<u-n+1;
	cout<<endl;}
int main(){
	int t;
	cin>>t;
	while(t--){
	top=-1;
	ans.clear();
	memset(x,0,sizeof(x));
	memset(stack,0,sizeof(stack));
	memset(h,0,sizeof(h));
	memset(yes,0,sizeof(yes));
	cin>>n;
	for(int i=0;i<n;++i){
		for(int j=0;j<n;++j){
			cin>>x[i][j];
			if(x[i][j]=='X')
				h[i+n]++;}
		if(h[i+n]==n)
			stack[++top]=i+n;
		else if(h[i+n]==0)
			yes[i+n]=-1;}
	for(int j=0;j<n;++j){
		for(int i=0;i<n;++i){
			if(x[i][j]=='O')
				h[j]++;}
		if(h[j]==n)
			stack[++top]=j;
		else if(h[j]==0)
			yes[j]=-1;}
	if(top==-1)
		cout<<"No solution"<<endl;
	else
		work();}
	return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: