D. Drazil and Tiles (CF 515D BFS Search)

Source: Internet
Author: User

D. Drazil and Tilestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Drazil created a following problem about putting 1?x?2 tiles to an nx? M Grid:

"There is a grid with some cells," is empty and some cells, "is occupied. You should use 1?x?2 tiles to cover all empty cells and no, tiles should cover each of the other. And you should the print a solution about what to do it. "

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

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

Can You solve this new problem?

Note that you should print ' isunique' either when there exists no solution or when there exists several differe NT Solutions for the original task.


The First line contains Integers  n  and 

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


If There is no solution or the solution are not unique, you should print the string ' notunique'.

Otherwise should print how to cover all empty cells with 1?x?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....* ....
Not unique
4 4..***...*.** ....
2. *....
1 1.
Not unique
1 1*

In the first case, there is indeed the solutions:

<> ^^ *vv<>



The answer is ' notunique'.

Test instructions: N*m's diagram, '. ' Indicate a space, now use 1*2 brick to fill it, can horizontal (' < ', ' > ') Fill and vertical (' ^ ', ' V ') fill. Find the base element block, (I,J) and its adjacent four points as a basic element block, if (i,j) around the '. ' Only one then this (i,j) place is fixed, fill out (i,j) see if there are other points around it because filling (i,j) after the only change, there is the queue, so step by step to find the fixed fill (i,j), update the surrounding points.


#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 RSO  N 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, si Zeof (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 Nam Espace std;typedef pair<int,int>pa;int a[maxn][MAXN]; '. '; 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    &LT;=M) return true; return false;}    int degree (int x,int y,int &dir,int &xx,int &yy)//(XX,YY) record the position of the point (X, y) paired with {int s=0;  if (a[x-1][y]==1) {s++;d ir=4;xx=x-1;yy=y;}  On if (a[x][y-1]==1) {s++;d ir=3;xx=x;yy=y-1;}  Left if (a[x+1][y]==1) {s++;d ir=2;xx=x+1,yy=y;}  Under if (a[x][y+1]==1) {s++;d ir=5;xx=x;yy=y+1;} Right 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)//Finally if there is '. '                    It is necessary to output ' 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. *.... 1 1.1 1*10 10*. **....*.. **.*....**...*****.. **.. *...**...*...*.*.*.*.*...***.**.*.****.**.*....*....*.**.**.**.. */

D. Drazil and Tiles (CF 515D BFS Search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.