codeforces 515d 拓扑排序/二分图

D. Drazil and Tiles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Drazil created a following problem about putting 1 × 2 tiles into an n × m grid:

"There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."

But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ".

Drazil found that the constraints for this task may be much larger than for the original task!

Can you solve this new problem?

Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2000).

The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

Output

If there is no solution or the solution is not unique, you should print the string "Not unique".

Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

Sample test(s)
input
3 3
...
.*.
...
output
Not unique
input
4 4
..**
*...
*.**
....
output
<>**
*^<>
*v**
<><>
input
2 4
*..*
....
output
*<>*
<><>
input
1 1
.
output
Not unique
input
1 1
*
output
*
Note

In the first case, there are indeed two solutions:

<>^
^*v
v<>

and

^<>
v*^
<>v

so the answer is "Not unique".



题目给出一个n*m的网格,用1*2的砖块将上面的空地都铺满,输出方案是否唯一,若唯一输出解决方案。

把所有的格子当成点,相邻格子间连接一条边,并统计每个格子的度数。将所有度数为1的点放入队列中,度数为1的一定只有一种铺法。铺完更新周围的点的度数,将度数为1的入队,循环直到队列为空。如果此时还没铺满,则方案不唯一。

如果铺完度数为1的,那么剩下的点都是度数>=2,所有的边构成了一个二分图,并且一定存在一个环其中相邻的边可以染成不同颜色。所以如果有解则一定有>=2个解


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

#define maxn 2005

int n,m;
char g[maxn][maxn];
int d[maxn][maxn];
int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF){
        for(int i = 1; i <= n; i++)
            scanf("%s", g[i]+1);

        memset(d, 0, sizeof(d));
        int tot = 0;

        queue<int> q;
        for(int i = 1; i<= n;i++)
        for(int j =1; j<= m; j++){
            int nx,ny;
            if(g[i][j] != '.') continue;
            tot++;
            for(int k = 0; k < 4; k++){
                nx = i+dir[k][0], ny = j+dir[k][1];
                if(g[nx][ny] == '.')
                    d[i][j]++;
            }
            if(d[i][j] == 1)
                q.push(i*(m+1)+j);
        }

        int cnt = 0;
        while(!q.empty()){
            int x = q.front()/(m+1), y = q.front()%(m+1);
            q.pop();

            for(int i = 0; i < 4; i++){
                int nx = dir[i][0]+x, ny = dir[i][1]+y;
                if(g[nx][ny] != '.') continue;
                if(i == 0){
                    g[nx][ny] = '^';
                    g[x][y] = 'v';
                }
                else if(i == 1){
                    g[nx][ny] = 'v';
                    g[x][y] = '^';
                }
                else if(i == 2){
                    g[nx][ny] = '>';
                    g[x][y] = '<';
                }
                else{
                    g[nx][ny] = '<';
                    g[x][y] = '>';
                }

                cnt+=2;
                for(int k = 0; k < 4; k++){
                int nnx = nx+dir[k][0], nny = ny+dir[k][1];
                if(g[nnx][nny]!='.') continue;
                d[nnx][nny]--;
                if(d[nnx][nny] == 1)
                    q.push(nnx*(m+1)+nny);
                }

            }
        }

        if(cnt!=tot)
            printf("Not unique\n");
        else{
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= m; j++)
                    printf("%c", g[i][j]);
                printf("\n");
            }
        }

    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值