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

如果某一行存在x,则这一行必然会被横着涂一遍,同时上一次可以为竖着涂这一列;如果是o也可以得到相似的结论,所以这样可以建立起一个有向图,用拓扑排序判断即可。

BFS拓扑排序

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=505;
vector<int>G[N<<1];
int n;
char a[N];
queue<int>que;
bool del[N<<1],vis[N<<1];
int in[N<<1];
bool bfs(){
memset(del,false,sizeof(del));
memset(vis,false,sizeof(vis));
priority_queue<int,vector<int>,greater<int> >q;
for(int i=1;i<=(n<<1);i++) if(in[i]==0)
q.push(i),vis[i]=true;
if(q.size()==0) return false;
for (int i=1;i<=(n<<1);i++){
int cur = q.top();
q.pop();
if(del[cur]) return false;
del[cur]=true;que.push(cur);
for (int j=0;j<G[cur].size();j++){
int nxt=G[cur][j];
if(del[nxt]) return false;
in[nxt]--;
if(in[nxt]==0)
q.push(nxt);
}
}
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(in,0,sizeof(in));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[j].push_back(i+n);
in[i+n]++;
}
else{
G[i+n].push_back(j);
in[j]++;
}
}
}
if(!bfs()||que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
if(vis[x]) continue;
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(!que.empty()) printf(" ");
}
puts("");
}
return 0;
}

dfs

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 1000000000LL
#define mod 1000000007
using namespace std;
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=515;
vector<int>G[N<<1];
int id[N<<1],n;
char a[N];
int vis[N<<1];
queue<int>que; bool dfs(int u)
{
vis[u]=-1;
sort(G[u].begin(),G[u].end());
for(int i=0; i<(int)G[u].size(); i++){
int v=G[u][i];
if(vis[v]<0) return false;
if(!vis[v]&&!dfs(v)) return false;
}
vis[u]=1;
if(id[u]!=n) que.push(u);
return true;
}
int main()
{
int T=read();
while(T--){
while(!que.empty()) que.pop();
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
n=read();
for(int i=1; i<=(n<<1); i++) G[i].clear();
for(int i=1; i<=n; i++){
scanf("%s",a+1);
for(int j=1; j<=n; j++){
if(a[j]=='X'){
G[i+n].push_back(j); //横着,大于n的为横着的标号
id[j]++;
}
else{
G[j].push_back(i+n); //竖着,小于n为竖着的标号
id[i+n]++;
}
}
}
for(int i=1; i<=(n<<1); i++) if(!id[i]&&!vis[i]) if(!dfs(i)){
//无前驱的节点可以作为开始节点
while(!que.empty()) que.pop();
break;
}
if(que.empty()){
puts("No solution");
continue;
}
while(!que.empty()){
int x=que.front();
que.pop();
printf("%c%d",x>n?'R':'C',x>n?x-n:x);
if(que.empty()) puts("");
else printf(" ");
}
}
return 0;
}

【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 E - Paint the Grid Again 拓扑排序

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

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

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

  4. ZOJ 3780 Paint the Grid Again

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

  5. zjuoj 3780 Paint the Grid Again

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

  6. ZOJ - 3780-Paint the Grid Again-(拓扑排序)

    Description Leo has a grid with N × N cells. He wants to paint each cell with a specific color (eith ...

  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. 各个nginx conf的虚拟主机的配置

    server { listen 80; server_name t-cl.orangevip.com; rewrite ^(.*)$ https://$host$1 permanent;} serve ...

  2. PostgreSQL 9.6.2版本在centOS下的安装和配置

    1.如果有用yum安装过旧版,卸载掉: yum remove postgresql* 2.更新一下yum: sudo yum update 3.去 官网 找到 适合你系统 的资源的下载地址,然后使用w ...

  3. 使用VS2008,VS2010编译64位的应用程序

    要编译生成64位的应用程序,就必须把vs2008,或vs2010的配置管理器设置为x64. 如果你的配置管理器那里没有x64这个选项,那么是你在安装vs时可能没有安装这个组件.你不用卸载vs,只需打开 ...

  4. 51nod1099 任务执行顺序

    #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int r,o; int s ...

  5. 洛谷 P2824 [HEOI2016/TJOI2016]排序 (线段树合并)

    (另外:题解中有一种思路很高妙而且看上去可以适用一些其他情况的离线方法) 线段树合并&复杂度的简单说明:https://blog.csdn.net/zawedx/article/details ...

  6. 嵌套查询--------关联一对多关系----------collection

    参考来源:   http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...

  7. java单元测试注释执行顺序

    JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before 在测试方法运行之前运行 @Test 测试方法 @Afte ...

  8. React全家桶之一 react-router之高级

    使用query获取URL中的参数 //引入相关的依赖 const Page = props => <div> <h1>{props.location.query.mess ...

  9. ScrollView属性

    1.文本内容过长,一个屏幕显示不下,这时候就把显示文本的 TextView包裹在ScrollView里面,可以做到滚动下滑查看的功能 2.隐藏滚动条 标签属性设置android:scrollbars= ...

  10. greenplum4.3.8.2安装

    GREENPLUM总体结构:   数据库由Master Severs和Segment Severs通过Interconnect互联组成. Master主机负责:建立与客户端的连接和管理:SQL的解析并 ...