題目:http://codeforces.com/problemset/problem/515/D 題意:給你一張方格,叫你用1X2填充方格,有些方格已經被佔了(用‘*’),沒被占的('.')。豎起來填,用 "^v"表示,橫起來,用”<>"表示。如果放的方法獨一無二,輸出放好的方格圖。否則輸出“Not unique". 分析:dfs一下,一對一對地找。 Input
3 3
...
.*.
...
Output
Not unique
Input
4 4
..**
*...
*.**
....
Output
<>**
*^<>
*v**
<><>
#include <bits/stdc++.h>using namespace std;int const MAX = 2010;char s[MAX][MAX];int n, m, cnt;int dx[4] = {0, 0, 1, -1};int dy[4] = {1, -1, 0, 0};void dfs(int x, int y){ if(x < 1 || x > n || y < 1 || y > m || s[x][y] != '.') return; int dir = -1, sum = 0; for(int i = 0; i < 4; i++) { int xx = x + dx[i]; int yy = y + dy[i]; if(s[xx][yy] == '.') { sum ++; dir = i; } } if(sum == 1) { if(dir == 0) { s[x][y] = '<'; s[x][y + 1] = '>'; } else if(dir == 1) { s[x][y] = '>'; s[x][y - 1] = '<'; } else if(dir == 2) { s[x][y] = '^'; s[x + 1][y] = 'v'; } else if(dir == 3) { s[x][y] = 'v'; s[x - 1][y] = '^'; } cnt += 2; for(int i = 0; i < 4; i++) dfs(x + dx[dir] + dx[i], y + dy[dir] + dy[i]); }}void init(){ cnt = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(s[i][j] == '*') cnt ++;}int main(){ scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) scanf("%s", s[i] + 1); init(); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) dfs(i, j); if(cnt == n * m) { for(int i = 1; i <= n; i++) { printf("%s\n", s[i] + 1); } } else printf("Not unique\n"); return 0;}