0%

UVA - 1589

思路

用一个二维字符数组 Xiangqi[11][10] 模拟棋盘,一个二维整型数组 checkmate[11][10] 记录黑将不能走的点
通过车马炮的位置与其走子的规则更新 checkmate 数组,更新完后判断黑方能否走子,若不能,则将死
注意:
如果一开始将帅就碰面,直接判 “NO”
黑将可以吃子

1000个测试用例也过了,各种输入方式也试了,尽力了,还是WA,也许是还有细节没找到,或者方法有问题吧。。。
代码真是又臭又长
只能先放着了,菜,是原罪

代码

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;

char Xiangqi[11][10];
int checkmate[11][10];

bool is_valid(int x, int y) {
int flag = 1;
if (x < 1 || x > 10 || y < 1 || y > 9) flag = 0;
if (flag) return true;
else return false;
}

void general_check(int x, int y) {
for (int i = x - 1; i > 0; i--) {
if (Xiangqi[i][y] != '0') {
if (i > 0 && i < 4) {
if (Xiangqi[i - 1][y] == 'J' || Xiangqi[i][y - 1] == 'J' || Xiangqi[i][y + 1] == 'J') checkmate[i][y] = 1;
}
break;
}
else checkmate[i][y] = 1;
}
}

void chariot_check(int x, int y) {
for (int l_w = x; l_w > 0; l_w--) {
if (is_valid(l_w - 1, y)) {
if (Xiangqi[l_w - 1][y] != '0' && Xiangqi[l_w - 1][y] != 'J') {
checkmate[l_w - 1][y] = 1; break;
}else checkmate[l_w - 1][y] = 1;
}
}
for (int l_s = x; l_s < 11; l_s++) {
if (is_valid(l_s + 1, y)) {
if (Xiangqi[l_s + 1][y] != '0' && Xiangqi[l_s + 1][y] != 'J') {
checkmate[l_s + 1][y] = 1; break;
}else checkmate[l_s + 1][y] = 1;
}
}
for (int l_a = y; l_a > 0; l_a--) {
if (is_valid(x, l_a - 1)) {
if (Xiangqi[x][l_a - 1] != '0' && Xiangqi[x][l_a - 1] != 'J') {
checkmate[x][l_a - 1] = 1; break;
}else checkmate[x][l_a - 1] = 1;
}
}
for (int l_d = y; l_d < 10; l_d++) {
if (is_valid(x, l_d + 1)) {
if (Xiangqi[x][l_d + 1] != '0' && Xiangqi[x][l_d + 1] != 'J') {
checkmate[x][l_d + 1] = 1; break;
}else checkmate[x][l_d + 1] = 1;
}
}
}

void horse_check(int x, int y) {
if (x - 1 > 0 && Xiangqi[x - 1][y] == '0') {
if (is_valid(x - 2, y - 1)) checkmate[x - 2][y - 1] = 1;
if (is_valid(x - 2, y + 1)) checkmate[x - 2][y + 1] = 1;
}
if (x + 1 < 11 && Xiangqi[x + 1][y] == '0') {
if (is_valid(x + 2, y - 1)) checkmate[x + 2][y - 1] = 1;
if (is_valid(x + 2, y + 1)) checkmate[x + 2][y + 1] = 1;
}
if (y - 1 > 0 && Xiangqi[x][y - 1] == '0') {
if (is_valid(x - 1, y - 2)) checkmate[x - 1][y - 2] = 1;
if (is_valid(x + 1, y - 2)) checkmate[x + 1][y - 2] = 1;
}
if (y + 1 < 10 && Xiangqi[x][y + 1] == '0') {
if (is_valid(x - 1, y + 2)) checkmate[x - 1][y + 2] = 1;
if (is_valid(x + 1, y + 2)) checkmate[x + 1][y + 2] = 1;
}
}

void cannon_check(int x, int y) {
for (int l_w = x - 1; l_w > 0; l_w--) {
if (Xiangqi[l_w][y] != '0' && Xiangqi[l_w][y] != 'J') {
int loca = l_w - 1;
while (is_valid(loca, y)) {
if (Xiangqi[loca][y] == '0' || Xiangqi[loca][y] == 'J') {
checkmate[loca][y] = 1;
loca--;
}
else {
checkmate[loca][y] = 1;
break;
}
}
}
}
for (int l_s = x + 1; l_s < 11; l_s++) {
if (Xiangqi[l_s][y] != '0' && Xiangqi[l_s][y] != 'J') {
int loca = l_s + 1;
while (is_valid(loca, y)) {
if (Xiangqi[loca][y] == '0' || Xiangqi[loca][y] == 'J') {
checkmate[loca][y] = 1;
loca++;
}
else {
checkmate[loca][y] = 1;
break;
}
}
}
}
for (int l_a = y - 1; l_a > 0; l_a--) {
if (Xiangqi[x][l_a] != '0' && Xiangqi[x][l_a] != 'J') {
int loca = l_a - 1;
while (is_valid(x, loca)) {
if (Xiangqi[x][loca] == '0' || Xiangqi[x][loca] == 'J') {
checkmate[x][loca] = 1;
loca--;
}
else {
checkmate[x][loca] = 1;
break;
}
}
}
}
for (int l_d = y + 1; l_d < 10; l_d++) {
if (Xiangqi[x][l_d] != '0' && Xiangqi[x][l_d] != 'J') {
int loca = l_d + 1;
while (is_valid(x, loca)) {
if (Xiangqi[x][loca] == '0' || Xiangqi[x][loca] == 'J') {
checkmate[x][loca] = 1;
loca++;
}
else {
checkmate[x][loca] = 1;
break;
}
}
}
}
}

void init_checkmate() {
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 10; j++) {
if (Xiangqi[i][j] == 'R')
chariot_check(i, j);
else if (Xiangqi[i][j] == 'C')
cannon_check(i, j);
else if (Xiangqi[i][j] == 'H')
horse_check(i, j);
else if (Xiangqi[i][j] == 'G')
general_check(i, j);
}
}
}

bool check_mate(int x, int y) {
int flag = 1;
for (int j = 3; j <= 7; j++) {
checkmate[0][j] = 1;
checkmate[4][j] = 1;
}
for (int i = 0; i <= 4; i++) {
checkmate[i][3] = 1;
checkmate[i][7] = 1;
}
if (checkmate[x - 1][y] == 0 || checkmate[x + 1][y] == 0 || checkmate[x][y - 1] == 0 || checkmate[x][y + 1] == 0) flag = 0;
if (flag)
return true;
else
return false;
}

bool GJ_meet(int X1, int Y1, int X2, int Y2) {
if (Y1 == Y2) {
int r = Y1;
int flag = 0;
for (int s = X1 + 1; s < X2; s++)
if (Xiangqi[s][r] != '0') flag = 1;
if (!flag) return true;
}
return false;
}

int main()
{
int L_x, L_y;
int J_x, J_y;
int G_x, G_y;
int n;
//char ch[101];
//freopen("out.txt", "w", stdout);
//while (scanf("%d%d%d", &n, &L_x, &L_y) == 3 && n != 0)
while (cin >> n >> L_x >> L_y && n != 0)
{
memset(Xiangqi, '0', sizeof(Xiangqi));
memset(checkmate, 0, sizeof(checkmate));
Xiangqi[L_x][L_y] = 'J';
J_x = L_x; J_y = L_y;
while (n--) {
char Qizi;
//getchar();
//scanf("%c", &Qizi);
//scanf("%d%d", &L_x, &L_y);
cin >> Qizi;
cin >> L_x >> L_y;
//gets(ch);
//sscanf(ch, "%c %d %d", &Qizi, &L_x, &L_y);
Xiangqi[L_x][L_y] = Qizi;
if (Qizi == 'G') {
G_x = L_x;
G_y = L_y;
}
}
if (GJ_meet(J_x, J_y, G_x, G_y)) {
printf("NO\n");
//cout << "NO" << endl;
continue;
}
/*
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 10; j++)
printf("%c", Xiangqi[i][j]);
printf("\n");
}printf("\n");
*/
init_checkmate();
/*
for (int i = 1; i < 11; i++) {
for (int j = 1; j < 10; j++)
printf("%d", checkmate[i][j]);
printf("\n");
}printf("\n");
*/
if (check_mate(J_x, J_y))
printf("YES\n");
//cout << "YES" << endl;
else
printf("NO\n");
//cout << "NO" << endl;
}
//fclose(stdout);
}