传送门:QAQ
题意:相邻两个点’ . ‘之间可以放<,>.问你将所有’.‘都覆盖,是否存在唯一的方法。
思路:简单的拓扑排序,选择相邻的空格为’.‘开始放,如果不存在这样的空格,就代表方法不止一种。
代码:
#include<iostream>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
char ch[2100][2100];
int dx[5] = { -1,0,0,1 };
int dy[5] = { 0,1,-1,0 };
char code[5] = { 'v','<','>','^'};
struct inst {
int x;
int y;
inst() {}
inst(int x, int y) :x(x), y(y) {}
};
int n, m;
void bfs() {
queue<inst>q;
for (int i = 0; i < n; i++) {
for (int z = 0; z < m; z++) {
int gg = 0;
if (ch[i][z] == '.') {
for (int g = 0; g < 4; g++) {
int x1 = i + dx[g];
int y1 = z + dy[g];
if (x1 >= 0 && x1 < n&&y1 >= 0 && y1 < m&&ch[x1][y1] == '.') {
gg++;
}
}
if (gg == 1) {
q.push(inst(i, z));
}
}
}
}
while (!q.empty()) {
inst a = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x1 = a.x + dx[i];
int y1 = a.y + dy[i];
if (x1 >= 0 && x1 < n&&y1 >= 0 && y1 < m&&ch[x1][y1] == '.') {
ch[a.x][a.y] = code[i];
ch[x1][y1] = code[3 - i];
for (int g = 0; g < 4; g++) {
int xx1 = x1 + dx[g];
int yy1 = y1 + dy[g];
if (xx1 >= 0 && xx1 < n&&yy1 >= 0 && yy1 < m&&ch[xx1][yy1] == '.') {
int gg = 0;
for (int j = 0; j < 4; j++) {
int xxx1 = xx1 + dx[j];
int yyy1 = yy1 + dy[j];
if (xxx1 >= 0 && xxx1 < n&&yyy1 >= 0 && yyy1 < m&&ch[xxx1][yyy1] == '.') {
gg++;
}
}
if (gg == 1) {
q.push(inst(xx1, yy1));
}
}
}
}
}
}
}
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
getchar();
for (int z = 0; z < m; z++) {
scanf("%c", &ch[i][z]);
}
}
bfs();
int flag = 0;
for (int i = 0; i < n; i++) {
for (int z = 0; z < m; z++) {
if (ch[i][z] == '.') {
flag = 1;
break;
}
}
if (flag)
break;
}
if (flag)
printf("Not unique\n");
else {
for (int i = 0; i < n; i++) {
for (int z = 0; z < m; z++) {
printf("%c", ch[i][z]);
}
printf("\n");
}
}
return 0;
}