-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP639.cpp
75 lines (69 loc) · 1.46 KB
/
P639.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
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cstdio>
using namespace std;
#define ROOK 0
#define EMPTY 1
#define WALL 2
void run(const int N, int * const board, const int cur, int &max) {
// try to ad a new rook:
for(int x = 0; x < N; ++x) {
for(int y = 0; y < N; ++y) {
if(board[N*y+x] != EMPTY)
continue;
// See if any other rook challenges this position:
bool ok = true;
// Up:
for(int yy = y+1; ok && yy < N; ++yy) {
if(board[N*yy+x] == ROOK)
ok = false;
if(board[N*yy+x] == WALL)
break;
}
// down:
for(int yy = y-1; ok && yy >= 0; --yy) {
if(board[N*yy+x] == ROOK)
ok = false;
if(board[N*yy+x] == WALL)
break;
}
// Right:
for(int xx = x+1; ok && xx < N; ++xx) {
if(board[N*y+xx] == ROOK)
ok = false;
if(board[N*y+xx] == WALL)
break;
}
// Left:
for(int xx = x-1; ok && xx >= 0; --xx) {
if(board[N*y+xx] == ROOK)
ok = false;
if(board[N*y+xx] == WALL)
break;
}
if(!ok)
continue;
if(cur+1 > max)
max = cur+1;
board[N*y+x] = ROOK;
run(N, board, cur+1, max);
board[N*y+x] = EMPTY;
} // for x
} // for y
} // run()
int main() {
int N, board[16];
string s;
while(true) {
cin >> N;
if(N == 0)
return 0;
for(int i = 0; i < N; ++i) {
cin >> s;
for(int j = 0; j < N; ++j)
board[N*i+j] = s[j] == '.' ? EMPTY : WALL;
}
int max = 0;
run(N, board, 0, max);
cout << max << endl;
}
}