简单模拟题,代码长了点,不过许多都是copy前面的然后稍微修改一下。
一个小错误导致了2Y。
以下是我的代码:
#include<cstdio>
using namespace std;
const int nx[]={-2,-2,-1,1,2,2,1,-1},ny[]={-1,1,2,2,1,-1,-2,-2};
const int bx[]={-1,-1,1,1},by[]={-1,1,-1,1};
const int rx[]={-1,0,1,0},ry[]={0,1,0,-1};
char r[10][10];
bool End()
{
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
if(r[i][j]!='.')
return false;
return true;
}
void Solve()
{
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
if(r[i][j]>='a' && r[i][j]<='z')
{
if(r[i][j]=='p')
{
if((i+1<=8 && j-1>=1 && r[i+1][j-1]=='K') ||
(i+1<=8 && j+1<=8 && r[i+1][j+1]=='K'))
{
printf("white king is in check.\n");
return;
}
}
else if(r[i][j]=='n')
{
for(int k=0;k<8;k++)
{
int ii(i+nx[k]),jj(j+ny[k]);
if(ii>=1 && ii<=8 && jj>=1 && jj<=8 && r[ii][jj]=='K')
{
printf("white king is in check.\n");
return;
}
}
}
else if(r[i][j]=='b')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='K')
{
printf("white king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+bx[k];
jj=jj+by[k];
}
}
}
else if(r[i][j]=='r')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='K')
{
printf("white king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+rx[k];
jj=jj+ry[k];
}
}
}
else if(r[i][j]=='q')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='K')
{
printf("white king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+rx[k];
jj=jj+ry[k];
}
}
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='K')
{
printf("white king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+bx[k];
jj=jj+by[k];
}
}
}
}
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++)
if(r[i][j]>='A' && r[i][j]<='Z')
{
if(r[i][j]=='P')
{
if((i-1>=1 && j-1>=1 && r[i-1][j-1]=='k') ||
(i-1>=1 && j+1<=8 && r[i-1][j+1]=='k'))
{
printf("black king is in check.\n");
return;
}
}
else if(r[i][j]=='N')
{
for(int k=0;k<8;k++)
{
int ii(i+nx[k]),jj(j+ny[k]);
if(ii>=1 && ii<=8 && jj>=1 && jj<=8 && r[ii][jj]=='k')
{
printf("black king is in check.\n");
return;
}
}
}
else if(r[i][j]=='B')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='k')
{
printf("black king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+bx[k];
jj=jj+by[k];
}
}
}
else if(r[i][j]=='R')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='k')
{
printf("black king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+rx[k];
jj=jj+ry[k];
}
}
}
else if(r[i][j]=='Q')
{
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='k')
{
printf("black king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+rx[k];
jj=jj+ry[k];
}
}
for(int k=0;k<4;k++)
{
int ii(i),jj(j);
while(ii>=1 && ii<=8 && jj>=1 && jj<=8)
{
if(r[ii][jj]=='k')
{
printf("black king is in check.\n");
return;
}
else if(!(ii==i && jj==j) && r[ii][jj]!='.')
break;
ii=ii+bx[k];
jj=jj+by[k];
}
}
}
}
printf("no king is in check.\n");
}
int main()
{
int T(0);
while(true)
{
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
scanf("%c",&r[i][j]);
getchar();
}
getchar();
if(End())
break;
// Input
T++;
printf("Game #%d: ",T);
Solve();
}
return 0;
}
posted on 2011-08-29 08:06
lee1r 阅读(322)
评论(0) 编辑 收藏 引用 所属分类:
题目分类:基础/模拟