Codeforces Round #292 (Div. 1) - B. Drazil and Tiles

B. Drazil and Tiles

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:

<>^^*vv<>

and

^<>v*^<>v

so the answer is "Not unique".



题意:用一些1*2的瓷砖覆盖m*n的网格(有障碍),问方案是否唯一,是则输出覆盖的方案。(1 ≤ n, m ≤ 2000).

这题像极了某次cf的一道题(判断网格中是否有环),不过那题数据比较小,直接暴力的拓扑排序即可。

而这题为了在o(m*n)内构造出解,需要维护一个保存所有度为1的点的队列,以及一个保存所有点的度数的数组,每次将一个点出队,同时将唯一的那个与该点相邻的点删掉,然后更改删掉的点的相邻的点的度数(常数时间),度为1的话即入队。

当发现队列空的时候,表明找不到度为1的点了,这时要不是全部覆盖完了,就是存在环或者单独的点(这种情况显然是无解或者多解),再循环判断一遍即可。


 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <queue>
 4 using namespace std;
 5 #define MAXN 2010
 6
 7 struct Point
 8 {
 9     int x,y;
10 };
11 char G[MAXN][MAXN];
12 int degree[MAXN][MAXN]={0};
13 int m,n;
14 bool isin(int x,int y)
15 {
16     return x>=0 && x<m && y>=0 && y<n;
17 }
18
19 int dx[]={1,0,-1, 0};
20 int dy[]={0,1, 0,-1};
21 int getDegree(int x,int y)
22 {
23     if(G[x][y]==‘*‘)
24         return 0;
25     int sum=0;
26     for(int i=0;i<4;i++)
27     {
28         int newx=x+dx[i];
29         int newy=y+dy[i];
30         sum+=(isin(newx,newy) && G[newx][newy]!=‘*‘);
31     }
32     return sum;
33 }
34
35 int main()
36 {
37     cin>>m>>n;
38     for(int i=0;i<m;i++)
39         scanf("%s",G[i]);
40
41     queue<Point> Q;
42     for(int i=0;i<m;i++)
43         for(int j=0;j<n;j++)
44         {
45             degree[i][j]=getDegree(i,j);
46             if(degree[i][j] == 1)
47                 Q.push(Point{i,j});
48         }
49
50 //    int dx[]={1,0,-1, 0};
51 //    int dy[]={0,1, 0,-1};
52     char oldtype[]={‘^‘,‘<‘,‘v‘,‘>‘};
53     char newtype[]={‘v‘,‘>‘,‘^‘,‘<‘};
54     while(!Q.empty())
55     {
56         Point x=Q.front();
57         Q.pop();
58         degree[x.x][x.y]=0;
59         for(int i=0;i<4;i++)
60         {
61             int newx=x.x+dx[i];
62             int newy=x.y+dy[i];
63             if(isin(newx, newy) && degree[newx][newy])
64             {
65                 degree[newx][newy]=0;
66                 G[x.x][x.y]=oldtype[i];
67                 G[newx][newy]=newtype[i];
68                 // 更新与(newx, newy)相邻的点的度数
69                 for(int j=0;j<4;j++)
70                 {
71                     int curx=newx+dx[j];
72                     int cury=newy+dy[j];
73                     if(isin(curx, cury) && degree[curx][cury])
74                     {
75                         degree[curx][cury]--;
76                         if(degree[curx][cury]==1)
77                             Q.push(Point{curx, cury});
78                     }
79                 }
80                 break;
81             }
82         }
83     }
84
85     for(int i=0;i<m;i++)
86         for(int j=0;j<n;j++)
87         {
88             if(G[i][j]==‘.‘)
89             {
90                 puts("Not unique");
91                 return 0;
92             }
93         }
94
95     for(int i=0;i<m;i++)
96         puts(G[i]);
97
98     return 0;
99 }
时间: 2024-08-05 18:26:09

Codeforces Round #292 (Div. 1) - B. Drazil and Tiles的相关文章

Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]

传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes 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 occupie

Codeforces Round #292 (Div. 2) -- D. Drazil and Tiles (拓扑排序)

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 a

Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓扑排序)

题目地址:codeforces 292 B 用队列维护度数为1的点,也就是可以唯一确定的点,然后每次找v1,v2,并用v2来更新与之相连的点,如果更新后的点度数为1,就加入队列.若最后还有为"."的,说明无解或解不唯一. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #i

Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似topsort,'.'与上下左右的'.',的相连.从度为1的点作为突破口. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostr

Codeforces Round #292 (Div. 1)---A. Drazil and Factorial

Drazil is playing a math game with Varda. Let's define for positive integer x as a product of factorials of its digits. For example, . First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This n

Codeforces Round #292 (Div. 2) -- B. Drazil and His Happy Friends

B. Drazil and His Happy Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his f

Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C

C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Drazil is playing a math game with Varda. Let's define  for positive integer x as a product of factorials of its dig

#292 (div.2) D.Drazil and Tiles (贪心+bfs)

Description 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

Codeforces Round #292 (Div. 2 Div. 1)

比赛链接:http://codeforces.com/contest/515  http://codeforces.com/contest/516 A. Drazil and Date time limit per test 1 second memory limit per test 256 megabytes Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. D