寒假的ACM训练三(PC110107/UVa10196)

时间:2023-03-08 18:19:06
#include <iostream>
#include <string.h> using namespace std;
char qp[10][10];
int result; //结果
int black_k_x; //将军的x,y
int black_k_y;
int white_k_x;
int white_k_y;
int abs(int i){
if(i<0)
return -i;
return i;
}
//马
bool n_check(int n_x,int n_y,int k_x,int k_y){ if(abs(n_x-k_x)==2&&abs(n_y-k_y)==1)
return true;
if(abs(n_x-k_x)==1&&abs(n_y-k_y)==2)
return true; return false; }
//兵
bool p_check(int p_x,int p_y,int k_x,int k_y){
if(qp[p_x][p_y]=='p'){
if(p_x+1==k_x&&abs(p_y-k_y)==1)
return true;
}
else if(qp[p_x][p_y]=='P'){
if(p_x-1==k_x&&abs(p_y-k_y)==1)
return true;
}
return false; }
//车
bool r_check(int r_x,int r_y,int k_x,int k_y){
int i;
if(r_x==k_x){ //同横
if(k_y>r_y){ //r_y 比较小
for(i=r_y+1;i<k_y;i++)
if(qp[r_x][i]!='.')
break;
if(i==k_y)
return true;
}
if(k_y<r_y){ for(i=k_y+1;i<r_y;i++)
if(qp[r_x][i]!='.')
break; if(i==r_y)
return true; } }
if(r_y==k_y){ //同列
if(k_x>r_x){ //r_x比较小
for(i=r_x+1;i<k_x;i++)
if(qp[i][k_y]!='.')
break;
if(i==k_x)
return true;
}
if(k_x<r_x){ //k_x 比较小 for(i=k_x+1;i<r_x;i++)
if(qp[i][k_y]!='.')
break; if(i==r_x)
return true;
} }
return false; }
//象
bool b_check(int b_x,int b_y,int k_x,int k_y){
int i,j;
//x差和y差相同
if(abs(b_x-k_x)==abs(b_y-k_y)){ //xy同增
i=b_x;j=b_y;
while(i<k_x&&j<k_y){
i++;j++;
if(qp[i][j]!='.')
break;
} //x减y增
while(i>k_x&&j<k_y){ i--;j++;
if(qp[i][j]!='.')
break;
} //x增y减
while(i<k_x&&j>k_y){ i++;j--;
if(qp[i][j]!='.')
break;
} //x减y减
while(i>k_x&&j>k_y){
i--;j--;
if(qp[i][j]!='.')
break;
} if(i==k_x&&j==k_y)
return true; }
return false; }
//后
bool q_check(int q_x,int q_y,int k_x,int k_y){
return r_check(q_x,q_y,k_x,k_y)||b_check(q_x,q_y,k_x,k_y); }
void check(){ bool f;
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++){ if(qp[i][j]=='p'){
f=(p_check(i,j,white_k_x,white_k_y)); }else if(qp[i][j]=='n'){
f=(n_check(i,j,white_k_x,white_k_y)); } else if(qp[i][j]=='q'){
f=(q_check(i,j,white_k_x,white_k_y)); }else if(qp[i][j]=='r'){
f=(r_check(i,j,white_k_x,white_k_y)); }else if(qp[i][j]=='b'){
f=(b_check(i,j,white_k_x,white_k_y)); } if(f==true){ result=1;
return;
} if(qp[i][j]=='P'){
f=(p_check(i,j,black_k_x,black_k_y)); }else if(qp[i][j]=='N'){
f=(n_check(i,j,black_k_x,black_k_y)); }else if(qp[i][j]=='Q'){
f=(q_check(i,j,black_k_x,black_k_y)); }else if(qp[i][j]=='R'){
f=(r_check(i,j,black_k_x,black_k_y)); }else if(qp[i][j]=='B'){
f=(b_check(i,j,black_k_x,black_k_y));
}
if(f==true){ result=0;
return;
} }
result=2; } int main()
{
int run=1; while(true){
bool flag=true; //退出标志 memset(qp,'.',sizeof(qp));
for(int i=1;i<=8;i++)
for(int j=1;j<=8;j++){
cin>>qp[i][j];
if(flag&&qp[i][j]!='.')
flag=false;
if(qp[i][j]=='k'){black_k_x=i;black_k_y=j;}
if(qp[i][j]=='K'){white_k_x=i;white_k_y=j;} }
if(flag) break; //空盘退出 check(); //读盘 if(result==0) //结果为0
cout << "Game #"<<run<<": black king is in check." << endl;
else if(result==1)
cout << "Game #"<<run<<": white king is in check." <<endl;
else
cout << "Game #"<<run<<": no king is in check."<<endl; run++; }
return 0;
}