ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 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

xxxo先涂行,在涂列

x

o

x

x 先涂列在涂行。 因此可以构造一个图,进行topsort。如果要col先可以给col 小的标号,每次用无入度点的最小点。

记住,刚开始的无入度点可以不涂。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>

#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define DWN(i,a,b) for(int i=a;i>=b;--i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
#define PII pair<int,int>
#define PB push_back
#define LL long long

using namespace std;

const int mm = 508;

class Node
{
public:
    int v,next;
}e[mm*mm*2];

int head[mm+mm],edge,id[mm+mm];
bool first[mm+mm];
vector<int>ans;
priority_queue<int,vector<int>,greater<int> > Q;

void init()
{
    clr(head,-1); edge = 0;
    clr(id,0);
    clr(first,0);
}

void add(int u,int v)
{
    e[edge].v = v; e[edge].next = head[u]; head[u] = edge++;
    ++id[v];
}
int N;
char s[mm][mm];
bool topsort(int n)
{
    for(int i=0;i<n;++i)
        if(id[i] == 0)
            Q.push(i),first[i] = 1;
    int u,v;
    while(!Q.empty())
    {
        u = Q.top();Q.pop();
        ans.PB(u);
        for(int i=head[u];~i;i=e[i].next)
        {
            v=e[i].v;
            --id[ v ];
            if(id[v] == 0)
                Q.push(v);
        }
    }
    for(int i=0;i<n;++i)
        if(id[i])
        return false;
    return true;
}
int main()
{
    int cas;
    while(~scanf("%d",&cas))
    {
        while(cas--)
        {
            scanf("%d",&N);
            init();
            for(int i=0;i<N;++i)
                scanf("%s",s[i]);
            for(int i=0;i<N;++i)
                for(int j=0;j<N;++j)
                    if(s[i][j] == ‘X‘) //x 先列再行 o 先行再列
                        add(j,i+N);
                    else add(i+N,j);
            ans.clear();
            if(topsort(N+N))
            {
                int len = ans.size();
                for(int i=0;i<len;++i)
                    if(first[ ans[i] ])
                        continue;
                    else if(ans[i]<N)
                    printf("C%d%c",ans[i]+1,i==len-1?‘\n‘:‘ ‘);
                    else
                    printf("R%d%c",ans[i]-N+1,i==len-1?‘\n‘:‘ ‘);
            }
            else printf("No solution\n");

        }
    }
    return 0;
}

ZOJ 3780 Paint the Grid Again(topsort)

时间: 2024-11-06 10:36:46

ZOJ 3780 Paint the Grid Again(topsort)的相关文章

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

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

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

zoj 3781 Paint the Grid Reloaded (比较隐含的最短路)

Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are

zjuoj 3780 Paint the Grid Again

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 h

ZOJ 3781 Paint the Grid Reloaded(BFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in

ZOJ - 3781 Paint the Grid Reloaded 题解

题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X->O或O->X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1.每次操作都将本身所在的连通块与和自己相邻的不同颜色的连通块变成同一种颜色,也就是变成一个连通块了,那么要使n次操作后全部变成一样的颜色,也就是从某点出发到达其余所有点. 2.因此dfs把连通块缩成点,然后相邻的连通块之间建边,枚举以每个点为根的情况,bfs求出每种情况的深度,取最小的即为答案. 反思: 1.h

2014 Super Training #4 D Paint the Grid Again --模拟

原题:ZOJ 3780 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3780 刚开始看到还以为是搜索题,没思路就跳过了.结果后来发现就是一个简单的模拟啊,因为每行每列都只能消去一次,直接慢慢消去就好了,因为按字典序从小到大,那就按行从大到小,列从大到小的顺序来消就可以了,消完了标记一下,把那行或者那列的元素都赋为一个特殊的字符'*'即可. 还是应该多思考啊,不要被题目吓到了.探寻题目的本质才能更好的解题. 代码: #i

Paint the Grid Reloaded(缩点,DFS+BFS)

Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are in the same color, or there exists a cell C connected to both A and B. Leo