标签:
题目链接: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 <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <vector> 8 #include <queue> 9 #include <cmath> 10 #include <ctime> 11 #include <list> 12 #include <set> 13 #include <map> 14 using namespace std; 15 typedef long long LL; 16 typedef pair <int, int> P; 17 const int N = 2e3 + 5; 18 int n, m, tx[] = {-1, 0, 1, 0}, ty[] = {0, -1, 0, 1}; 19 char str[N][N]; 20 int du[N*N]; 21 vector <int> G[N*N]; 22 inline bool judge(int x, int y) { 23 if(x >= 0 && x < n && y >= 0 && y < m && str[x][y] == ‘.‘) 24 return true; 25 return false; 26 } 27 inline void get(int x, int y) { 28 for(int i = 0 ; i < 4 ; ++i) { 29 int xx = x + tx[i], yy = y + ty[i]; 30 if(judge(xx, yy)) { 31 G[x*m + y].push_back(xx*m + yy); 32 du[x*m + y]++; 33 } 34 } 35 } 36 inline void change(int x, int y) { 37 if(m == 1) { 38 if(x < y) { 39 str[x][0] = ‘^‘; 40 str[y][0] = ‘v‘; 41 } else { 42 str[x][0] = ‘v‘; 43 str[y][0] = ‘^‘; 44 } 45 } else { 46 if(x - y == -m) { 47 str[x/m][x%m] = ‘^‘; 48 str[y/m][y%m] = ‘v‘; 49 } else if(x - y == m) { 50 str[x/m][x%m] = ‘v‘; 51 str[y/m][y%m] = ‘^‘; 52 } else if(x - y == -1) { 53 str[x/m][x%m] = ‘<‘; 54 str[y/m][y%m] = ‘>‘; 55 } else { 56 str[x/m][x%m] = ‘>‘; 57 str[y/m][y%m] = ‘<‘; 58 } 59 } 60 } 61 62 int main() 63 { 64 int cnt = 0, op = 0; 65 scanf("%d %d", &n, &m); 66 for(int i = 0; i < n; ++i) { 67 scanf("%s", str[i]); 68 } 69 for(int i = 0; i < n; ++i) { 70 for(int j = 0; j < m; ++j) { 71 if(judge(i, j)) { 72 get(i, j); 73 ++op; 74 } 75 } 76 } 77 queue <int> que; 78 for(int i = 0; i < n*m; ++i) { 79 if(du[i] == 1) 80 que.push(i); 81 } 82 while(!que.empty()) { 83 int u = que.front(); 84 que.pop(); 85 du[u]--; 86 for(int i = 0; i < G[u].size(); ++i) { 87 int v = G[u][i]; 88 du[v]--; 89 if(du[v] > 0) { 90 cnt++; 91 for(int j = 0; j < G[v].size(); ++j) { 92 du[G[v][j]]--; 93 if(du[G[v][j]] == 1) { 94 que.push(G[v][j]); 95 } 96 } 97 du[v] = 0; 98 change(u, v); 99 } else if(du[v] == 0) { 100 cnt++; 101 change(u, v); 102 } 103 } 104 } 105 if(cnt * 2 == op) { 106 for(int i = 0; i < n; ++i) { 107 printf("%s\n", str[i]); 108 } 109 } else { 110 printf("Not unique\n"); 111 } 112 return 0; 113 }
Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
标签:
原文地址:http://www.cnblogs.com/Recoder/p/5742781.html