//一个棋盘搜索题,比较简单
1 #include<stdio.h>
2 char a[10][10],g,w;
3 int b[10][10];
4 int c[4][2]={0,-1,0,+1,-1,0,1,0};
5 int e[4][2]={1,-1,1,1,-1,-1,-1,1};
6 int z[8][2]={0,-1,0,+1,-1,0,1,0,1,-1,1,1,-1,-1,-1,1};
7 int j[8][2]={-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2};
8 int x1,y1;
9 int fp(int x,int y,int f)
10 {
11 int i,t;
12 if(f==0)
13 {
14 g='P';
15 t=0;
16 }
17 else
18 {
19 g='p';
20 t=2;
21 }
22 for(i=t;i<t+2;i++)
23 {
24 x1=x;
25 y1=y;
26 while((x1+e[i][0])>=0&&(x1+e[i][0])<8&&(y1+e[i][1])>=0&&(y1+e[i][1])<8)
27 {
28 if(a[x1+e[i][0]][y1+e[i][1]]==g)
29 return 1;
30 else
31 break;
32 }
33 }
34 if(i>=t+2)
35 return 0;
36 else
37 return 1;
38 }
39 int fn(int x,int y,int f)
40 {
41 if(f==0)
42 g='N';
43 else
44 g='n';
45 int i;
46 for(i=0;i<8;i++)
47 {
48 x1=x;
49 y1=y;
50 while((x1+j[i][0])>=0&&(x1+j[i][0])<8&&(y1+j[i][1])>=0&&(y1+j[i][1])<8)
51 {
52 if(a[x1+j[i][0]][y1+j[i][1]]==g)
53 return 1;
54 else
55 break;
56 }
57 }
58 if(i>=8)
59 return 0;
60 else
61 return 1;
62
63 }
64 int fr(int x,int y,int f)
65 {
66 if(f==0)
67 {
68 g='R';
69 w='Q';
70 }
71 else
72 {
73 g='r';
74 w='q';
75 }
76 int i;
77 for(i=0;i<4;i++)
78 {
79 x1=x;
80 y1=y;
81 while((x1+c[i][0])>=0&&(x1+c[i][0])<8&&(y1+c[i][1])>=0&&(y1+c[i][1])<8)
82 {
83 if(a[x1+c[i][0]][y1+c[i][1]]==g||a[x1+c[i][0]][y1+c[i][1]]==w)
84 return 1;
85 else if(b[x1+c[i][0]][y1+c[i][1]])
86 {
87 x1=x1+c[i][0];
88 y1=y1+c[i][1];
89 }
90 else
91 break;
92 }
93 }
94 if(i>=4)
95 return 0;
96 else
97 return 1;
98
99 }
100 int fb(int x,int y,int f)
101 {
102 if(f==0)
103 {
104 g='B';
105 w='Q';
106 }
107 else
108 {
109 g='b';
110 w='q';
111 }
112 int i;
113 for(i=0;i<4;i++)
114 {
115 x1=x;
116 y1=y;
117 while((x1+e[i][0])>=0&&(x1+e[i][0])<8&&(y1+e[i][1])>=0&&(y1+e[i][1])<8)
118 {
119 if(a[x1+e[i][0]][y1+e[i][1]]==g||a[x1+e[i][0]][y1+e[i][1]]==w)
120 return 1;
121 else if(b[x1+e[i][0]][y1+e[i][1]])
122 {
123 x1=x1+e[i][0];
124 y1=y1+e[i][1];
125 }
126 else
127 break;
128 }
129 }
130 if(i>=4)
131 return 0;
132 else
133 return 1;
134 }
135 int fq(int x,int y,int f)
136 {
137 if(f==0)
138 g='Q';
139 else
140 g='q';
141 int i;
142 for(i=0;i<8;i++)
143 {
144 x1=x;
145 y1=y;
146 while((x1+z[i][0])>=0&&(x1+z[i][0])<8&&(y1+z[i][1])>=0&&(y1+z[i][1])<8)
147 {
148 if(a[x1+z[i][0]][y1+z[i][1]]==g)
149 return 1;
150 if(b[x1+z[i][0]][y1+z[i][1]])
151 {
152 x1=x1+z[i][0];
153 y1=y1+z[i][1];
154 }
155 else
156 break;
157 }
158 }
159 if(i>=8)
160 return 0;
161 else
162 return 1;
163 }
164 int main()
165 {
166 int i,j,num,t=0,x0,y0,x,y;
167 while(1)
168 {
169 num=0;
170 for(i=0;i<8;i++)
171 {
172 for(j=0;j<8;j++)
173 {
174 scanf("%c",&a[i][j]);
175 if(a[i][j]=='.')
176 {
177 num++;
178 b[i][j]=1;
179 }
180 else if(a[i][j]=='k')
181 {
182 x0=i;
183 y0=j;
184 b[i][j]=0;
185
186 }
187 else if(a[i][j]=='K')
188 {
189 x=i;
190 y=j;
191 b[i][j]=0;
192 }
193 else
194 b[i][j]=0;
195 }
196 getchar();
197 }
198 getchar();
199 if(num==64)
200 return 0;
201 t++;
202 printf("Game #%d: ",t);
203 if(fp(x0,y0,0)||fn(x0,y0,0)||fb(x0,y0,0)||fr(x0,y0,0))
204 printf("black king is in check.\n");
205 else if(fp(x,y,1)||fn(x,y,1)||fb(x,y,1)||fr(x,y,1))
206 printf("white king is in check.\n");
207 else
208 printf("no king is in check.\n");
209
210 }
211 return 0;
212 }
213