【OJ】1.6.7将军(Check the Check)UVa 10196 // PC 1101017 // acmclub.com 25177

简介: /* 1.6.7将军(Check the Check)UVa 10196 // PC 1101017 // hzu.acmclub.com 25177*/#include#includeusing namespace std;char a[10][10]...
/*
 1.6.7将军(Check the Check)UVa 10196 // PC 1101017 //  hzu.acmclub.com 25177
*/

#include<iostream>
#include<stdio.h>
using namespace std;
char a[10][10];
int x,y,X,Y;  int white=0,black=0;

/////////////////////////////////blackk
void che1(int x,int y){
 if(y<=8&&(a[x][y]=='R'||a[x][y]=='Q')){black=1;}////
 else if(y<=7&&a[x][y]=='.')che1(x,y+1);
}
void che2(int x,int y){
 if(y>=0&&(a[x][y]=='R'||a[x][y]=='Q')){black=1;}////
 else if(y>=1&&a[x][y]=='.')che2(x,y-1);
}
void che3(int x,int y){
 if(x<=8&&(a[x][y]=='R'||a[x][y]=='Q')){black=1;}////
 else if(x<=7&&a[x][y]=='.')che3(x+1,y);
}
void che4(int x,int y){
 if(x>=0&&(a[x][y]=='R'||a[x][y]=='Q')){black=1;}////
 else if(x>=1&&a[x][y]=='.')che4(x-1,y);
}
//////////////////////////
void xiang1(int x,int y){
 if(x<=8&&y<=8&&(a[x][y]=='B'||a[x][y]=='Q'))black=1;
 else if(x<=7&&y<=7&&(a[x][y]=='.'))xiang1(x+1,y+1);
}
void xiang2(int x,int y){
 if(x<=8&&y>=0&&(a[x][y]=='B'||a[x][y]=='Q'))black=1;
 else if(x<=7&&y>=1&&(a[x][y]=='.'))xiang2(x+1,y-1);
}
void xiang3(int x,int y){
 if(x>=0&&y<=8&&(a[x][y]=='B'||a[x][y]=='Q'))black=1;
 else if(x>=1&&y<=7&&(a[x][y]=='.'))xiang3(x-1,y+1);
}
void xiang4(int x,int y){
 if(x>=0&&y>=0&&(a[x][y]=='B'||a[x][y]=='Q'))black=1;///->1未改
 else if(x>=1&&y>=1&&(a[x][y]=='.'))xiang4(x-1,y-1);///->2
}
//////////////////////////////////


/////////////////////////////////white
void che11(int X,int Y){
 if(Y<=8&&(a[X][Y]=='r'||a[X][Y]=='q')){white=1;}////
 else if(Y<=7&&a[X][Y]=='.')che11(X,Y+1);
}
void che22(int X,int Y){
 if(Y>=0&&(a[X][Y]=='r'||a[X][Y]=='q')){white=1;}////
 else if(Y>=1&&a[X][Y]=='.')che22(X,Y-1);
}
void che33(int X,int Y){
 if(X<=8&&(a[X][Y]=='r'||a[X][Y]=='q')){white=1;}////
 else if(X<=7&&a[X][Y]=='.')che33(X+1,Y);
}
void che44(int X,int Y){
 if(X>=0&&(a[X][Y]=='r'||a[X][Y]=='q')){white=1;}////
 else if(X>=1&&a[X][Y]=='.')che44(X-1,Y);
}
//////////////////////////
void xiang11(int X,int Y){
 if(X<=8&&Y<=8&&(a[X][Y]=='b'||a[X][Y]=='q'))white=1;
 else if(X<=7&&Y<=7&&(a[X][Y]=='.'))xiang11(X+1,Y+1);
}
void xiang22(int X,int Y){
 if(X<=8&&Y>=0&&(a[X][Y]=='b'||a[X][Y]=='q'))white=1;
 else if(X<=7&&Y>=1&&(a[X][Y]=='.'))xiang22(X+1,Y-1);
}
void xiang33(int X,int Y){
 if(X>=0&&Y<=8&&(a[X][Y]=='b'||a[X][Y]=='q'))white=1;
 else if(X>=1&&Y<=7&&(a[X][Y]=='.'))xiang33(X-1,Y+1);
}
void xiang44(int X,int Y){
 if(X>=0&&Y>=0&&(a[X][Y]=='b'||a[X][Y]=='q'))white=1;///->1未改
 else if(X>=1&&Y>=1&&(a[X][Y]=='.'))xiang44(X-1,Y-1);///->2
}
//////////////////////////////////

 


int main(){
 
// freopen("1.6.7input.txt","r",stdin);
// freopen("1.6.7output.txt","w",stdout);
 int flag=1,flag1=1,flag2=1,num=0;

//input1
 for(int i=1;i<=8;i++){
  for(int j=1;j<=8;j++){
   a[i][j]=getchar();
   while(flag){if(a[i][j]!='.')flag=0;break;}
   while(flag1){if(a[i][j]=='k'){x=i;y=j;flag1=0;}break;}
   while(flag2){if(a[i][j]=='K'){X=i;Y=j;flag2=0;}break;}
  }
  getchar();
 }
//mainfunc 
 while(!flag){
  
 getchar();///////////空行
 
  num++;flag=flag1=flag2=1;
   white=0,black=0;
  while(1){
   
////////////////////////////////////////BLACK

   if(a[x+1][y+1]=='P'&&x+1<=8&&y+1<=8||a[x+1][y-1]=='P'&&x+1<=8&&y-1>=1){black=1;break;}//卒

      che1(x,y+1);if(black==1)break;//有皇后
      che2(x,y-1);if(black==1)break;
      che3(x+1,y);if(black==1)break;
      che4(x-1,y);if(black==1)break;

   xiang1(x+1,y+1);if(black==1)break; //有皇后     
   xiang2(x+1,y-1);if(black==1)break; //有皇后     
   xiang3(x-1,y+1);if(black==1)break; //有皇后     
   xiang4(x-1,y-1);if(black==1)break; //有皇后     
//   
   if(a[x+2][y+1]=='N'&&x+2<=8&&y+1<=8||
   a[x+1][y+2]=='N'&&x+1<=8&&y+2<=8||
   a[x-1][y-2]=='N'&&x-1>=1&&y-2>=1||
   a[x-2][y-1]=='N'&&x-2>=1&&y-1>=1||
   a[x+1][y-2]=='N'&&x+1<=8&&y-2>=1||
   a[x+2][y-1]=='N'&&x+2<=8&&y-1>=1||
   a[x-1][y+2]=='N'&&x-1>=1&&y+2<=8||
   a[x-2][y+1]=='N'&&x-2>=1&&y+1<=8   
   ) {black=1;break;}//马
   
   if(a[x+1][y+1]=='K'&&x+1<=8&&y+1<=8||
   a[x-1][y-1]=='K'&&x-1>=1&&y-1>0||
   a[x+1][y-1]=='K'&&x+1<=8&&y+1>0||
   a[x-1][y+1]=='K'&&x+1>0&&y+1<=8||
   a[x][y+1]=='K'&&y+1<=8||
   a[x+1][y]=='K'&&x+1<=8||
   a[x-1][y]=='K'&&x-1>=1||
   a[x][y-1]=='K'&&y-1>=1   
   ) {black=1;break;}//王


/////////////////////////////////////////////WHITE
//卒上
   if(a[X-1][Y-1]=='p'&&X-1>=1&&Y-1>=1||a[X-1][Y+1]=='p'&&Y+1<=8&&X-1>=1){white=1;break;}//卒

      che11(X,Y+1);if(white==1)break;//有皇后
      che22(X,Y-1);if(white==1)break;
      che33(X+1,Y);if(white==1)break;
      che44(X-1,Y);if(white==1)break;

   xiang11(X+1,Y+1);if(white==1)break; //有皇后     
   xiang22(X+1,Y-1);if(white==1)break; //有皇后     
   xiang33(X-1,Y+1);if(white==1)break; //有皇后     
   xiang44(X-1,Y-1);if(white==1)break; //有皇后     
//   
   if(a[X+2][Y+1]=='n'&&X+2<=8&&Y+1<=8||
   a[X+1][Y+2]=='n'&&X+1<=8&&Y+2<=8||
   a[X-1][Y-2]=='n'&&X-1>=1&&Y-2>=1||
   a[X-2][Y-1]=='n'&&X-2>=1&&Y-1>=1||
   a[X+1][Y-2]=='n'&&X+1<=8&&Y-2>=1||
   a[X+2][Y-1]=='n'&&X+2<=8&&Y-1>=1||
   a[X-1][Y+2]=='n'&&X-1>=1&&Y+2<=8||
   a[X-2][Y+1]=='n'&&X-2>=1&&Y+1<=8   
   ) {white=1;break;}//马
   
   if(a[X+1][Y+1]=='k'&&X+1<=8&&Y+1<=8||
   a[X-1][Y-1]=='k'&&X-1>=1&&Y-1>0||
   a[X+1][Y-1]=='k'&&X+1<=8&&Y+1>0||
   a[X-1][Y+1]=='k'&&X+1>0&&Y+1<=8||
   a[X][Y+1]=='k'&&Y+1<=8||
   a[X+1][Y]=='k'&&X+1<=8||
   a[X-1][Y]=='k'&&X-1>=1||
   a[X][Y-1]=='k'&&Y-1>=1   
   ) {white=1;break;}//王

 

   break;  
  }
//output  
  if(black)cout<<"Game #"<<num<<": black king is in check.\n";
  else if(white)cout<<"Game #"<<num<<": white king is in check.\n";
  else cout<<"Game #"<<num<<": no king is in check.\n";

//input
  for(int i=1;i<=8;i++){
  for(int j=1;j<=8;j++){
   a[i][j]=getchar();
   while(flag){if(a[i][j]!='.')flag=0;break;}
   while(flag1){if(a[i][j]=='k'){x=i;y=j;flag1=0;}break;}
   while(flag2){if(a[i][j]=='K'){X=i;Y=j;flag2=0;}break;}
  }
  getchar();
  }
 
 }
 return 0;
}


目录
相关文章
|
3月前
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
【bug记录】旋转链表与力扣报错:member access within null pointer of type ‘struct ListNode‘
每日一题---27. 移除元素[力扣][Go]
每日一题---27. 移除元素[力扣][Go]
每日一题---27. 移除元素[力扣][Go]
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
C/C++零散知识点汇总之break、continue、goto、return和exit
C/C++零散知识点汇总之break、continue、goto、return和exit
【1075】PAT Judge (25 分)
【1075】PAT Judge (25 分) 【1075】PAT Judge (25 分)
110 0