Paint the Grid Again


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

题目链接:ZOJ 3780

比赛的时候真心没想出来,这题居然是用拓扑排序做,不得不膜拜一下。

做法:题目中最重要的条件就是每一行或每一列最多只能变换一次,考虑一个格子$(i,j)$,若它是'X'则说明它肯定是先刷$j$列后刷$i$行变换得到的;若为'O'则是先刷$i$行后刷$j$列的变换得到,那么$n*n$个格子可以得到$n*n$种顺序条件,最后再看能否存在一种拓扑序列同时符合这么多的条件,若符合则说明按照这个拓扑序列去刷可以刷出给定的字符样式。那么问题就成了拓扑排序问题了。最后题目要求字典序最小的方案,由于列变换字符'C'的字典序比行变换'R'的字典序小,因此把列号设为$1$~$n$,行号设为$n+1$~$2n$,而且要求变换的行列坐标也要最小,因此用最小堆的优先队列来代替普通队列进行拓扑排序,一开始的入度为0的点实际上是不用刷的,因为一开始的点是没有行、列顺序要求的。比如单一个点'X',拓扑序为第1列->第1行,但是显然刷第1列这个操作是多余的。

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 510;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
} E[N * N];
char s[N][N];
int head[N << 1], tot;
int deg[N << 1], no_use[N << 1];
vector<int>ans; void init()
{
CLR(head, -1);
tot = 0;
CLR(deg, 0);
CLR(no_use, 0);
ans.clear();
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
int Top_sort(int n)
{
int i, u;
priority_queue<int, vector<int>, greater<int> >Q;
for (i = 1; i <= n; ++i)
{
if (!deg[i])
{
Q.push(i);
no_use[i] = 1;
}
}
while (!Q.empty())
{
u = Q.top();
Q.pop();
ans.push_back(u);
for (i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (!--deg[v])
Q.push(v);
}
}
return (int)ans.size() == n;
}
int main(void)
{
int tcase, n, i, j;
scanf("%d", &tcase);
while (tcase--)
{
init();
scanf("%d", &n);
for (i = 1; i <= n; ++i)
scanf("%s", s[i] + 1);
for (i = 1; i <= n; ++i) //N*N
{
for (j = 1; j <= n; ++j)
{
if (s[i][j] == 'X')
add(j, i + n), ++deg[i + n];
else
add(i + n, j), ++deg[j];
}
}
if (!Top_sort(n << 1))
puts("No solution");
else
{
int sz = ans.size();
for (i = 0; i < sz; ++i)
{
int x = ans[i];
if (no_use[x])
continue;
printf("%c%d%s", x <= n ? 'C' : 'R', x <= n ? x : x - n, i == sz - 1 ? "\n" : " ");
}
}
}
return 0;
}

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

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

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

  2. ZOJ 3780 Paint the Grid Again

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

  3. zjuoj 3780 Paint the Grid Again

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

  4. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  5. 八数码问题+路径寻找问题+bfs(隐式图的判重操作)

    Δ路径寻找问题可以归结为隐式图的遍历,它的任务是找到一条凑够初始状态到终止问题的最优路径, 而不是像回溯法那样找到一个符合某些要求的解. 八数码问题就是路径查找问题背景下的经典训练题目. 程序框架 p ...

  6. uva658(最短路径+隐式图+状态压缩)

    题目连接(vj):https://vjudge.net/problem/UVA-658 题意:补丁在修正 bug 时,有时也会引入新的 bug.假定有 n(n≤20)个潜在 bug 和 m(m≤100 ...

  7. nyoj 21--三个水杯(隐式图bfs)

    三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识 ...

  8. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  9. 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 ...

随机推荐

  1. 前端三大框架 Vue.js、AngularJS、React 的区别

    Vue.js Vue.js 是一种构建数据驱动的Web界面的渐进式框架,Vue.js 采用自底向上增量开发的设计. Vue.js 轻量高效,数据双向绑定(响应式数据绑定), 它会自动响应数据的变化情况 ...

  2. SEO人士一定要了解的搜索引擎惩罚原则

       SEO人士一定要了解的搜索引擎惩罚原则 SEO 的人一般都知道SEO分为白帽,黑帽,甚至还有灰帽.简单说,如果你熟读过谷歌网站质量指南,就可以了解,符合搜索引擎质量规范并且符合用户体验的SEO ...

  3. sublime package control以及常用插件

    一.package Control安装 1.sublime 3 import urllib.request,os; pf = 'Package Control.sublime-package'; ip ...

  4. Ubuntu下安装pip3和Python的第三方库

    一.Ubuntu原有环境说明 无论是在服务器上面还是在我们自己的电脑上面,当我们成功安装了Ubuntu系统之后,系统一般情况下会自带Python2.x和Python3.x环境.比如我在自己的阿里云服务 ...

  5. Vscdoe技巧1

    vscdoe常用快捷键 主命令框 F1 或 Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以输入任何命令,例如: 按一下 Backspace 会进入到 Ctrl+P 模式 在 Ctrl ...

  6. Oracle Analyze

    Analyze使用场景 之前很多次都说到,对表的索引等信息进行了增删改之后,需要对表进行analyze更新统计信息,才能使数据库做出最好的执行计划,没有注意到,即使是一张很小的空表,如果进行了字段的增 ...

  7. Django之视图和URL配置

    1.在创建项目时,Django会自动创建URL配置,在urls.py文件中 文件的默认内容如下所示: """mysite URL Configuration The ur ...

  8. Linux监控二之cacti简单安装部署

    目录 cacti简单部署    1 环境依赖包部署    1 1.    cacti中文版0.8e搭建    2 2.    cacti安装向导 url:http://192.168.200.243/ ...

  9. CSS-标准盒模型 & 怪异盒模型

    CSS中Box model分类 CSS中Box model是分为两种:: W3C标准 和 IE标准盒子模型. 大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 怪异模式是“ ...

  10. Scrapy用pipelines把字典保存为csv格式

    import csv class MyProjectPipeline(object): # 保存为csv格式 def __init__(self): # 打开文件,指定方式为写,利用第3个参数把csv ...