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 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".

思路:用有点类似拓扑排序的方法做。。一直找度为1的结点,直到找不到为止,这里的度的意思是‘.‘挨着的‘.‘的个数

注意这里由于数据输入输出比较大,最好用scanf和printf来输入输出,直接用cin和cout会超时,如果硬要用cin和cout可以加一句

ios::sync_with_stdio(false);

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 2005;
char a[maxn][maxn];
pair<int ,int> p[maxn * maxn];
int mox[4] = {-1, 0, 1, 0};
int moy[4] = {0, 1, 0, -1};
char cg[4] = {'v', '<', '^', '>'};
int n, m;

bool ispoint(int x, int y) {
	return (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '.');
}

int deg(int x, int y) {
	int ans = 0;
	for(int i = 0; i < 4; i++) {
		int fx = x + mox[i];
		int fy = y + moy[i];
		if(ispoint(fx, fy)) ans++;
	}
	return ans;
}

int main() {
	while(scanf("%d %d", &n, &m) != EOF) {
		for(int i = 0; i < n; i++)
			scanf("%s", a[i]);

		int h = 0, sum = 0;
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				if(ispoint(i, j) && deg(i, j) == 1)
					p[sum++] = make_pair(i, j);

		for(; h < sum; h++) {
			int x = p[h].first, y = p[h].second;
			for(int i = 0; i < 4; i++) {
				int fx = x + mox[i];
				int fy = y + moy[i];
				if(ispoint(fx, fy)) {
					a[x][y] = cg[i];
					a[fx][fy] = cg[i ^ 2];
					for(int i = 0; i < 4; i++) {
						int ffx = fx + mox[i];
						int ffy = fy + moy[i];
						if(ispoint(ffx, ffy) && deg(ffx, ffy) == 1)
							p[sum++] = make_pair(ffx, ffy);
					}
					break;
				}
			}
		}

		int flag = 0;
		for(int i = 0; i < n && !flag; i++)
			for(int j = 0; j < m && !flag; j++)
				 if(a[i][j] == '.' ) flag = 1;
		if(flag == 1) printf("Not unique\n");
		else {
			for(int i = 0; i < n; i++, printf("\n"))
				for(int j = 0; j < m; j++)
					printf("%c", a[i][j]);
		}
	}
	return 0;
}
时间: 2024-10-29 19:05:44

Codeforces Round #292 (Div. 2) -- D. 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. 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

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 tile

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 #541 (Div. 2) D 并查集 + 拓扑排序

https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点

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

Codeforces Round #290 (Div. 2) C. Fox And Names 拓扑排序

C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t