-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva260.cpp
65 lines (59 loc) · 1.26 KB
/
uva260.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int mx = 401;
int grid[mx][mx];
int vis[mx][mx];
int n;
bool safe(int i, int j) {
return (i<n && i>=0) && (j<n && j>=0) && (grid[i][j]==1);
}
bool kill(int i, int j) {
if (vis[i][j] == 1) return false;
if (!safe(i, j)) return false;
if (i == (n-1)) return true;
vis[i][j] = 1;
if (kill(i-1, j-1)) return true;
if (kill(i-1, j)) return true;
if (kill(i, j-1)) return true;
if (kill(i, j+1)) return true;
if (kill(i+1, j+1)) return true;
if (kill(i+1, j)) return true;
return false;
}
bool solve() {
memset(grid, -1, sizeof grid);
memset(vis, -1, sizeof vis);
string s;
// cout << " solving\n";
for (int i=0; i<n; i++) {
cin >> s;
for (int j=0; j<n; j++) {
grid[i][j] = (s[j]=='b' ? 1 : 0);
}
}
for (int i=0; i<n; i++) {
if (kill(0, i)) {
return true;
}
}
return false;
}
int main() {
// int t,
int cnt=1;;
cin >> n;
while (n != 0) {
cout << cnt << " ";
if (solve()) {
cout << "B\n";
} else {
cout << "W\n";
}
cnt++;
cin >> n;
}
}