-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoj 1002(2).cpp
83 lines (70 loc) · 1.21 KB
/
zoj 1002(2).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
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <string.h>
int n, best;
char map[5][5];
void Search(int m);
int Over(void);
int Canput(int i, int j);
int main(void)
{
while (scanf("%d", &n) != EOF && n)
{
for (int i = 0; i < n; i++)
scanf("%s", map[i]);
best = 0;
Search(0);
printf("%d\n", best);
}
return 0;
}
void Search(int m)
{
if (Over())
{
best = m > best? m : best;
return;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (Canput(i,j))
{
map[i][j] = '@';
Search(m + 1);
map[i][j] = '.';
}
}
}
int Over(void)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (Canput(i,j)) return 0;
return 1;
}
int Canput(int i, int j)
{
int row, col;
if (map[i][j] != '.') return 0;
for (row = i - 1; row >= 0; row--)
{
if (map[row][j] == 'X') break;
if (map[row][j] == '@') return 0;
}
for (row = i + 1; row < n; row++)
{
if (map[row][j] == 'X') break;
if (map[row][j] == '@') return 0;
}
for (col = j - 1; col >= 0; col--)
{
if (map[i][col] == 'X') break;
if (map[i][col] == '@') return 0;
}
for (col = j + 1; col < n; col++)
{
if (map[i][col] == 'X') break;
if (map[i][col] == '@') return 0;
}
return 1;
}