Description

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
 
题意:
在一张空白的图上有两个操作:
·Rx  将x行涂成黑色
·Cx  将x列涂成白色
每行每列只能进行一次操作。
给定一个目标的图形,问至少需要几次操作才能达到目标图形,输出路径
分析:
一开始就想到了用图论来解决
我首先想到了如何建图:
由于每个点最多会被涂两次(R一次,C一次)。
由于R和C涂的颜色是不一样的,我们可以根据这个点的目标颜色判断出对这个点的这两次操作的先后顺序。
由此可以按先后顺序建一条边。
我们的目的就是求出一条路径满足所有这些条件(即拓扑排序)
最后题目要求字典序最小的方案,由于列变换字符'C'的字典序比行变换'R'的字典序小,因此把列号设为1~n,行号设为n+1~2n,而且要求变换的行列坐标也要最小,因此用最小堆的优先队列来代替普通队列进行拓扑排序,
另外注意一点,起点(第一个入度为0的点)是不用涂的。因为起点在后面涂的时候一定会被覆盖
比如单一个点'X',拓扑序为第1列->第1行,但是显然刷第1列这个操作是多余的。
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1030;
struct edge
{
int e;
int nxt;
edge():nxt(0){};
edge(int e2,int nxt2):e(e2),nxt(nxt2){};
}e[MAXN*MAXN];
int head[MAXN];
int tot;
int deg[MAXN];
int n;
void add(int b,int ee)
{
e[tot]=edge(ee,head[b]);
head[b]=tot++;
}
std::vector<int> res;
int non[MAXN];
bool topo()
{
priority_queue<int,vector<int>,greater<int> > q;
for(int i=1;i<=2*n;i++){ //!注意是2*n
if(deg[i]==0){
q.push(i);
//res.push_back(i);
//cout<<"push "<<i<<endl;
non[i]=1;
}
}
int t;
int now;
while(!q.empty()){
t=q.top();
q.pop();
res.push_back(t);
for(int i=head[t];i!=0;i=e[i].nxt){
now=e[i].e;
deg[now]--;
if(!deg[now]){
q.push(now);
}
}
}
//cout<<"size "<<res.size()<<endl;
return res.size()==n*2;//!注意是2*n
}
void init(){
memset(head,0,sizeof(head));
memset(deg,0,sizeof(deg));
tot=1;
res.clear();
memset(non,0,sizeof(non));
}
int main()
{
//freopen("data.in","r",stdin);
int t;
scanf("%d",&t);
char ch;
while(t--){
init();
scanf("%d",&n);
getchar();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ch=getchar();
if(ch=='X'){
add(j,i+n);deg[i+n]++;
}
else{
add(i+n,j);deg[j]++;
}
}
getchar();
}
if(!topo()){
printf("No solution\n");
}
else{
int now=0;
int len=res.size();
for(int i=0;i<len;i++){
now=res[i];
if(non[now]) continue;
else{
printf("%c%d%c",now>n?'R':'C',now>n?now-n:now,i==len-1?'\n':' ');
}
}
//printf("\n");
}
}
}

最新文章

  1. Java多线程5:synchronized锁方法块
  2. Kernel Methods (5) Kernel PCA
  3. ERP权限设置和CRM分析 (十二)
  4. java常见类型的转化以及风险
  5. Silverlight中嵌套html、swf、pdf
  6. CentOS 6.4 下搭建 MongoDB 2.4.9 环境
  7. 为什么要CGI
  8. Struts2 教程
  9. 安全HTTPS-全面详解对称加密,非对称加密,数字签名,数字证书和HTTPS【下】
  10. Android dump .so 文件crash log
  11. 统计数据方面SQL与HQL
  12. 简单的独享smb
  13. Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size
  14. vue 学习中 版本、问题集锦
  15. 如何用python下载一张图片
  16. WiFi-ESP8266入门http(3-1)网页认证上网-post请求(原教程)
  17. IntelliJ IDEA详情
  18. [CF528D]Fuzzy Seach
  19. vue里的v-show和v-if
  20. 异常分类VS垃圾分类

热门文章

  1. 摘抄大神对VUE 中slot-scope的深度理解
  2. DLL的创建与使用
  3. git clone 含有子模块的项目
  4. Git_基础命令
  5. CentOS 7 关闭Selinux
  6. java之设计模式汇总
  7. 链接校验——是否是协议http://或https://开头的
  8. 转pip更新后ImportError: cannot import name ‘main&#39;
  9. c++ 数组 结构体
  10. C#和Java的最大不同