Paint the Grid Again

3人阅读 评论(0) 收藏 举报
分类:

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.

<h4< dd="">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.

<h4< dd="">Sample Input
2
2
XX
OX
2
XO
OX
<h4< dd="">Sample Output
R2 C1 R1
No solution
//用拓扑排序,将每一行i看成一个点,几位i,每一列j看成一个点 ,记为n+j;一共有2*n-1个点 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
vector<int>edge[550*2];
int inde[550*2];
int n;
int first[550*2]; 
void topo()
{
	memset(first,0,sizeof(first));
	priority_queue<int,vector<int>,greater<int> >q;
	int i;
	for(i=0;i<n*2;i++)
	{
		if(inde[i]==0)
		{
			q.push(i); 
			first[i]=1;//标记所有没有入度的点 
		}
		
	}	
	vector<int>ans;
	ans.clear();
	while(!q.empty())
	{
		int node=q.top();
		ans.push_back(node);
		q.pop();
		for(i=0;i<edge[node].size();i++)//由于建图时,必定从0~2*n-1,所以遍历时,必定是从字典序从小到大 
		{
			inde[edge[node][i]]--;
			if(inde[edge[node][i]]==0)
			{
				q.push(edge[node][i]);
			}
		}
	}
	if(ans.size()==n*2)
	{
		for(i=0;i<ans.size()-1;i++)
		{
			if(first[ans[i]]==1) continue;//对于入度为0的点,无需操作,比如样例1,本该为C2,R2,C1,R1,但C2的操作会被覆盖,不如不操作 
            if(ans[i]>=n)
			{
                printf("C%d ",ans[i]-n+1);
            }else{
                printf("R%d ",ans[i]+1);
            }	
		}
		if(ans[i]>=n)
		{
            printf("C%d\n",ans[i]-n+1);
        }else
		{
            printf("R%d\n",ans[i]+1);
        }	
	}else
	{
		printf("No solution\n");
	}
}
int main()
{
	int t;
	while(~scanf("%d",&t))
	{
		int i,j;
		while(t--)
		{
			for(i=0;i<550*2;i++)
				edge[i].clear();
			memset(inde,0,sizeof(inde));
			scanf("%d",&n);
			for(i=0;i<n;i++)
			{
				getchar();
				for(j=0;j<n;j++)
				{
					char x;
					scanf("%c",&x);
					if(x=='X')//若(i,j)点位X,则该点必定先刷第j列,再刷第i行 
					{
						edge[n+j].push_back(i);
						inde[i]++;
					}
					else//若(i,j)点位O,则该点必定先刷第i行,再刷第j列 
					{
						edge[i].push_back(n+j);
						inde[n+j]++;
					}
				}//建图 
			}
			topo();
		}
	}
	return 0;
}

查看评论

Paint the Grid Again----模拟||拓扑

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N ×...
  • bless924295
  • bless924295
  • 2017-01-14 12:04:15
  • 183

zoj 3780 Paint the Grid Again

#include #include #include using namespace std; const int N=1502; int tt,n; char mp[N][N]; bool col...
  • zhengweijian15
  • zhengweijian15
  • 2014-04-16 12:11:14
  • 555

ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB ...
  • nealgavin
  • nealgavin
  • 2014-05-08 21:35:50
  • 940

ZOJ 3780 Paint the Grid Again (逆推)

思路:如果这张图能涂成的话,那么我们每次一定能找到一行或一列一样的,涂完的这行或者列就可以是任意的了,在接着向前边的状态变换。注意题目要求的字典序最小,那么可以列得在行前,所以我们从后往前处理时先处理...
  • wing_wuchen
  • wing_wuchen
  • 2017-02-18 13:02:23
  • 298

练习赛(三)E - Paint the Grid Again

题目链接~~> 做题感悟:这题开始读时感觉很麻烦输出
  • u011074149
  • u011074149
  • 2014-04-20 07:32:49
  • 667

ZOJ 3781 Paint the Grid Reloaded

题目链接~~>
  • u011074149
  • u011074149
  • 2014-04-21 15:21:47
  • 771

ZOJ3780 Paint the Grid Again(模拟)

题目传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 题意: 给一块n*n的格子,每次可以将任意行变成X,...
  • Rain722
  • Rain722
  • 2017-03-20 18:21:11
  • 207

ZOJ 3780 Paint the Grid Again

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N...
  • u012797220
  • u012797220
  • 2014-04-14 17:31:44
  • 1343

Paint the Grid Again ZOJ - 3780

Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or ...
  • qq_33362864
  • qq_33362864
  • 2017-01-14 16:03:29
  • 131

ZOJ 3780 - Paint the Grid Again

E - Paint the Grid Again Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu...
  • leifjacky
  • leifjacky
  • 2016-04-11 16:17:42
  • 248
    个人资料
    持之以恒
    等级:
    访问量: 817
    积分: 177
    排名: 90万+
    文章存档