码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3780 Paint the Grid Again (拓扑排序)

时间:2014-05-07 06:56:10      阅读:470      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   color   int   

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


题意:
将一面墙刷成给出的图形,你可以将一排刷成X或者将一列刷成O,问字典序最小的方案是什么。

思路:
拓扑排序的简单应用,好久没见拓扑排序的题了,竟然都没看出来。衰~
将每行每列看做点,根据每个点为X或者O来建图,然后dfs或者用优先队列就能找到字典序最小的方案了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#define maxn 1005
using namespace std;

int n,m,ans,tot,flag,cnt;
bool vis[maxn];
int app[maxn],num[maxn],res[maxn];
char mp[maxn][maxn];
vector<int>v[maxn];

void build()
{
    int i,j,t;
    tot=0;
    memset(app,0,sizeof(app));
    memset(num,0,sizeof(num));
    for(i=1; i<=n; i++)
    {
        flag=0;
        for(j=1; j<=n; j++)
        {
            if(mp[i][j]==‘X‘)
            {
                flag=1;
                break ;
            }
        }
        if(flag)
        {
            tot++,app[n+i]=1;
        }
    }
    for(j=1; j<=n; j++)
    {
        flag=0;
        for(i=1; i<=n; i++)
        {
            if(mp[i][j]==‘O‘)
            {
                flag=1;
                break ;
            }
        }
        if(flag)
        {
            tot++,app[j]=1;
        }
    }
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(mp[i][j]==‘X‘)
            {
                if(app[j])
                {
                    num[n+i]++;
                    v[j].push_back(n+i);
                }
            }
            else
            {
                if(app[n+i])
                {
                    num[j]++;
                    v[n+i].push_back(j);
                }
            }
        }
    }
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1; i<=n; i++)
        {
            scanf("%s",mp[i]+1);
            v[i].clear();v[i+n].clear();
        }
        build();
        priority_queue<int, vector<int>, greater<int> > q;
        memset(vis,0,sizeof(vis));
        for(i=1;i<=tot;i++)
        {
            for(j=1;j<=n+n;j++)
            {
                if(app[j]&&num[j]==0&&!vis[j]) vis[j]=1,q.push(j);
            }
            if(q.empty()) break ;
            int x=q.top();
            res[i]=x;
            q.pop();
            for(j=0;j<v[x].size();j++)
            {
                num[v[x][j]]--;
            }
        }
        if(i<tot) printf("No solution\n");
        else
        {
            for(i=1;i<=tot;i++)
            {
                if(i>1) printf(" ");
                if(res[i]<=n) printf("C%d",res[i]);
                else printf("R%d",res[i]-n);
            }
            printf("\n");
        }
    }
    return 0;
}






zoj 3780 Paint the Grid Again (拓扑排序),布布扣,bubuko.com

zoj 3780 Paint the Grid Again (拓扑排序)

标签:style   blog   class   code   color   int   

原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/25081747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!