Drazil and Tiles - CodeForces 513 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
*

题意:问是否只有一种将空白部分用1*2的砖铺满的方法,如果只有一种,输出铺砖的结果。

思路:将周围有三个不能铺砖的点的空白点放入队列,那么这个点的铺砖方式就确定了,同时重新搜索周围的点,直到队列为空。最后若能铺满,则答案唯一。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
int T,t,n,m,sum;
char s[2010][2010];
bool vis[2010][2010];
bool flag;
queue<int> qu;
void find(int i,int j)
{
    int k;
    k=vis[i-1][j]+vis[i+1][j]+vis[i][j-1]+vis[i][j+1];
         if(k==4)
         {
             flag=true;
             return;
         }
         else if(k==3)
           qu.push(i*3000+j);
}
void solve2(int i,int j)
{
    if(vis[i-1][j]==0)
       find(i-1,j);
    if(vis[i+1][j]==0)
       find(i+1,j);
    if(vis[i][j-1]==0)
       find(i,j-1);
    if(vis[i][j+1]==0)
       find(i,j+1);
}
void debug()
{
    for(int i=0;i<=n+1;i++)
           printf("%s\n",s[i]);
}
void solve()
{
    int i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
       scanf("%s",s[i]+1);
    for(i=0;i<=n+1;i++)
       s[i][0]=s[i][m+1]='*';
    for(i=0;i<=m+1;i++)
       s[0][i]=s[n+1][i]='*';
    for(i=0;i<=n+1;i++)
       for(j=0;j<=m+1;j++)
          if(s[i][j]=='*')
          {
              vis[i][j]=1;
              sum++;
          }
    sum=sum-n*2-m*2-4;
    if((n*m-sum)&1)
    {
        flag=true;
        return;
    }
    for(i=1;i<=n;i++)
      for(j=1;j<=m;j++)
      if(vis[i][j]==0)
      {
         k=vis[i-1][j]+vis[i+1][j]+vis[i][j-1]+vis[i][j+1];
         if(k==4)
         {
             flag=true;
             return;
         }
         else if(k==3)
           qu.push(i*3000+j);
      }
    while(!qu.empty())
    {
        if(flag)
          return;
        k=qu.front();
        qu.pop();
        i=k/3000;j=k%3000;
        if(vis[i][j])
          continue;
        if(vis[i-1][j]==0)
        {
            vis[i][j]=vis[i-1][j]=1;
            s[i][j]='v';s[i-1][j]='^';
            solve2(i,j);solve2(i-1,j);
        }
        else if(vis[i+1][j]==0)
        {
            vis[i][j]=vis[i+1][j]=1;
            s[i][j]='^';s[i+1][j]='v';
            solve2(i,j);solve2(i+1,j);
        }
        else if(vis[i][j-1]==0)
        {
            vis[i][j]=vis[i][j-1]=1;
            s[i][j]='>';s[i][j-1]='<';
            solve2(i,j);solve2(i,j-1);
        }
        else if(vis[i][j+1]==0)
        {
            vis[i][j]=vis[i][j+1]=1;
            s[i][j]='<';s[i][j+1]='>';
            solve2(i,j);solve2(i,j+1);
        }
        sum+=2;
    }
    if(sum!=n*m)
      flag=true;
}
int main()
{
    int i,j,k;
    flag=false;
    solve();
    if(flag)
      printf("Not unique\n");
    else
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
                printf("%c",s[i][j]);
            printf("\n");
        }

    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值