Friday, May 13, 2016

UVA 10196 -Check the Check : Solution

Unknown
#include<cstdio>
#include<string.h>
using namespace std;

char board[8][9];
int check(int wi, int wj,int bi, int bj)
{
     int i,j;
     i=wi;
     j=wj;
     while (--j>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='r')  return 1;
      else break;
     }
     j=wj;
     while (++j<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='r')  return 1;
      else break;
     }
     j=wj;
     while (--i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='r')  return 1;
      else break;
     }
     i=wi;
     while (++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='r')  return 1;
      else break;
     }
     i=wi;
     while (--j>=0 && --i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='b')  return 1;
      else break;
     }
     i=wi;
     j=wj;
     while (--j>=0 && ++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='b')  return 1;
      else break;
     }
     i=wi;
     j=wj;
     while (++j<8 && --i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='b')  return 1;
      else break;
     }
     i=wi;
     j=wj;
     while (++j<8 && ++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='q' || board[i][j]=='b')  return 1;
      else break;
     }
     i=wi-1;
     j=wj;
     if (i>=0)
     {
      if (j-1>=0 && board[i][j-1]=='p') return 1;
      if (j+1<8 && board[i][j+1]=='p') return 1;
     }
     i++;
     if (i-2>=0)
     {
      if (j-1>=0 && board[i-2][j-1]=='n') return 1;
      if (j+1<8 && board[i-2][j+1]=='n') return 1;
     }
     if (i+2<8)
     {
      if (j-1>=0 && board[i+2][j-1]=='n') return 1;
      if (j+1<8 && board[i+2][j+1]=='n') return 1;
     }
     if (j-2>=0)
     {
      if (i-1>=0 && board[i-1][j-2]=='n') return 1;
      if (i+1<8 && board[i+1][j-2]=='n') return 1;
     }
     if (j+2<8)
     {
      if (i-1>=0 && board[i-1][j+2]=='n') return 1;
      if (i+1<8 && board[i+1][j+2]=='n') return 1;
     }
     // black king check checking
     i=bi;
     j=bj;
     while (--j>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='R')  return 2;
      else break;
     }
     j=bj;
     while (++j<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='R')  return 2;
      else break;
     }
     j=bj;
     while (--i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='R')  return 2;
      else break;
     }
     i=bi;
     while (++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='R')  return 2;
      else break;
     }
     i=bi;
     while (--j>=0 && --i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='B')  return 2;
      else break;
     }
     i=bi;
     j=bj;
     while (--j>=0 && ++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='B')  return 2;
      else break;
     }
     i=bi;
     j=bj;
     while (++j<8 && --i>=0)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='B')  return 2;
      else break;
     }
     i=bi;
     j=bj;
     while (++j<8 && ++i<8)
     {
      if (board[i][j]=='.') continue;
      if (board[i][j]=='Q' || board[i][j]=='B')  return 2;
      else break;
     }
     i=bi+1;
     j=bj;
     if (i<8)
     {
      if (j-1>=0 && board[i][j-1]=='P') return 2;
      if (j+1<8 && board[i][j+1]=='P') return 2;
     }
     i--;
     if (i-2>=0)
     {
      if (j-1>=0 && board[i-2][j-1]=='N') return 2;
      if (j+1<8 && board[i-2][j+1]=='N') return 2;
     }
     if (i+2<8)
     {
      if (j-1>=0 && board[i+2][j-1]=='N') return 2;
      if (j+1<8 && board[i+2][j+1]=='N') return 2;
     }
     if (j-2>=0)
     {
      if (i-1>=0 && board[i-1][j-2]=='N') return 2;
      if (i+1<8 && board[i+1][j-2]=='N') return 2;
     }
     if (j+2<8)
     {
      if (i-1>=0 && board[i-1][j+2]=='N') return 2;
      if (i+1<8 && board[i+1][j+2]=='N') return 2;
     }
     return 0;
     
    }

    int main()
    {
     int test=1;
     while (1)
     {
      for (int i=0;i<8;i++)
        scanf("%s",board[i]);
      int wi,wj,bi,bj,blank=0;
      for (int i=0;i<8;i++)
       for (int j=0;j<8;j++)
       {
        if (board[i][j]=='.') blank++;
        else if (board[i][j]=='K')
        {
         wi=i;
         wj=j;
        }
        else if (board[i][j]=='k')
        {
         bi=i;
         bj=j;
        }
       }
      if (blank==64) break;
      int c=check(wi,wj,bi,bj);
      if (c==1) printf("Game #%d: white king is in check.\n",test++);
      else if (c==2) printf("Game #%d: black king is in check.\n",test++);
      else printf("Game #%d: no king is in check.\n",test++);
     }
     return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,