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.

<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;
}

stackaslist,again

05-10

//基类stackrn#ifndef STACK_Hrn#defineSTACK_Hrntemplaternclass stackrnrnvirtual ~stack()=0;rnvirtual const T&top()const=0;rnvirtual void push(const T&)=0;rnvirtual const T& pop()=0;rn rn;rn#endifrn//rnrnrnrnrnrnrnrn//派生类stackaslistrn//stack的实现用list来完成。分别用stl中list和userdefine的list.rn#includern#includern#include"stack.h"rnusing std::domain_error;rnrnrntemplaternclass stackaslist:public stackrnrnpublic:rnstackaslist();rn~stackaslist();//用stl中的list应该没有必要再使用destructor了吧??????rnrnvoid push(const T& obj);rnconst T& top();rnconst T& pop();rnrnint getsize();rnrnprivate:rnint size;//最大的存储空间rnint count;//已经存储数据的个数rnstd::list li;rn rn;rnrnrnrnrnrntemplaternstackaslist::stackaslist()//提示说基类stack没有可用的默认构造函数???????rn //这个构造函数应该怎么写?rnsize=0;//0li.max_size; ;//应该多大?????????rncount=0;rnrnrntemplaternstackaslist::~stackaslist()rnrnrnrnrnrntemplaternint stackaslist::getsize()rnrnreturn size;rnrnrntemplaternconst T& stackaslist::pop()rnrnif(count==0)rnthrow domain_error("the stack is emputy");rnconst T temp= *li.end();rnli.remove(temp);rncout<<"after pop() the size is"<rn void stackaslist::push(const T& obj)rnrnif(count==size)rnthrow domain_error("the stack is full");rnli.push_front(obj);rncout<<"now the size after push is"<rnconst T& stackaslist::top()rnrnreturn *li.end();rnrnrnrnrnrn//main.cpp#includern#includern#include"stack.h"rn#include"stackaslist.h"rnusing std::cout;rnusing std::endl;rnint main()rnrnstackaslist sta;rncout<<"now the size"<

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试