Drazil and Tiles - CODEFORCES 515D 贪心

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的瓷砖去填满空位置,如果只有一种方法,输出该方法。如果无解或有2种以上的方法输出Not unique.

解题思路:先遍历找出节点度数为1的节点,度数表示节点周围'.'的个数,放入队列中,然后填入瓷砖,并更新两点周围所有节点的度数,将度数为1的节点继续放入队列中。最后扫一遍看是否还有空位置。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define maxn 2000+10

int m,n;
char s[maxn][maxn];
int degree[maxn][maxn];
struct Node{
    int x,y;
    Node(){}
    Node(int a,int b){
        x =a;
        y = b;
    }
};
queue<Node> que;
int dx[] = {0,0,1,-1};   // right left down up
int dy[] = {1,-1,0,0};

int Count(int x,int y){
    int cnt = 0;
    for(int i=0;i<4;i++){
        int xx = x+dx[i], yy=y+dy[i];
        if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.') cnt++;
    }
    return cnt;
}

void update(int x,int y){
    for(int i=0;i<4;i++){
        int xx = x+dx[i], yy=y+dy[i];
        if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.'){
            degree[xx][yy] = Count(xx,yy);
            if(degree[xx][yy]==1) que.push(Node(xx,yy));
        }
    }
}

void solve(){
    Node node;
    memset(degree,0,sizeof(degree));
    while(!que.empty()) que.pop();
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='*') continue;
            degree[i][j] = Count(i,j);
            if(degree[i][j]==1){
                node.x = i, node.y = j;
                que.push(node);
            }
        }
    }
 /*   for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            printf("%d ",degree[i][j]);
        }
        printf("\n");
    }*/
    int x,y;
    while(!que.empty()){
        node = que.front();
        que.pop();
        x = node.x, y = node.y;
        for(int i=0;i<4;i++){
            int xx = x+dx[i], yy=y+dy[i];
            if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy]=='.'){
                if(i==0) s[x][y]='<',s[xx][yy]='>';
                else if(i==1) s[xx][yy]='<',s[x][y]='>';
                else if(i==2) s[x][y]='^',s[xx][yy]='v';
                else if(i==3) s[xx][yy]='^',s[x][y]='v';
                update(x,y);
                update(xx,yy);
                break;
            }
        }
    }
}

void print(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            printf("%c",s[i][j]);
        }
        printf("\n");
    }
}

bool check(){
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(s[i][j]=='.') return false;
        }
    }
    return true;
}

int main(){
    scanf("%d %d\n",&n,&m);
    for(int i=0;i<n;i++){
        gets(s[i]);
    }
    solve();
    if(check()) print();
    else printf("Not unique\n");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值