题目链接:http://codeforces.com/problemset/problem/516/B
一个n*m的方格,'*'不能填。给你很多个1*2的尖括号,问你是否能用唯一填法填满方格。
类似topsort,'.'与上下左右的'.',的相连。从度为1的点作为突破口。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 2e3 + ;
int n, m, tx[] = {-, , , }, ty[] = {, -, , };
char str[N][N];
int du[N*N];
vector <int> G[N*N];
inline bool judge(int x, int y) {
if(x >= && x < n && y >= && y < m && str[x][y] == '.')
return true;
return false;
}
inline void get(int x, int y) {
for(int i = ; i < ; ++i) {
int xx = x + tx[i], yy = y + ty[i];
if(judge(xx, yy)) {
G[x*m + y].push_back(xx*m + yy);
du[x*m + y]++;
}
}
}
inline void change(int x, int y) {
if(m == ) {
if(x < y) {
str[x][] = '^';
str[y][] = 'v';
} else {
str[x][] = 'v';
str[y][] = '^';
}
} else {
if(x - y == -m) {
str[x/m][x%m] = '^';
str[y/m][y%m] = 'v';
} else if(x - y == m) {
str[x/m][x%m] = 'v';
str[y/m][y%m] = '^';
} else if(x - y == -) {
str[x/m][x%m] = '<';
str[y/m][y%m] = '>';
} else {
str[x/m][x%m] = '>';
str[y/m][y%m] = '<';
}
}
} int main()
{
int cnt = , op = ;
scanf("%d %d", &n, &m);
for(int i = ; i < n; ++i) {
scanf("%s", str[i]);
}
for(int i = ; i < n; ++i) {
for(int j = ; j < m; ++j) {
if(judge(i, j)) {
get(i, j);
++op;
}
}
}
queue <int> que;
for(int i = ; i < n*m; ++i) {
if(du[i] == )
que.push(i);
}
while(!que.empty()) {
int u = que.front();
que.pop();
du[u]--;
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
du[v]--;
if(du[v] > ) {
cnt++;
for(int j = ; j < G[v].size(); ++j) {
du[G[v][j]]--;
if(du[G[v][j]] == ) {
que.push(G[v][j]);
}
}
du[v] = ;
change(u, v);
} else if(du[v] == ) {
cnt++;
change(u, v);
}
}
}
if(cnt * == op) {
for(int i = ; i < n; ++i) {
printf("%s\n", str[i]);
}
} else {
printf("Not unique\n");
}
return ;
}