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

ZOJ - 3780-Paint the Grid Again-(拓扑排序)的更多相关文章

  1. ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

    Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cel ...

  2. 【ZOJ - 3780】 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 ...

  3. ZOJ 3780 E - Paint the Grid Again 拓扑排序

    https://vjudge.net/problem/49919/origin 题意:给你n*n只出现O和X的字符阵.有两种操作,一种操作Ri将i行全变成X,一种操作Ci将i列全变成O,每个不同的操作 ...

  4. ZOJ 3780 - Paint the Grid Again - [模拟][第11届浙江省赛E题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Time Limit: 2 Seconds      Me ...

  5. ZOJ 3780 Paint the Grid Again

    拓扑排序.2014浙江省赛题. 先看行: 如果这行没有黑色,那么这个行操作肯定不操作. 如果这行全是黑色,那么看每一列,如果列上有白色,那么这一列连一条边到这一行,代表这一列画完才画那一行 如果不全是 ...

  6. zjuoj 3780 Paint the Grid Again

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 Paint the Grid Again Time Limit: 2 ...

  7. ZOJ 3781 Paint the Grid Reloaded(BFS+缩点思想)

    Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows ...

  8. ZOJ 3781 Paint the Grid Reloaded(BFS)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows an ...

  9. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

随机推荐

  1. JWT了解和实际使用

    一.JWT JSON Web Token(JWT)是目前最流行的跨域身份验证解决方案.虫虫今天给大家介绍JWT的原理和用法. 1.跨域身份验证 Internet服务无法与用户身份验证分开.一般过程如下 ...

  2. spark教程(四)-SparkContext 和 RDD 算子

    SparkContext SparkContext 是在 spark 库中定义的一个类,作为 spark 库的入口点: 它表示连接到 spark,在进行 spark 操作之前必须先创建一个 Spark ...

  3. @Transactional spring事务回滚相关

    还可以设置回滚点,看下面 /** * 用户登录接口 * * * 1明确事务方法前的命名规则 * 2保证事务方法执行的时间尽可能的短,不允许出现循环操作,不允许出现RPC等网络请求操作 * 3不允许所有 ...

  4. golang(5):struct & 链表 & 二叉树 & 接口

    struct : 结构体 // 1. 用来自定义复杂数据结构 // 2. struct里面可以包含多个字段(属性) // 3. struct类型可以定义方法,注意和函数的区分 // 4. struct ...

  5. luogu题解 UVA11536 【Smallest Sub-Array】最短set区间&滑动窗口

    题目链接: https://www.luogu.org/problemnew/show/UVA11536 题目大意: 给定一个\(N,M,K\),构造这样的数列: \(x[1]=1,x[2]=2,x[ ...

  6. performance面板使用,以及解决动画卡顿

    https://googlechrome.github.io/devtools-samples/jank//    官方案例 https://juejin.im/post/5b65105f518825 ...

  7. Nginx请求限制配置

    Nginx请求限制配置 请求限制可以通过两种方式来配置,分别是  连接频率限制和请求频率限制 首先我们要知道什么是http请求和连接,浏览器和服务端首先通过三次握手完成连接,然后发起请求,传输请求参数 ...

  8. javaee 自定义标签实战

    用过javaee标准标签库的里的标签应该都知道,标签的存在使得页面上的jsp脚本大大减少,甚至说没有了,大大提高了工作效率,使得页面的整洁性也有了很大的提高.下面我们就 模仿核心标签库中choose标 ...

  9. Linux排查磁盘空间顺序解决空间不足问题

    1 先查看整个磁盘的情况 df    -h                     查看整台服务器的硬盘使用情况 cd    /                       进入根目录 du   -s ...

  10. python中的定时任务

    使用threading模块中的Timer函数 from threading import Timer import time def execute_func(name, age, gender, h ...