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

D. Drazil and Tiles (CF 515D bfs搜索)

时间:2015-03-28 17:22:34      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:d. drazil and tiles   cf 515d   bfs搜索   

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的砖去把它填满,可以横向(‘<’,‘>‘)填和竖向(‘^‘,‘v‘)填。找出基本元素块,(i,j)和它相邻的四个点看成一个基本元素块,如果(i,j)周围的‘.’只有一个那么这个(i,j)处的填法就是固定的,填完(i,j)后看它周围是否有其他点因为填完(i,j)后填法变的唯一,有就入队,  就这样一步一步找到固定填法的(i,j),更新周围的点。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 2005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

typedef pair<int,int>pa;
int a[maxn][maxn];  //1--'.' ; 2--'^' ; 3--'>' ; 4--'v' ; 5--'<'
char mp[maxn][maxn];
int n,m;

bool Isok(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m)
        return true;
    return false;
}

int degree(int x,int y,int &dir,int &xx,int &yy)//(xx,yy)记录下和(x,y)配对的点的位置
{
    int s=0;
    if (a[x-1][y]==1) {s++;dir=4;xx=x-1;yy=y;}  //上
    if (a[x][y-1]==1) {s++;dir=3;xx=x;yy=y-1;}  //左
    if (a[x+1][y]==1) {s++;dir=2;xx=x+1,yy=y;}  //下
    if (a[x][y+1]==1) {s++;dir=5;xx=x;yy=y+1;}  //右
    return s;
}

void bfs()
{
    int i,j;
    queue<pa>Q;
    while (!Q.empty()) Q.pop();
    pa st;
    FRE(i,1,n)
    {
        FRE(j,1,m)
        {
            int dir,xx,yy;
            if (a[i][j]==1&°ree(i,j,dir,xx,yy)==1)
            {
                a[i][j]=dir;
                a[xx][yy]=dir%4+2;
                Q.push(make_pair(xx,yy));
            }
        }
    }
    while (!Q.empty())
    {
        st=Q.front(); Q.pop();
        FRE(i,st.first-1,st.first+1)
        {
            FRE(j,st.second-1,st.second+1)
            {
                int dir,xx,yy;
                if (Isok(i,j)&&a[i][j]==1&°ree(i,j,dir,xx,yy)==1)
                {
                    a[i][j]=dir;
                    a[xx][yy]=dir%4+2;
                    Q.push(make_pair(xx,yy));
                }
            }
        }
    }
}

int main()
{
    int i,j;
    while (~sff(n,m))
    {
        mem(a,0);
        FRE(i,1,n)
        {
            scanf("%s",mp[i]+1);
            FRE(j,1,m)
            {
                if (mp[i][j]=='.') a[i][j]=1;
                else a[i][j]=0;
            }
        }
        bfs();
        bool Unique=true;
        FRE(i,1,n)  //最后如果还有'.'那就要输出'Not unique'
        {
            FRE(j,1,m)
            {
                if (a[i][j]==1)
                {
                    Unique=false;
                    break;
                }
            }
            if (!Unique) break;
        }
        if (!Unique)
        {
            pf("Not unique\n");
            continue;
        }
        FRE(i,1,n)
        {
            FRE(j,1,m)
            {
                if (a[i][j]==0) pf("*");
                else if (a[i][j]==2) pf("^");
                else if (a[i][j]==3) pf(">");
                else if (a[i][j]==4) pf("v");
                else if (a[i][j]==5) pf("<");
            }
            pf("\n");
        }
    }
    return 0;
}
/*
3 3
...
.*.
...
4 4
..**
*...
*.**
....
2 4
*..*
....
1 1
.
1 1
*
10 10
*..**....*
..**.*....
**...*****
..**..*...
**...*...*
.*.*.*.*..
.***.**.*.
****.**.*.
...*....*.
**.**.**..

*/



D. Drazil and Tiles (CF 515D bfs搜索)

标签:d. drazil and tiles   cf 515d   bfs搜索   

原文地址:http://blog.csdn.net/u014422052/article/details/44702541

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