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. 微软云Linux服务器 Mysql、tomcat远程连接错误解决办法

    在微软云linux服务器成功配置好mysql.tomcat,通过外部链接却发现一直错误.Mysql 一直提示错误代码2003, tomcat连接一直提示EOF. 反复检查配置都无问题,最后得知是微软云 ...

  2. 关于sping quartz定时执行理解与思考

    转载请注明原创出处,谢谢! 一直以为自己理解spring quartz,忽然最近几天发现自己理解的不对,在4月18号的时候,我执行了一个spring quartz的计划如下: 1 0 0 */3 * ...

  3. FastDFS安装步骤

    FastDFS是用c语言编写的一款开源的分布式文件系统,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传.下 ...

  4. idea启动tomcat报错:Error during artifact deployment. See server log for details.

    出现这种情况的原因老夫猜想是改变了artifact然而tomcat的配置中的artifact没有重新配就会出现这种报错 打开tomcat配置 删除原来的artifact 新添加artifact 保存 ...

  5. 【京东详情页】——原生js爬坑之二级菜单

    一.引言 做京东详情页仿写的时候,要用原生js实现顶部菜单的二级菜单显示与隐藏事件的触发. 过程中遇到了一个坑,在这里与大家分享.要实现的效果如下: 二.坑 谁触发事件?显示.隐藏二级菜单       ...

  6. C++11获取线程的返回值

    C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会 ...

  7. 利用百度地图WEB服务APIGeoCoding API批量地址解析

    Geocoding API包括地址解析和逆地址解析功能: 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息,例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.3 ...

  8. IE兼容

    这个基本知识http://www.cnblogs.com/yoosou/archive/2012/07/27/2612443.html 参考: http://www.cnblogs.com/cocow ...

  9. strut2-学习笔记(二)

     Struts2学习笔记(二) 1. 自定义结果视图的类型(结果视图类型的应用) CAPTCHA图像(随机验证码图像) 实现步骤: (1)编写一个类实现com.opensymphony.xwork ...

  10. 有趣的flash例子

    仓鼠 <object type="application/x-shockwave-flash" data="http://cdn.abowman.com/widge ...