-
Notifications
You must be signed in to change notification settings - Fork 0
/
10196.cpp
154 lines (141 loc) · 3.56 KB
/
10196.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
struct Chess {
char type;
int x, y;
} wc[64], bc[64], wk, bk;
int lenw, lenb;
bool exist[8][8];
inline void parse(int x, int y, char c)
{
exist[x][y] = true;
switch (c) {
case 'K':
wk.x = x;
wk.y = y;
case 'P':
case 'N':
case 'B':
case 'R':
case 'Q':
wc[lenw].x = x;
wc[lenw].y = y;
wc[lenw++].type = c;
break;
case 'k':
bk.x = x;
bk.y = y;
case 'p':
case 'n':
case 'b':
case 'r':
case 'q':
bc[lenb].x = x;
bc[lenb].y = y;
bc[lenb++].type = c;
break;
}
}
bool checkDiagonal(int cx, int cy, int kx, int ky)
{
if ((abs(cx - kx) == abs(cy - ky))) {
if (cx > kx) {
if (cy > ky) {
for (int i = cx - 1, j = cy - 1; i > kx && j > ky; --i, --j)
if (exist[i][j]) return false;
}
else {
for (int i = cx - 1, j = cy + 1; i > kx && j < ky; --i, ++j)
if (exist[i][j]) return false;
}
}
else {
if (cy > ky) {
for (int i = cx + 1, j = cy - 1; i < kx && j > ky; ++i, --j)
if (exist[i][j]) return false;
}
else {
for (int i = cx + 1, j = cy + 1; i < kx && j < ky; ++i, ++j)
if (exist[i][j]) return false;
}
}
return true;
}
return false;
}
bool checkCross(int cx, int cy, int kx, int ky)
{
if (cx == kx) {
if (cy < ky) swap(cy, ky);
for (int i = ky + 1; i < cy; ++i)
if (exist[cx][i]) return false;
return true;
}
if (cy == ky) {
if (cx < kx) swap(cx, kx);
for (int i = kx + 1; i < cx; ++i)
if (exist[i][cy]) return false;
return true;
}
return false;
}
bool check(Chess *c, Chess *k)
{
int cx = c->x;
int cy = c->y;
int kx = k->x;
int ky = k->y;
switch (c->type) {
case 'P':
return (cx - 1 == kx && abs(cy - ky) == 1);
case 'p':
return (cx + 1 == kx && abs(cy - ky) == 1);
case 'N':
case 'n':
return (abs(cx - kx) == 2 && abs(cy - ky) == 1) || (abs(cx - kx) == 1 && abs(cy - ky) == 2);
case 'Q':
case 'q':
return checkDiagonal(cx, cy, kx, ky) || checkCross(cx, cy, kx, ky);
case 'B':
case 'b':
return checkDiagonal(cx, cy, kx, ky);
case 'R':
case 'r':
return checkCross(cx, cy, kx, ky);
}
return false;
}
int main(void)
{
bool checkw, checkb;
char line[10];
int T = 0;
lenw = lenb = 0;
memset(exist, false, sizeof(exist));
while (1) {
for (int i = 0; i < 8; ++i) {
scanf("%s", line);
for (int j = 0; j < 8; ++j)
if (line[j] != '.') parse(i, j, line[j]);
}
if (!lenw && !lenb) break;
checkb = false;
checkw = false;
for (int i = 0; !checkw && !checkb && i < lenb; ++i)
checkw = check(bc + i, &wk);
for (int i = 0; !checkw && !checkb && i < lenw; ++i)
checkb = check(wc + i, &bk);
if (checkw)
printf("Game #%d: white king is in check.\n", ++T);
else if (checkb)
printf("Game #%d: black king is in check.\n", ++T);
else
printf("Game #%d: no king is in check.\n", ++T);
lenw = lenb = 0;
memset(exist, false, sizeof(exist));
}
return 0;
}