http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780

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

Author: YU, Xiaoyao
Source: The 11th Zhejiang Provincial Collegiate Programming Contest

分析;

给定n*n的矩阵

有2个操作:

1、把一行变成X

2、把一列变成O

限制:每行(每列)只能变一次

给定结果图,开始时图无O,X,问最小操作步数(且字典序最小)

思路:

对于(i,j)这个格子,若现在涂的是 O,则去掉O这排,(让这排都变成X即可)可以直接认为(i,j)是X

所以当某排的X攒满n个时,就可以去掉这排X

直接模拟即可

先把所有 全为O或全为X的 行和列预处理出来,放到一个栈里

因为字典序最小,所以先处理列再处理行,第i列 用i+n表示, 第i行用i表示
然后给栈排个序,这样就得到处理当前情况的顺序, 入个队列,然后一个个去掉就可以了。

AC代码:

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define N 1005
vector<int>ans;
char mp[N][N];
int n, h[N], l[N];
int yes[N];
int Stack[N], Top; void init(){
ans.clear();
memset(yes, , sizeof yes);
memset(h, , sizeof h);
memset(l, , sizeof l);
Top = ;
}
bool cmp(int a,int b){return a>b;}
//0-n-1 表示列 n-2n-1 表示行
void work(){
sort(Stack, Stack+Top, cmp);
queue<int>q;
int i, j;
for(int i = ; i < Top; i++){
q.push(Stack[i]), ans.push_back(Stack[i]); yes[Stack[i]]=-;
}
Top = ;
while(!q.empty()){
int u = q.front(); q.pop();
Top = ;
if(u<n)
for(j = ; j < n; j++)
{
mp[j][u] = 'X';
h[j]++;
if(yes[j+n]!=- && h[j]==n)Stack[Top++] = j+n;
}
else {
u-=n;
for(j = ; j < n; j++)
{
mp[u][j] = 'O';
l[j]++;
if(yes[j]!=- && l[j]==n)Stack[Top++] = j;
}
}
sort(Stack, Stack+Top, cmp);
for(i = ; i < Top; i++)q.push(Stack[i]), yes[Stack[i]] = -, ans.push_back(Stack[i]);
}
for(int i = ; i < *n; i++)if(yes[i]==){puts("No solution");return;}
for(int i = ans.size()-; i>=; i--){
int u = ans[i];
if(u>=n)printf("R"), u-=n;
else printf("C");
printf("%d",u+);
i ? printf(" ") : puts("");
}
}
int main(){
int T;scanf("%d",&T);
int i, j;
while(T--){
scanf("%d",&n);
init();
for(i=;i<n;i++)scanf("%s",mp[i]);
for(i=;i<n;i++)
{
for(j = ; j<n; j++)if(mp[i][j]=='X')h[i]++;
if(h[i]==n) Stack[Top++] = i+n;
else if(h[i]==) yes[i+n] = -;
}
for(i=;i<n;i++)
{
for(j = ; j<n; j++)if(mp[j][i]=='O')l[i]++;
if(l[i]==n) Stack[Top++] = i;
else if(l[i]==) yes[i] = -;
}
if(Top==){puts("No solution");continue;}
work();
}
return ;
}
/*
99
1
O
3
OOO
OOO
OOO 2
XX
OX
2
XO
OX */

 

zjuoj 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(隐式图拓扑排序)

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

  3. zjuoj 3773 Paint the Grid

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

  4. ZOJ 3780 Paint the Grid Again

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

  5. Paint the Grid Again ZOJ - 3780 拓扑

    Paint the Grid Again Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu [ ...

  6. 【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 ...

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

  8. Paint the Grid Reloaded ZOJ - 3781 图论变形

    Paint the Grid Reloaded Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %ll ...

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

随机推荐

  1. 北京电子科技学院(BESTI)实验报告1

    北京电子科技学院(BESTI)实验报告1 课程: 信息安全系统设计基础 班级:1452.1453 姓名:(按贡献大小排名)郑凯杰 .周恩德 学号:(按贡献大小排名)20145314 .20145217 ...

  2. HBase如何选取split point

    hbase region split操作的一些细节,具体split步骤很多文档都有说明,本文主要关注regionserver如何选取split point 首先推荐web ui查看hbase regi ...

  3. Educational Codeforces Round 12 E Beautiful Subarrays

    先转换成异或前缀和,变成询问两个数异或≥k的方案数. 分治然后Trie树即可. #include<cstdio> #include<algorithm> #define N 1 ...

  4. Eclipse 报java.lang.OutOfMemoryError: PermGen space错

    这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader时就会被放到PermGen space中,它和存放类实例(Instance)的Heap区域不同,GC(Garbage C ...

  5. JS运算符

    JS运算符: 使用的运算符的时候不需要声明变量,运算符非变量:1.算术运算符 + - * / % (%为取余数运算符) (自增运算符++) (自减运算符 --) + 运算符作用:1.数值相加 2.字符 ...

  6. Wordpress 所有hoor列表

    d 在插件加载的时候执行 wp_footer 加载页面底部时执行 admin_menu 加载管理员菜单时执行 wp_head 在body标签的开始添加html内容 after_setup_theme ...

  7. dedecms有条件sql注入(x0day)

    https://www.t00ls.net/thread-35569-1-1.html http://localhost/dedecms/plus/advancedsearch.php?mid=1&a ...

  8. Learn ZYNC (5)

    今天为了熟悉axiLite的自定义ip核设计, 把LED和SW的往AXI总线输入输出定义在一个ip核中, BD设计如下: ip核顶层文件(增加了LED_Out和SW_In的定义)mygpio_v1.0 ...

  9. Java中的递归运算

    Java中的递归运算是一种在自己的方法内部调用自己的方法 递归的设计思想是:把一个复杂的问题,分解为若干个等同的子问题,重复执行,直到之问题能够简单到直接求解,这样复杂的问题就得以解决. 递归运算有两 ...

  10. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...