抱歉,您的瀏覽器無法訪問本站
本頁面需要瀏覽器支持(啟用)JavaScript
了解詳情 >

第一步 初始化

就按上图的格式进行初始化棋盘,注意棋盘的方向与数组下标,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void begining(){

//Blue
//underline character
mapp[1][1][0]=5,mapp[1][2][0]=4,mapp[1][3][0]=3,mapp[1][4][0]=2,mapp[1][5][0]=1;
mapp[1][6][0]=2,mapp[1][7][0]=3,mapp[1][8][0]=4,mapp[1][9][0]=5;
//duck
mapp[3][1][0]=6,mapp[3][9][0]=6;
//soldier
mapp[4][1][0]=7,mapp[4][3][0]=7,mapp[4][5][0]=7,mapp[4][7][0]=7,mapp[4][9][0]=7;

//Red
//underline character
mapp[10][1][1]=5,mapp[10][2][1]=4,mapp[10][3][1]=3,mapp[10][4][1]=2,mapp[10][5][1]=1;
mapp[10][6][1]=2,mapp[10][7][1]=3,mapp[10][8][1]=4,mapp[10][9][1]=5;
//duck
mapp[8][1][1]=6,mapp[8][9][1]=6;
//soldier
mapp[7][1][1]=7,mapp[7][3][1]=7,mapp[7][5][1]=7,mapp[7][7][1]=7;mapp[7][9][1]=7;

}

这里我调换了红蓝双方的顺序,所以就不用翻转棋盘。

第二步 基础操作函数:

①attack 移动/攻击

因为移动也是一种特殊的攻击,所以也可以把移动归入攻击一栏,通过from,to参数,考虑攻击前后的敌我关系,具体代码如下:

1
2
3
4
5
void attack(int x1,int y1,int x2,int y2,int from,int to){
mapp[x2][y2][from]=mapp[x1][y1][from];
mapp[x1][y1][from]=0;
if(to!=-1)mapp[x2][y2][to]=0;
}

②getgroup 分组

对于一个坐标的棋子,我们肯定要知道它属于哪一方,自然要写一个函数来获取分组。

1
2
3
4
5
int getgroup(int x,int y){
if(mapp[x][y][0])return 0;
else if(mapp[x][y][1])return 1;
else return -1;
}

③getcaptain 获取主帅位置

这一函数将在最后判断将军时用到。比较好理解。

1
2
3
4
5
6
7
8
9
10
11
Node getcaptain(int gr){
Node t;
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][gr]==1){
t.x=i,t.y=j;
return t;
}
}
}
}

第三步 判断每个棋子移动的合法性

这是整道题的重中之重,这里将一一讲解:

①captain 将的移动

这是最为简单的移动之一,只要求移动前后的坐标差即可。

1
2
3
4
5
6
bool captain(int x1,int y1,int x2,int y2,int from,int to){
int tx=abs(x1-x2),ty=abs(y1-y2);
if(tx==1&&ty==0)return true;
else if(tx==0&&ty==1)return true;
else return false;
}

②guard 士的移动

这个移动与将差不多,同样求坐标差

1
2
3
4
5
6
bool guard(int x1,int y1,int x2,int y2,int from,int to){
int tx=abs(x1-x2),ty=abs(y1-y2);
if(tx==1&&ty==1)
return true;
else return false;
}

③elephant 象的移动

这个移动就有点复杂,注意要判断是否移动受阻,可以单独开一个数组判断障碍,这个思路就有点像大家初学BFS的时候模拟移动。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool elephant(int x1,int y1,int x2,int y2,int from,int to){
int aimx[4]={-2,-2,2,2},aimy[4]={-2,2,-2,2};
int stx[4]={-1,-1,1,1,},sty[4]={-1,1,-1,1};
for(int i=0;i<4;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0){
return true;
}
}
}
return false;
}

④horse 马的移动

与象差不多,只要注意马的移动的x,y轴要一一对应,而障碍位置就是马的上下左右4个点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool horse(int x1,int y1,int x2,int y2,int from,int to){
int aimx[8]={-2,-2,-1,1,2,2,1,-1},aimy[8]={-1,1,2,2,1,-1,-2,-2};
int stx[8]={-1,-1,0,0,1,1,0,0},sty[8]={0,0,1,1,0,0,-1,-1};
for(int i=0;i<8;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0){
return true;
}
}
}
return false;
}

⑤car 车的移动

车的移动相对简单,只要一重循环模拟即可。注意一点:要先判断两点时候同行或者同列,否则模拟是无意义的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool car(int x1,int y1,int x2,int y2,int from,int to){
if(x1!=x2&&y1!=y2)return false;
if(x1==x2){
for(int i=min(y1,y2)+1;i<=max(y1,y2)-1;i++){
if(mapp[x1][i][0]||mapp[x1][i][1])
return false;
}
}
if(y1==y2){
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;i++){
if(mapp[i][y1][0]||mapp[i][y1][1]){
return false;
}
}
}
return true;
}

⑥duck 鸭的移动

鸭这个棋子是所有棋子中最难模拟的,但仔细分析,可以发现,鸭的障碍就是马对应方向的障碍与立足点,所以开2个数组去判断鸭的两层障碍即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool duck(int x1,int y1,int x2,int y2,int from,int to){
int aimx[8]={-3,-3,-2,2,3,3,2,-2},aimy[8]={-2,2,3,3,2,-2,-3,-3};
int stx[8]={-2,-2,-1,1,2,2,1,-1},sty[8]={-1,1,2,2,1,-1,-2,-2};
int subx[8]={-1,-1,0,0,1,1,0,0},suby[8]={0,0,1,1,0,0,-1,-1};
for(int i=0;i<8;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
int tttx=x1+subx[i],ttty=y1+suby[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0&&mapp[tttx][ttty][0]==0&&mapp[tttx][ttty][1]==0){
return true;
}
}
}
return false;
}

⑦soldier 兵的移动

仔细观察,不难发现,兵的移动就是将的移动加上士的移动。所以直接调用上面的函数即可。

1
2
3
4
5
bool soldier(int x1,int y1,int x2,int y2,int from,int to){
if(captain(x1,y1,x2,y2,from,to))return true;
if(guard(x1,y1,x2,y2,from,to))return true;
return false;
}

第三步 移动操作

有了之前的几个函数做铺垫,这个操作就好写很多,先判断是否符合题目中要求,比如同方,起始点无己方棋子等等,然后一一判断是哪种棋子,调用对应的函数即可。如果合法,那么就移动/对敌方棋子攻击,调用attack函数,否则输出无解,注意一点,在移动棋子前要先输出前两个信息,即移动的棋子和被攻击的棋子,因为在移动后信息会被改变。 最后输出后两个信息,在下文会讲到。

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
void move(int x1,int y1,int x2,int y2,int from,int to){
//nowgame();

if(from!=roun){
cout<<"Invalid command"<<endl;
return;
}
//check the size
if(x1<1||x2<1||x1>10||x2>10||y1<1||y2<1||y1>9||y2>9){
cout<<"Invalid command"<<endl;
return;
}

//check the gruops
if(from==to){
cout<<"Invalid command"<<endl;
return;
}

//check the from
if(from==-1){
cout<<"Invalid command"<<endl;
return;
}

//check if the game is over
if(flg==1){
cout<<"Invalid command"<<endl;
return;
}

//captain
if(mapp[x1][y1][from]==1){
if(captain(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//guard
if(mapp[x1][y1][from]==2){
if(guard(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//elephant
if(mapp[x1][y1][from]==3){
if(elephant(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//horse
if(mapp[x1][y1][from]==4){
if(horse(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//car
if(mapp[x1][y1][from]==5){
if(car(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//duck
if(mapp[x1][y1][from]==6){
if(duck(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//soldier
if(mapp[x1][y1][from]==7){
if(soldier(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

}

第四步 判断将军

将军这个问题看似很难解决,其实换一种思路即可,假设每个敌方棋子都可以将军,然后调用之前的函数,判断假设是否成立,如果成立,说明存在将军,否则则不存在将军。

这样一来,代码就非常好写了:遍历每个点,对于有敌方棋子的点,就做出假设,然后判断假设。并且这里要用到之前的getcaptain函数来获取两方将的位置。

代码如下:

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
bool cankill(){
Node blue,red;
blue=getcaptain(0),red=getcaptain(1);
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][0]){
if(mapp[i][j][0]==1)
if(captain(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==2)
if(guard(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==3)
if(elephant(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==4)
if(horse(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==5)
if(car(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==6)
if(duck(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==7)
if(soldier(i,j,red.x,red.y,0,1))
return true;
}
if(mapp[i][j][1]){
if(mapp[i][j][1]==1)
if(captain(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==2)
if(guard(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==3)
if(elephant(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==4)
if(horse(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==5)
if(car(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==6)
if(duck(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==7)
if(soldier(i,j,blue.x,blue.y,1,0))
return true;
}
}
}
return false;
}

第五步 判断游戏是否结束

这个问题非常好解决,跑一遍二重循环,统计将的个数,如果小于2,游戏一定结束,注意游戏结束后要设置一个flg,使对接下来命令的处理都是无解。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool isend(){

int t=0;
for(int k=0;k<=1;k++){
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][k]==1)t++;
}
}
}
if(t<2){
flg=1;
return true;
}
else return false;

}

第六步 输出

因为在move函数中已经将前两个信息输出,所以这里只要输出后两个信息即可,直接调用上面的函数,同时在输出的时候更新当前应当走棋的阵营。 对于不合法的调用,输出无解即可。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void print(int M_C,int M_G,int K_C,int K_G,int canmove){
//canmove?
if(canmove==1){
cout<<"Invalid command"<<endl;
return;
}
roun=1-roun;

//End?
if(isend()){
cout<<"no;yes"<<endl;
}
else
{
if(cankill())cout<<"yes;";
else cout<<"no;";
cout<<"no"<<endl;
}
}

Tip:主程序中对于每条指令,调用move函数即可。

完整代码

献上完整代码,10K,396行:

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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include<bits/stdc++.h>
using namespace std;

//begining words
int Q;
struct Node{
int x,y;
};
int mapp[20][20][2];
string name[10]={"","captain","guard","elephant","horse","car","duck","soldier"};
string group[3]={"red","blue"};
bool flg=0;
bool roun=0;

void begining(){

//Blue
//underline character
mapp[1][1][0]=5,mapp[1][2][0]=4,mapp[1][3][0]=3,mapp[1][4][0]=2,mapp[1][5][0]=1;
mapp[1][6][0]=2,mapp[1][7][0]=3,mapp[1][8][0]=4,mapp[1][9][0]=5;
//duck
mapp[3][1][0]=6,mapp[3][9][0]=6;
//soldier
mapp[4][1][0]=7,mapp[4][3][0]=7,mapp[4][5][0]=7,mapp[4][7][0]=7,mapp[4][9][0]=7;

//Red
//underline character
mapp[10][1][1]=5,mapp[10][2][1]=4,mapp[10][3][1]=3,mapp[10][4][1]=2,mapp[10][5][1]=1;
mapp[10][6][1]=2,mapp[10][7][1]=3,mapp[10][8][1]=4,mapp[10][9][1]=5;
//duck
mapp[8][1][1]=6,mapp[8][9][1]=6;
//soldier
mapp[7][1][1]=7,mapp[7][3][1]=7,mapp[7][5][1]=7,mapp[7][7][1]=7;mapp[7][9][1]=7;

}

void attack(int x1,int y1,int x2,int y2,int from,int to){
mapp[x2][y2][from]=mapp[x1][y1][from];
mapp[x1][y1][from]=0;
if(to!=-1)mapp[x2][y2][to]=0;
}

int getgroup(int x,int y){
if(mapp[x][y][0])return 0;
else if(mapp[x][y][1])return 1;
else return -1;
}

Node getcaptain(int gr){
Node t;
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][gr]==1){
t.x=i,t.y=j;
return t;
}
}
}
}

bool captain(int x1,int y1,int x2,int y2,int from,int to){
int tx=abs(x1-x2),ty=abs(y1-y2);
if(tx==1&&ty==0)return true;
else if(tx==0&&ty==1)return true;
else return false;
}

bool guard(int x1,int y1,int x2,int y2,int from,int to){
int tx=abs(x1-x2),ty=abs(y1-y2);
if(tx==1&&ty==1)
return true;
else return false;
}

bool elephant(int x1,int y1,int x2,int y2,int from,int to){
int aimx[4]={-2,-2,2,2},aimy[4]={-2,2,-2,2};
int stx[4]={-1,-1,1,1,},sty[4]={-1,1,-1,1};
for(int i=0;i<4;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0){
return true;
}
}
}
return false;
}

bool horse(int x1,int y1,int x2,int y2,int from,int to){
int aimx[8]={-2,-2,-1,1,2,2,1,-1},aimy[8]={-1,1,2,2,1,-1,-2,-2};
int stx[8]={-1,-1,0,0,1,1,0,0},sty[8]={0,0,1,1,0,0,-1,-1};
for(int i=0;i<8;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0){
return true;
}
}
}
return false;
}

bool car(int x1,int y1,int x2,int y2,int from,int to){
if(x1!=x2&&y1!=y2)return false;
if(x1==x2){
for(int i=min(y1,y2)+1;i<=max(y1,y2)-1;i++){
if(mapp[x1][i][0]||mapp[x1][i][1])
return false;
}
}
if(y1==y2){
for(int i=min(x1,x2)+1;i<=max(x1,x2)-1;i++){
if(mapp[i][y1][0]||mapp[i][y1][1]){
return false;
}
}
}
return true;
}

bool duck(int x1,int y1,int x2,int y2,int from,int to){
int aimx[8]={-3,-3,-2,2,3,3,2,-2},aimy[8]={-2,2,3,3,2,-2,-3,-3};
int stx[8]={-2,-2,-1,1,2,2,1,-1},sty[8]={-1,1,2,2,1,-1,-2,-2};
int subx[8]={-1,-1,0,0,1,1,0,0},suby[8]={0,0,1,1,0,0,-1,-1};
for(int i=0;i<8;i++){
int tx=x1+aimx[i],ty=y1+aimy[i];
if(tx==x2&&ty==y2){
int ttx=x1+stx[i],tty=y1+sty[i];
int tttx=x1+subx[i],ttty=y1+suby[i];
if(mapp[ttx][tty][0]==0&&mapp[ttx][tty][1]==0&&mapp[tttx][ttty][0]==0&&mapp[tttx][ttty][1]==0){
return true;
}
}
}
return false;
}

bool soldier(int x1,int y1,int x2,int y2,int from,int to){
if(captain(x1,y1,x2,y2,from,to))return true;
if(guard(x1,y1,x2,y2,from,to))return true;
return false;
}

bool cankill(){
Node blue,red;
blue=getcaptain(0),red=getcaptain(1);
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][0]){
if(mapp[i][j][0]==1)
if(captain(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==2)
if(guard(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==3)
if(elephant(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==4)
if(horse(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==5)
if(car(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==6)
if(duck(i,j,red.x,red.y,0,1))
return true;
if(mapp[i][j][0]==7)
if(soldier(i,j,red.x,red.y,0,1))
return true;
}
if(mapp[i][j][1]){
if(mapp[i][j][1]==1)
if(captain(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==2)
if(guard(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==3)
if(elephant(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==4)
if(horse(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==5)
if(car(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==6)
if(duck(i,j,blue.x,blue.y,1,0))
return true;
if(mapp[i][j][1]==7)
if(soldier(i,j,blue.x,blue.y,1,0))
return true;
}
}
}
return false;
}

bool isend(){

int t=0;
for(int k=0;k<=1;k++){
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(mapp[i][j][k]==1)t++;
}
}
}
if(t<2){
flg=1;
return true;
}
else return false;

}

void print(int M_C,int M_G,int K_C,int K_G,int canmove){
//canmove?
if(canmove==1){
cout<<"Invalid command"<<endl;
return;
}
roun=1-roun;

//End?
if(isend()){
cout<<"no;yes"<<endl;
}
else
{
if(cankill())cout<<"yes;";
else cout<<"no;";
cout<<"no"<<endl;
}
}

void move(int x1,int y1,int x2,int y2,int from,int to){
//nowgame();

if(from!=roun){
cout<<"Invalid command"<<endl;
return;
}
//check the size
if(x1<1||x2<1||x1>10||x2>10||y1<1||y2<1||y1>9||y2>9){
cout<<"Invalid command"<<endl;
return;
}

//check the gruops
if(from==to){
cout<<"Invalid command"<<endl;
return;
}

//check the from
if(from==-1){
cout<<"Invalid command"<<endl;
return;
}

//check if the game is over
if(flg==1){
cout<<"Invalid command"<<endl;
return;
}

//captain
if(mapp[x1][y1][from]==1){
if(captain(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//guard
if(mapp[x1][y1][from]==2){
if(guard(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//elephant
if(mapp[x1][y1][from]==3){
if(elephant(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//horse
if(mapp[x1][y1][from]==4){
if(horse(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//car
if(mapp[x1][y1][from]==5){
if(car(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//duck
if(mapp[x1][y1][from]==6){
if(duck(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

//soldier
if(mapp[x1][y1][from]==7){
if(soldier(x1,y1,x2,y2,from,to)){
int ta=mapp[x1][y1][from];
int tb=mapp[x2][y2][to];
cout<<group[from]<<' '<<name[ta]<<';';
if(to==-1)cout<<"NA;";
else cout<<group[to]<<' '<<name[tb]<<';';
attack(x1,y1,x2,y2,from,to);
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,0);
}

else
print(mapp[x1][y1][from],from,mapp[x2][y2][to],to,1);
}

}

int main(){
begining();
cin>>Q;
for(int i=1;i<=Q;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
x1++,y1++,x2++,y2++;
move(x1,y1,x2,y2,getgroup(x1,y1),getgroup(x2,y2));
}

return 0;
}

评论