码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-18 14:09:31      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:codeforces   拓扑排序   acm   

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;
}

















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

标签:codeforces   拓扑排序   acm   

原文地址:http://blog.csdn.net/u014355480/article/details/43876569

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