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
/*
题意:一个矩阵有两种操作,将每行染成黑色,将每列染成白色每行,每列只能操作一次
现在给你特定的矩阵,最初的序列是什么颜色也没有的,问你至少操作几次才能形成
制定的矩阵 初步思路:对于单个元素来说,如果最后的颜色是黑色那么肯定是先进性染白色的操作,
然后进行的黑色操作,将行列信息建成图,然后用拓扑排序,进行排序并字典序输出 #错误:有一个点,就是最开始的入度为零的点,是不能操作的,因为这些点并没有状态转
化过来
*/
#include <bits/stdc++.h>
using namespace std;
vector<int>edge[];
int inv[];//表示每个点的入度
int t;
int n;
char mapn[][];
int frist[];//表示是不是第一个点
void topu(vector<int> &v){//用于存储操作的顺序
priority_queue<int,vector<int>,greater<int> >q;
for(int i=;i<n*;i++){//将所有的入度为零的点加入队列
if(inv[i]==){
q.push(i);
frist[i]=;
}
}
while(!q.empty()){
int x=q.top();
v.push_back(x);
q.pop();
for(int i=;i<edge[x].size();i++){
int Next=edge[x][i];
inv[Next]--;//将于这个点相关的边都删掉
if(inv[Next]==){//如果入度为零了那么加进队列
q.push(Next);
}
}
}
for(int i=;i<n*;i++){
if(inv[i]){
v.clear();
break;
}
}
}
void init(){
for(int i=;i<;i++){
edge[i].clear();
}
memset(inv,,sizeof inv);
memset(frist,,sizeof frist);
}
int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",mapn[i]);
for(int j=;j<n;j++){//按照元素信息进行建图
if(mapn[i][j]=='O'){//如果最后的颜色是黑色的那么肯定是先进行列染白色的,然后进行行染黑
edge[i].push_back(n+j);
inv[n+j]++;
}else{//如果最后的颜色是白色,那么肯定是先进行 行染黑色,然后进行列染白色
edge[n+j].push_back(i);
inv[i]++;
}
}
}
//建好图了然后进行topu排序
vector<int>v;
v.clear();
topu(v);
if(v.size()==){
puts("No solution");
}else{
for(int i=;i<v.size()-;i++){
if(frist[v[i]]) continue;
if(v[i]>=n){
printf("C%d ",v[i]-n+);
}else{
printf("R%d ",v[i]+);
}
}
if(v[v.size()-]>=n){
printf("C%d\n",v[v.size()-]-n+);
}else{
printf("R%d\n",v[v.size()-]+);
}
}
}
return ;
}

Paint the Grid Again (隐藏建图+优先队列+拓扑排序)的更多相关文章

  1. 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序

    题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆.  现在 ...

  2. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  3. 模拟赛T2 线段树优化建图+tarjan+拓扑排序

    然而这只是 70pts 的部分分,考场上没想到满分怎么做(现在也不会) code: #include <cstdio> #include <string> #include & ...

  4. HDU 4857 逃生 【拓扑排序+反向建图+优先队列】

    逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  5. bzoj5017 炸弹 (线段树优化建图+tarjan+拓扑序dp)

    直接建图边数太多,用线段树优化一下 然后缩点,记下来每个点里有多少个炸弹 然后按拓扑序反向dp一下就行了 #include<bits/stdc++.h> #define pa pair&l ...

  6. POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)

    (点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...

  7. 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  8. 图的拓扑排序,AOV,完整实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. HDU 4857 逃生(反向建边的拓扑排序+贪心思想)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

随机推荐

  1. 如何保存或读取数据(到android的data目录)利用context获取常见目录可优化代码

    读取用户信息 当然这里可以有多种返回值 非硬性

  2. ng-file-upload(在单文件选择,并且通过点击“上传”按钮上传文件的情况下,如何在真正选择文件之前保留上一文件信息?)

    文章前面研究ng-file-upload可能涉及指令: You can use ng-model or ngf-change instead of specifying function for ng ...

  3. websocket的理解及实例应用

    websocket协议是HTML5提出的一个新的规范,主要用于实现服务器及时推送信息给客户端的功能. websocket实现是基于HTTP协议的部分握手功能,但是websocket仅仅握手一次即可进行 ...

  4. eclipse导入源码

    1.window-----preferences 2.java---installed jres(点击不用展开)---选中使用的jar包-----editor 3.选中rt.jar ------sou ...

  5. 基于NodeJS进行前后端分离

    1.什么是前后端分离 传统的SPA模式:所有用到的展现数据都是后端通过异步接口(AJAX/JSONP)的方式提供的,前端只管展现. 从某种意义上来说,SPA确实做到了前后端分离,但这种方式存在两个问题 ...

  6. hdu4578 线段树 三次方,二次方,一次方的值

    Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, -, a n. The initial val ...

  7. Zabbix(二) : Zabbix Server端配置文件说明

    Zabbix Server端配置文件说明 # This is a configuration file for Zabbix Server process # To get more informat ...

  8. Cmder 软件中修改λ符号方法

    以前的版本 网上都有,我就不介绍了,  只介绍现在的 1. 打开Cmder软件安装位置 2. 打开vendor文件夹 profile.ps1文件 3. 找到第77行  Write-Host " ...

  9. ZOJ2286 Sum of Divisors 筛选式打表

    我想我是和Segmentation Fault有仇,我一直以为是空间开大的问题,然后一直减少空间,还是SF,谁让n没有给范围了,qwq. 教训:以后注意输入范围和开的空间大小. #include< ...

  10. 利用python生成交换机的VRF配置文件

    为了快速生成有规律的VRF,写了一个python脚本,可以快速生成如下的VRF配置. ip vpn-instance  vpn0ipv4-family  route-distinguisher 600 ...