【HDOJ】2699 Five in a Row
wa了几次,至少要考虑4个方向:下、右、左下、右下。很像当年北航的机试题目。
1 /* 2699 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cstring> 6 #include <queue> 7 using namespace std; 8 9 #define MAXN 16 10 11 typedef struct node_t { 12 char x, y, d, c; 13 bool f; 14 node_t() {} 15 node_t(char xx, char yy, char dd, char cc, bool ff) { 16 x = xx; y = yy; d = dd; c = cc; f = ff; 17 } 18 } node_t; 19 20 char map[MAXN][MAXN]; 21 char n = 15; 22 char ch; 23 char dir[4][2] = { 24 // right, down, diag(2) 25 0, 1, 1,0, 1, 1, 1,-1 26 }; 27 28 bool check(char x, char y) { 29 return x<0 || x>=n || y<0 || y>=n; 30 } 31 32 bool bfs(char xx, char yy) { 33 queue<node_t> Q; 34 node_t nd; 35 char i, j, k; 36 char x, y; 37 38 for (i=0; i<4; ++i) { 39 nd.x = xx + dir[i][0]; 40 nd.y = yy + dir[i][1]; 41 nd.d = i; 42 nd.c = 3; 43 if (check(nd.x, nd.y)) 44 continue; 45 if (map[nd.x][nd.y] == ch) { 46 nd.f = false; 47 Q.push(nd); 48 x = xx - dir[i][0]; 49 y = yy - dir[i][1]; 50 if (!check(x, y) && map[x][y]=='.') { 51 nd.f = true; 52 nd.c = 2; 53 Q.push(nd); 54 } 55 } else if (map[nd.x][nd.y] == '.') { 56 nd.f = true; 57 Q.push(nd); 58 } 59 } 60 61 while (!Q.empty()) { 62 nd = Q.front(); 63 Q.pop(); 64 if (nd.c==0) { 65 return true; 66 } 67 nd.x += dir[nd.d][0]; 68 nd.y += dir[nd.d][1]; 69 if (check(nd.x, nd.y)) 70 continue; 71 --nd.c; 72 if (map[nd.x][nd.y] == ch) { 73 Q.push(nd); 74 } else if (map[nd.x][nd.y]=='.' && nd.f==false){ 75 nd.f = true; 76 Q.push(nd); 77 } 78 } 79 80 return false; 81 } 82 83 int main() { 84 int t, tt=0; 85 int w, b; 86 char i, j, k; 87 bool flag; 88 89 #ifndef ONLINE_JUDGE 90 freopen("data.in", "r", stdin); 91 freopen("data.out", "w", stdout); 92 #endif 93 94 scanf("%d", &t); 95 while (t--) { 96 w = b = 0; 97 for (i=0; i<n; ++i) { 98 scanf("%s", map[i]); 99 for (j=0; j<n; ++j) { 100 if (map[i][j] == 'W') 101 ++w; 102 else if (map[i][j] == 'B') 103 ++b; 104 } 105 } 106 ch = (w==b) ? 'B' : 'W'; 107 flag = false; 108 for (i=0; i<n; ++i) { 109 for (j=0; j<n; ++j) { 110 if (map[i][j]==ch && bfs(i,j)) { 111 flag = true; 112 goto _output; 113 } 114 } 115 } 116 _output: 117 if (flag) 118 puts("YES"); 119 else 120 puts("NO"); 121 } 122 123 return 0; 124 }