Codeforces 515D Drazil and Tiles (construction) __ Construction

Source: Internet
Author: User

D. Drazil and Tiles time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard Output

Drazil created a following problem about putting 1x2 into a tiles grid:

"There is a grid with some cells, are empty and some cells that are. You are should use 1x2 tiles to cover all empty cells and no two tiles the should 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. He 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 is much larger than for the original task!

Can You solve this new problem?

Should print ' not unique ' either when there exists no solution or then there exists several different Soluti ONS for the original task. Input

The 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, which is occupied. Output

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

Otherwise you should print the cover all empty cells with 1x2 tiles. Use characters ' <> ' to denote horizontal tiles and characters ' ^v ' to denote vertical. 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 There are indeed two solutions:

<>^
^*v
v<>

and

^<>
v*^
<>v

So the answer are "not unique".

Title: Given a n*m black and white matrix, ask if there is a unique solution to make the 1*2 dominoes do not overlap and completely cover the black dots.
n,m<=1000.

The puzzle: Each time a point of 1 is removed to construct a domino until there is no point with a degree of 1.
If the black lattice has been completely covered at this time, then find the only solution, otherwise there are many solutions.
Prove:
The two points of the Domino cover are connected with the black edge, the rest is connected with the white edge, because this is a binary graph of the existence ring, we can obviously swap the black and white edges for the color and get another solution.

#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include <
Cstdio> #define N 2003 using namespace Std;
int n,m;
int ans[n][n],vis[n][n],size[n][n],map[n][n];
int st[2000003],st1[2000003],top;
int x[10]={0,0,-1,1},y[10]={-1,1,0,0};
	  	void Solve (int x1,int y1) {for (int k=0;k<4;k++) {int nowx=x1+x[k]; int nowy=y1+y[k]; if (Nowx>0&&nowy>0&&nowx<=n&&nowy<=m&&map[nowx][nowy]) {size[nowx][n
		   owy]--;
	     if (Size[nowx][nowy]==1&&!vis[nowx][nowy]) st[++top]=nowx,st1[top]=nowy;
	int main () {scanf ("%d%d", &n,&m);
		for (int i=1;i<=n;i++) {char s[n]; scanf ("%s", s+1);  for (int j=1;j<=m;j++) if (s[j]== '. ')
	Map[i][j]=1; for (int i=1;i<=n;i++) to (int j=1;j<=m;j++) if (Map[i][j]) for (int k=0;k<4;k++) {int nowx=i +X[K];
	  	int nowy=j+y[k]; if (nowx>0&&nowy>0&&nowx<=n&&nowy<=m&&map[nowx][nowy]) size[i][j]++;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (size[i][j]==1) st[++top]=i,st1[top]=j;
    	while (true) {int xx=0; int yy=0;
    	while (vis[st[top]][st1[top]]&&top>0) top--;
    	if (!top) break; Xx=st[top]; Yy=st1[top];
    	top--;
        if (!xx&&!yy) break;
    	int t=-1;
	  	   for (int k=0;k<4;k++) {int nowx=xx+x[k]; int nowy=yy+y[k]; if (Nowx>0&&nowy>0&&nowx<=n&&nowy<=m&&map[nowx][nowy]&&!vis [NOWX]
			 [Nowy])
			 	{t=k;
			 Break
	   } if (t==-1) continue; int nowx=xx+x[t];
	   int nowy=yy+y[t]; Vis[nowx][nowy]=1;
	   Vis[xx][yy]=1; 
	   ans[nowx][nowy]=t+1; Solve (nowx,nowy);
	   Solve (XX,YY);
	   if (ans[nowx][nowy]==1) ans[xx][yy]=2;
	   if (ans[nowx][nowy]==2) ans[xx][yy]=1;
	   if (ans[nowx][nowy]==3) ans[xx][yy]=4; 
    if (ans[nowx][nowy]==4) ans[xx][yy]=3; } boolF=true;
      	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (map[i][j]&&!vis[i][j)) {f=false;
      Break
    	} if (!f) {printf ("not unique\n");
    return 0;
    		for (int i=1;i<=n;i++) {(int j=1;j<=m;j++) {if (!map[i][j]) printf ("*");
    		if (ans[i][j]==1) printf ("<");
    		if (ans[i][j]==2) printf (">");
    		if (ans[i][j]==3) printf ("^");
    	if (ans[i][j]==4) printf ("V");
    printf ("\ n"); }
}



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.