Codeforces 516B Drazil and Tiles【类拓扑排序】好题!

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

Examples
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的<>或者是^v,使得铺满所有的点。

如果解不唯一,或者是无解,输出Not unique.


思路:


1、思路来源:http://blog.csdn.net/highmath_final/article/details/47358981


2、如果图小一点的话,跑二分图匹配即可~没想到这种题还可以用类拓扑排序的思维方式来解,真的蛮不错的一个模型。

我们这里定义一个顶点的度就是其周围点的个数。

那么对应度为1的点是一定确定了放置这个点的方式的,对应这个点的四周四个位子中,此时一定只有一个点,那么对应将此处和那个位子直接放置这个1*2的形状即可。

(假设这个点在这个度为1的点的下边,那么直接放置^v即可。)

然后处理完这个点之后,对应接下来进行拆边的操作,如果再发现了度为1的点,入队.....

一直处理下去这样的类拓扑排序的过程即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node
{
    int x,y;
} now,nex;
char a[2002][2002];
int degree[2002][2002];
int fx[4]= {0,0,1,-1};
int fy[4]= {1,-1,0,0};
int n,m;
void Getdegree()
{
    memset(degree,0,sizeof(degree));
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            for(int k=0; k<4; k++)
            {
                int xx=i+fx[k];
                int yy=j+fy[k];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                {
                    if(a[xx][yy]=='.')
                    {
                        degree[i][j]++;
                    }
                }
            }
        }
    }
}
void Top_Sort()
{
    queue<node >s;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(degree[i][j]==1&&a[i][j]=='.')
            {
                now.x=i;
                now.y=j;
                s.push(now);
            }
        }
    }
    while(!s.empty())
    {
        now=s.front();
        s.pop();
        int x=now.x;
        int y=now.y;
        if(a[x+1][y]=='.')
        {
            a[x][y]='^';
            a[x+1][y]='v';
            for(int z=0; z<4; z++)
            {
                int xx=x+1+fx[z];
                int yy=y+fy[z];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                if(degree[xx][yy]--)
                {
                    if(degree[xx][yy]==1&&a[xx][yy]=='.')
                    {
                        now.x=xx;
                        now.y=yy;
                        s.push(now);
                    }
                }
            }
        }
        else if(a[x-1][y]=='.')
        {
            a[x][y]='v';
            a[x-1][y]='^';
            for(int z=0; z<4; z++)
            {
                int xx=x-1+fx[z];
                int yy=y+fy[z];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                if(degree[xx][yy]--)
                {
                    if(degree[xx][yy]==1&&a[xx][yy]=='.')
                    {
                        now.x=xx;
                        now.y=yy;
                        s.push(now);
                    }
                }
            }
        }
        else if(a[x][y+1]=='.')
        {
            a[x][y]='<';
            a[x][y+1]='>';
            for(int z=0; z<4; z++)
            {
                int xx=x+fx[z];
                int yy=y+1+fy[z];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                if(degree[xx][yy]--)
                {
                    if(degree[xx][yy]==1&&a[xx][yy]=='.')
                    {
                        now.x=xx;
                        now.y=yy;
                        s.push(now);
                    }
                }
            }
        }
        else if(a[x][y-1]=='.')
        {
            a[x][y]='>';
            a[x][y-1]='<';
            for(int z=0; z<4; z++)
            {
                int xx=x+fx[z];
                int yy=y-1+fy[z];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                if(degree[xx][yy]--)
                {
                    if(degree[xx][yy]==1&&a[xx][yy]=='.')
                    {
                        now.x=xx;
                        now.y=yy;
                        s.push(now);
                    }
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j]=='.')
            {
                printf("Not unique\n");
                return ;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%s\n",a[i]);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
        }
        Getdegree();
        Top_Sort();
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值