10196 - Check The Check

All about problems in Volume 101. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Arashk_kh68
New poster
Posts: 6
Joined: Sun Sep 09, 2007 3:56 pm

Post by Arashk_kh68 » Thu Sep 20, 2007 9:11 am

Hi Dear ACMers,
I have coded this two weeks ago and I still get WA for it.
I cant figure out where is the problem, because I got right answer for every testcases i have tested.

Coded in C++:

Code: Select all

// CheckTheCheck.cpp : Defines the entry point for the console application. 
// UVA 10196 

//#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 

char board[8][8]; 
int caseNum = 1; 
int status = 0; // 0 = no king in check.  1 = Black king in check. 2 = White king in check 

bool checkTheCheck(int row, int col) { 
   switch (board[row][col]) { 
      case 'k': // Black king 
         if (row > 0) { // check up for rook or queen 
            for (int i = row - 1; i >= 0; i--) { 
               if (board[i][col] == 'R' || board[i][col] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[i][col] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (row < 7) { // check down for rook or queen 
            for (int i = row + 1; i <= 7; i++) { 
               if (board[i][col] == 'R' || board[i][col] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[i][col] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (col > 0) { // check left for rook or queen 
            for (int i = col - 1; i >= 0; i--) { 
               if (board[row][i] == 'R' || board[row][i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row][i] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (col < 7) { // check right for rook or queen 
            for (int i = col + 1; i <= 7; i++) { 
               if (board[row][i] == 'R' || board[row][i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row][i] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         //////////////////////////////// check for bishop or queen 
         if (row > 0 && col > 0) { // check up left 
            int temp = (row > col) ? col : row; 
            for (int i = 1; i <= temp; i++) { 
               if (board[row - i][col - i] == 'B' || board[row - i][col - i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row - i][col - i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row > 0 && col < 7) { // check up right 
            int temp = (row > 7 - col) ? (7 - col) : row; 
            for (int i = 1; i <= temp; i++) { 
               if (board[row - i][col + i] == 'B' || board[row - i][col + i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row - i][col + i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row < 7 && col > 0) { // check down left 
            int temp = (7 - row > col) ? col : (7 - row); 
            for (int i = 1; i <= temp; i++) { 
               if (board[row + i][col - i] == 'B' || board[row + i][col - i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row + i][col - i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row < 7 && col < 7) { // check down right 
            int temp = (7 - row > 7 - col) ? (7 - col) : (7 - row); 
            for (int i = 1; i <= temp; i++) { 
               if (board[row + i][col + i] == 'B' || board[row + i][col + i] == 'Q') { 
                  status = 1; 
                  return true; 
               } else if (board[row + i][col + i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         ///////////////////////// check for pawn 
         if (row != 7 && col != 0 && board[row + 1][col - 1] == 'P') { // check down right 
            status = 1; 
            return true; 
         } 
         if (row != 7 && col != 7 && board[row + 1][col + 1] == 'P') { // check down right 
            status = 1; 
            return true; 
         } 
         //////////////////////// check for knight 
         /////////////// Up pieces 
         if (row > 0 && col > 1 && board[row - 1][col - 2] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row > 1 && col > 0 && board[row - 2][col - 1] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row > 1 && col < 7 && board[row - 2][col + 1] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row > 0 && col < 6 && board[row - 1][col + 2] == 'N') { 
            status = 1; 
            return true; 
         } 
         //////////////// Down pieces 
         if (row < 7 && col > 1 && board[row + 1][col - 2] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row < 6 && col > 0 && board[row + 2][col - 1] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row < 6 && col < 7 && board[row + 2][col + 1] == 'N') { 
            status = 1; 
            return true; 
         } 
         if (row < 7 && col < 6 && board[row + 1][col + 2] == 'N') { 
            status = 1; 
            return true; 
         } 
         break; 

      case 'K': // White king 
         if (row > 0) { // check up for rook or queen 
            for (int i = row - 1; i >= 0; i--) { 
               if (board[i][col] == 'r' || board[i][col] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[i][col] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (row < 7) { // check down for rook or queen 
            for (int i = row + 1; i <= 7; i++) { 
               if (board[i][col] == 'r' || board[i][col] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[i][col] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (col > 0) { // check left for rook or queen 
            for (int i = col - 1; i >= 0; i--) { 
               if (board[row][i] == 'r' || board[row][i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row][i] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         if (col < 7) { // check right for rook or queen 
            for (int i = col + 1; i <= 7; i++) { 
               if (board[row][i] == 'r' || board[row][i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row][i] != '.') // reached a piece that is not king 
                  break; 
            } 
         } 
         //////////////////////////////// check for bishop and queen 
         if (row > 0 && col > 0) { // check up left 
            int temp = (row > col) ? col : row; 
            for (int i = 1; i <= temp; i++) { 
               if (board[row - i][col - i] == 'b' || board[row - i][col - i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row - i][col - i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row > 0 && col < 7) { // check up right 
            int temp = (row > 7 - col) ? (7 - col) : row; 
            for (int i = 1; i <= temp; i++) { 
               if (board[row - i][col + i] == 'b' || board[row - i][col + i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row - i][col + i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row < 7 && col > 0) { // check down left 
            int temp = (7 - row > col) ? col : (7 - row); 
            for (int i = 1; i <= temp; i++) { 
               if (board[row + i][col - i] == 'b' || board[row + i][col - i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row + i][col - i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         if (row < 7 && col < 7) { // check down right 
            int temp = (7 - row > 7 - col) ? (7 - col) : (7 - row); 
            for (int i = 1; i <= temp; i++) { 
               if (board[row + i][col + i] == 'b' || board[row + i][col + i] == 'q') { 
                  status = 2; 
                  return true; 
               } else if (board[row + i][col + i] != '.') // reached a piece that is not bishop 
                  break; 
            } 
         } 
         ///////////////////////// check for pawn 
         if (row != 0 && col != 0 && board[row - 1][col - 1] == 'p') { // check down right 
            status = 2; 
            return true; 
         } 
         if (row != 0 && col != 7 && board[row - 1][col + 1] == 'p') { // check down right 
            status = 2; 
            return true; 
         } 
         //////////////////////// check for knight 
         /////////////// Up pieces 
         if (row > 0 && col > 1 && board[row - 1][col - 2] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row > 1 && col > 0 && board[row - 2][col - 1] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row > 1 && col < 7 && board[row - 2][col + 1] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row > 0 && col < 6 && board[row - 1][col + 2] == 'n') { 
            status = 2; 
            return true; 
         } 
         //////////////// Down pieces 
         if (row < 7 && col > 1 && board[row + 1][col - 2] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row < 6 && col > 0 && board[row + 2][col - 1] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row < 6 && col < 7 && board[row + 2][col + 1] == 'n') { 
            status = 2; 
            return true; 
         } 
         if (row < 7 && col < 6 && board[row + 1][col + 2] == 'n') { 
            status = 2; 
            return true; 
         } 
         break; 
   } 
   return false; 
} 

int main() 
{ 
//   ifstream in("CheckTheCheck.in"); 
    
   bool end = false; 
   string temp; 

   while (!end) { 
      end = true; 
      status = 0; 
      for (int i = 0; i < 8; i++) { 
//         getline(in, temp); 
         getline(cin, temp); 
         for (int j = 0; j < 8; j++) { 
            if ((board[i][j] = temp[j]) != '.') 
               end = false; 
         } 
      } 
       
      bool checked = false; 
      for (int i = 0; i < 8; i++) { 
         for (int j = 0; j < 8; j++) { 
            if (board[i][j] == 'k' || board[i][j] == 'K') { 
               checked = checkTheCheck(i, j); 
               if (checked) 
                  break; 
            } 
         } 
         if (checked) 
            break; 
      } 
      if (end) 
         break; 
      if (caseNum > 1) 
         cout << endl; 
      switch (status) { 
         case 0: 
            cout << "Game #" << caseNum << ": no king is in check."; 
            break; 
         case 1: 
            cout << "Game #" << caseNum << ": black king is in check."; 
            break; 
         case 2: 
            cout << "Game #" << caseNum << ": white king is in check.";    
            break; 
      } 

//      getline(in, temp); 
      getline(cin, temp); 
      caseNum++; 
   } 


//   cin >> temp; 
//   in.close(); 
   return 0; 
} 



Thanks.

sapnil
Experienced poster
Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST
Contact:

Post by sapnil » Tue Oct 02, 2007 4:25 pm

I get wrong answer as lik you.
Finally i recode & get Acc.

The code is too long,so is hard to
find bugs.

Thanks
Keep posting
Sapnil

jud
New poster
Posts: 4
Joined: Mon Jan 28, 2008 7:24 pm

Re: 10196 > Critical input/output PLZ!!.

Post by jud » Sun Feb 10, 2008 3:46 pm

several of ur test-cases are illegal. on each board there are 2 kings!

greeez

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

Re: 10196 - Check The Check

Post by Chirag Chheda » Tue Jul 01, 2008 8:16 am

Can someone who has got ACC plz post some more test cases so that i can rectify my error.
Also, I think that the test cases provided by CodeMaker are not il-legal as posted int the above post.
Let me know if i m wrong.
Thanking you in advance.

Waiting for a reply.
Bye

jud
New poster
Posts: 4
Joined: Mon Jan 28, 2008 7:24 pm

Re: 10196 - Check The Check

Post by jud » Tue Jul 01, 2008 10:26 am

here some test cases:

Code: Select all

........
...k....
....P...
........
........
........
.....K..
........

........
...k....
..P.....
........
........
........
.....K..
........

........
...k....
........
........
........
...R....
.....K..
........

........
...k..R.
........
........
........
........
.....K..
........

........
R..k....
........
........
........
........
.....K..
........

........
........
...R....
...k....
........
........
.....K..
........

........
...k....
........
.....B..
........
........
.....K..
........

........
...k....
........
.B......
........
........
.....K..
........

.....B..
........
...k....
........
........
........
.....K..
........

.B......
........
...k....
........
........
........
.....K..
........

........
...k....
........
...p....
........
...R....
.....K..
........

........
...k....
..r.....
.B......
........
........
.....K..
........

........
...k....
........
..N.....
........
........
.....K..
........

........
...k....
...pp...
....N...
........
........
.....K..
........

........
........
........
...k....
........
....p...
.....K..
........

........
........
........
..pk....
.N......
........
.....K..
........

........
........
........
...k.q..
.....N..
........
.....K..
........

........
..N.....
........
...k....
........
........
.....K..
........

........
....N...
........
...k....
........
........
.....K..
........

........
........
.....N..
...k....
........
........
.....K..
........

........
........
.N......
...k....
........
........
.....K..
........

........
q.......
........
...k....
........
....P...
.....K..
........

........
q.......
........
...k....
........
....P.p.
.....K..
........

.....r..
........
........
...k....
........
........
.r...K..
........

........
........
........
...k....
........
........
.....K..
...b....

r......r
........
........
...k....
........
...n....
.....K..
........

K......k
........
........
........
........
........
........
.......b

K......k
........
..p.....
........
........
........
........
.......b

Kp.....k
prn.....
..p.....
........
........
........
........
.......b

K..R...k
........
..p.....
........
........
........
........
.......b

K.pr...k
b....q..
..p..nn.
........
..b.....
........
........
bq.....b

K..RB..k
.....R.P
..p.....
.......Q
........
........
........
.......b

......B.
........
..p.....
........
..Kpk..r
........
........
.......b

......P.
...K.k..
..p.....
........
........
........
........
.......b

........
...K....
pppppppp
..n.n...
........
........
.k......
.......b

........
...K....
pppppppp
........
........
........
k.......
.......b

........
........
...k....
........
.B.R....
........
........
.K......

..k.....
ppp.pppp
........
.R...B..
........
........
PPPPPPPP
K.......

rnbqk.nr
ppp..ppp
....p...
...p....
.bPP....
.....N..
PP..PPPP
RNBQKB.R

........
...k....
....P...
........
........
........
.....K..
........

........
...k....
..P.....
........
........
........
.....K..
........

........
...k....
........
........
........
...R....
.....K..
........

........
...k..R.
........
........
........
........
.....K..
........

........
R..k....
........
........
........
........
.....K..
........

........
........
...R....
...k....
........
........
.....K..
........

........
...k....
........
.....B..
........
........
.....K..
........

........
...k....
........
.B......
........
........
.....K..
........

.....B..
........
...k....
........
........
........
.....K..
........

.B......
........
...k....
........
........
........
.....K..
........

........
...k....
........
...p....
........
...R....
.....K..
........

........
...k....
..r.....
.B......
........
........
.....K..
........

........
...k....
........
..N.....
........
........
.....K..
........

........
...k....
...pp...
....N...
........
........
.....K..
........

........
........
........
...k....
........
........
.....K..
........

........
........
........
..pk....
.N......
........
.....K..
........

........
........
........
...k.q..
.....N..
........
.....K..
........

........
..N.....
........
...k....
........
........
.....K..
........

........
....N...
........
...k....
........
........
.....K..
........

........
........
.....N..
...k....
........
........
.....K..
........

........
........
.N......
...k....
........
........
.....K..
........

........
q.......
........
...k....
........
....P...
.....K..
........

........
q.......
........
...k....
........
....P.p.
.....K..
........

.....r..
........
........
...k....
........
........
.r...K..
........

........
........
........
...k....
........
........
.....K..
...b....

r......r
........
........
...k....
........
...n....
.....K..
........

........
........
........
........
........
........
........
........

Chirag Chheda
Learning poster
Posts: 74
Joined: Sat Jun 21, 2008 12:24 pm
Location: India

Re: 10196 - Check The Check

Post by Chirag Chheda » Tue Jul 01, 2008 11:32 am

Thanx a lot frnd.
Ur test cases were really very helpful.
I found my mistake and got ACC.

User avatar
KaDeG
New poster
Posts: 13
Joined: Sun Mar 04, 2007 8:40 pm

Re: 10196 - Check The Check

Post by KaDeG » Thu Jul 24, 2008 5:33 am

Code removed after ACC. My WA cause was a function checking if a given (x, y) position is in the chess table and i was checking
if (x < 0 || y < 0 || x > 8 || y > 8)
return 0

but should been x >= 8 or y >= 8

Anyway. The test case which made me see this error
was the following...
........
......k.
........
.N......
........
........
........
........
Good luck
/*No Comment*/

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10196 - Check The Check

Post by Obaida » Tue Aug 05, 2008 7:52 am

Hi every one please help me i edited my code but still TLE!!!
But why an 8x8 board search will get TLE???
Here is my code:

Code: Select all

#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,k,g=0;
	char arr[10][10];
	bool CHECK,check,close;
	while(1)
	{
		CHECK=0;check=0,close=1;
		for(i=0;i<8;i++)
		{
			for(j=0;j<8;j++){scanf("%c",&arr[i][j]);if(arr[i][j]!='.')close = 0;}
			getchar();
		}
		if(close)break;
		g++;
		printf("Game #%d:",g);
		for(i=0;i<8;i++)
		{
			for(j=0;j<8;j++)
			{
				if(arr[i][j]=='p'){if(arr[i+1][j+1]=='K'||arr[i+1][j-1]=='K'){CHECK = 1;break;}}
				else if(arr[i][j]=='P'){if(arr[i-1][j+1]=='k'||arr[i-1][j-1]=='k'){check=1;break;}}
				else if(arr[i][j]=='r')
				{	
					for(k=i+1;k<8;k++){if(arr[k][j]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i+1;k<8;k++){if(arr[i][k]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[k][j]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[i][k]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
				}
				else if(arr[i][j]=='R')
				{
					for(k=i+1;k<8;k++){if(arr[k][j]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i+1;k<8;k++){if(arr[i][k]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[k][j]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[i][k]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
				}
				else if(arr[i][j]=='b')
				{
					for(k=1;k<8-i+1;k++){if(arr[i+k][j+k]=='K'){CHECK = 1;break;}else if(arr[i+k][j+k]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j-k]=='K'){CHECK = 1;break;}else if(arr[i+k][j-k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j+k]=='K'){CHECK = 1;break;}else if(arr[i-k][j+k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j-k]=='K'){CHECK = 1;break;}else if(arr[i-k][j-k]!='.')break;}
				}
				else if(arr[i][j]=='B')
				{
					for(k=1;k<8-i+1;k++){if(arr[i+k][j+k]=='k'){check = 1;break;}else if(arr[i+k][j+k]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j-k]=='k'){check = 1;break;}else if(arr[i+k][j-k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j+k]=='k'){check = 1;break;}else if(arr[i-k][j+k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j-k]=='k'){check = 1;break;}else if(arr[i-k][j-k]!='.')break;}
				}
				else if(arr[i][j]=='q')
				{
					for(k=i+1;k<8;k++){if(arr[k][j]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i+1;k<8;k++){if(arr[i][k]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[k][j]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[i][k]=='K'){CHECK = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j+k]=='K'){CHECK = 1;break;}else if(arr[i+k][j+k]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j-k]=='K'){CHECK = 1;break;}else if(arr[i+k][j-k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j+k]=='K'){CHECK = 1;break;}else if(arr[i-k][j+k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j-k]=='K'){CHECK = 1;break;}else if(arr[i-k][j-k]!='.')break;}
				}
				else if(arr[i][j]=='Q')
				{
					for(k=i+1;k<8;k++){if(arr[k][j]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i+1;k<8;k++){if(arr[i][k]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[k][j]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=i-1;k>=0;k--){if(arr[i][k]=='k'){check = 1;break;}else if(arr[k][j]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j+k]=='k'){check = 1;break;}else if(arr[i+k][j+k]!='.')break;}
					for(k=1;k<8-i+1;k++){if(arr[i+k][j-k]=='k'){check = 1;break;}else if(arr[i+k][j-k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j+k]=='k'){check = 1;break;}else if(arr[i-k][j+k]!='.')break;}
					for(k=1;k<=i;k++){if(arr[i-k][j-k]=='k'){check = 1;break;}else if(arr[i-k][j-k]!='.')break;}
				}
				else if(arr[i][j]=='n')
				{
					if(arr[i+1][j+2]=='K'||arr[i+1][j-2]=='K'||arr[i-1][j+2]=='K'||arr[i-1][j-2]=='K'){CHECK = 1;break;}
				}
				else if(arr[i][j]=='N')
				{
					if(arr[i+1][j+2]=='k'||arr[i+1][j-2]=='k'||arr[i-1][j+2]=='k'||arr[i-1][j-2]=='k'){check = 1;break;}
				}
				if(CHECK)break;else if(check)break;
			}
		}
		if(CHECK)puts(" white king is in check.");
		else if(check)puts(" black king is in check.");
		else puts(" no king is in check.");
	}
	return 0;
}
try_try_try_try_&&&_try@try.com
This may be the address of success.

rtperson
New poster
Posts: 3
Joined: Fri Aug 22, 2008 2:53 pm

Re: 10196 - Check The Check

Post by rtperson » Fri Aug 22, 2008 3:10 pm

Hi Opu,

Hopefully you've figured this out by now, but the issue here is that getchar() when you set up your board. The judge ain't gonna hit return for you. :wink:

Also, I might have missed it, but I don't see how you're checking for an empty board. There's no guarantee that the empty board will be the last board, and checking the board after the empty one would be an error.

Hope this helps. I still have yet to pass this one, though I haven't yet found the test case that's failing me. Good luck!

Roger

stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Re: 10196 - Check The Check

Post by stcheung » Wed Oct 08, 2008 8:35 am

This problem is mostly a test of how careful you are. With that said, here are couple hints and things to watch out for:
(1) When checking pawn, only check 1 direction, depending on the pawn's color
(2) When checking rook, bishop and queen, remember if there's another piece in between, the king will still be safe
(3) No need to check for king attacking king, because the problem states at most one king will be in check
(4) It might be easier to first locate the king, and then determine whether it's being attacked by another piece. This is in constrast to scanning the board, figuring out what piece you are currently at, and then determining if it's attacking a king.
(5) Instead of a 8x8 char array, create a 12x12 char array instead. You will store the board in the center of course. This will save you a lot of boundary checking code.

In general, the more lines of code you have the more likely you will have bugs. My code is about 150 lines with no "code squeezing" at all. If your code is much longer than that and you keep getting WA, might as well try rewriting it to reduce the room for error.

danielg_ramos
New poster
Posts: 2
Joined: Mon Apr 20, 2009 10:27 pm

Re: 10196 - Check The Check

Post by danielg_ramos » Mon Apr 20, 2009 10:36 pm

Hi,

I'm really frustrated with this problem.
I've already tested ALL possible positions; i've got all test cases and compared answers with uva toolkit, and my answers are all right..
I dont know why im still getting WA all the time..

Please tell me if the situation bellow is a check:
K.......
.....r..
........
.q......
........
........
........
....k...

As we can see, the white king cant move, so he would loose the game..
But this situation is not mentioned on the problem, so i didnt worked on it.

Please someone give me some help, my code will be posted bellow, so if someone find a case where my problam fails, please tell me.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>

using namespace std;
char board[8][8];

//Retorna 0 se nao tiver fazendo check; retorna 1 se branco tiver fazendo check; retorna 2 se preto tiver fazendo check.
int isMakingCheck(char kind, int xpos, int ypos){
     if(kind == 'k'){
             return 0;
     }
     else if(kind == 'K'){
          return 0;
     }
     else if(kind == '.'){
          return 0;
     }
     
     else if(kind == 'p'){
          if(xpos < 7){
                  if(ypos > 0){
                          if(board[xpos+1][ypos-1] == 'K')
                               return 2;
                  }
                  if(ypos < 7){
                          if(board[xpos+1][ypos+1] == 'K')
                               return 2;
                  }
          }
     }
     
     else if(kind == 'P'){
          if(xpos > 0){
                  if(ypos > 0){
                          if(board[xpos-1][ypos-1] == 'k')
                               return 1;
                  }
                  if(ypos < 7){
                          if(board[xpos-1][ypos+1] == 'k')
                               return 1;
                  }
          }
     }
     
     else if(kind == 'r'){
          for(int i = xpos+1; i < 8; i++){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'K')
                         return 2;
                  else
                         break;
          }
          
          for(int i = xpos-1; i >= 0; i--){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'K')
                         return 2;
                  else
                         break;
          }
          
          for(int j = ypos+1; j < 8; j++){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          
          for(int j = ypos-1; j >= 0; j--){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'K')
                         return 2;
                  else
                         break;     
          }
          
     }
     else if(kind == 'R'){
          
          for(int i = xpos+1; i < 8; i++){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'k')
                         return 1;
                  else
                         break;
          }
          
          for(int i = xpos-1; i >= 0; i--){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'k')
                         return 1;
                  else
                         break;
          }
          
          for(int j = ypos+1; j < 8; j++){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          
          for(int j = ypos-1; j >= 0; j--){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'k')
                         return 1;
                  else
                         break;     
          }
          
     }
     
     else if(kind == 'b'){
          int i = xpos+1, j = ypos+1;
          
          while(i < 8 && j < 8){
                  if(board[i][j] == '.'){
                         i++;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break;     
          }
          i = xpos-1;
          j = ypos+1;
          while(i >=0 && j < 8){
                  if(board[i][j] == '.'){
                         i--;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          i = xpos+1;
          j = ypos-1;
          while(i < 8 && j >= 0){
                  if(board[i][j] == '.'){
                         i++;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          i = xpos-1;
          j = ypos-1;
          while(i >=0 && j >= 0){
                  if(board[i][j] == '.'){
                         i--;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break;                   
          }
          
     }
     else if(kind == 'B'){
          
          int i = xpos+1, j = ypos+1;
          
          while(i < 8 && j < 8){
                  if(board[i][j] == '.'){
                         i++;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break;     
          }
          i = xpos-1;
          j = ypos+1;
          while(i >=0 && j < 8){
                  if(board[i][j] == '.'){
                         i--;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          i = xpos+1;
          j = ypos-1;
          while(i < 8 && j >= 0){
                  if(board[i][j] == '.'){
                         i++;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          i = xpos-1;
          j = ypos-1;
          while(i >=0 && j >= 0){
                  if(board[i][j] == '.'){
                         i--;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break;                   
          }
          
     }
     else if(kind == 'q'){
          
          for(int i = xpos+1; i < 8; i++){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'K')
                         return 2;
                  else
                         break;
          }
          
          for(int i = xpos-1; i >= 0; i--){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'K')
                         return 2;
                  else
                         break;
          }
          
          for(int j = ypos+1; j < 8; j++){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          
          for(int j = ypos-1; j >= 0; j--){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'K')
                         return 2;
                  else
                         break;     
          }
          
          int i = xpos+1, j = ypos+1;
          
          while(i < 8 && j < 8){
                  if(board[i][j] == '.'){
                         i++;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break;     
          }
          i = xpos-1;
          j = ypos+1;
          while(i >=0 && j < 8){
                  if(board[i][j] == '.'){
                         i--;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          i = xpos+1;
          j = ypos-1;
          while(i < 8 && j >= 0){
                  if(board[i][j] == '.'){
                         i++;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break; 
          }
          i = xpos-1;
          j = ypos-1;
          while(i >=0 && j >= 0){
                  if(board[i][j] == '.'){
                         i--;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'K')
                         return 2;
                  else
                         break;                   
          }
          
          
     }
     else if(kind == 'Q'){
          
          for(int i = xpos+1; i < 8; i++){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'k')
                         return 1;
                  else
                         break;
          }
          
          for(int i = xpos-1; i >= 0; i--){
                  if(board[i][ypos] == '.')
                         continue;
                  else if(board[i][ypos] == 'k')
                         return 1;
                  else
                         break;
          }
          
          for(int j = ypos+1; j < 8; j++){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          
          for(int j = ypos-1; j >= 0; j--){
                  if(board[xpos][j] == '.')
                         continue;
                  else if(board[xpos][j] == 'k')
                         return 1;
                  else
                         break;     
          }
          
          int i = xpos+1, j = ypos+1;
          
          while(i < 8 && j < 8){
                  if(board[i][j] == '.'){
                         i++;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break;     
          }
          i = xpos-1;
          j = ypos+1;
          while(i >=0 && j < 8){
                  if(board[i][j] == '.'){
                         i--;
                         j++; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          i = xpos+1;
          j = ypos-1;
          while(i < 8 && j >= 0){
                  if(board[i][j] == '.'){
                         i++;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break; 
          }
          i = xpos-1;
          j = ypos-1;
          while(i >=0 && j >= 0){
                  if(board[i][j] == '.'){
                         i--;
                         j--; 
                         continue;
                  }
                  else if(board[i][j] == 'k')
                         return 1;
                  else
                         break;                   
          }
          
     }
     
     else if(kind == 'n'){
          if(xpos > 0 && ypos > 1){
                  if(board[xpos-1][ypos-2] == 'K')
                         return 2;
          }
          
          if(xpos > 1 && ypos > 0){
                  if(board[xpos-2][ypos-1] == 'K')
                         return 2;
          }
          
          if(xpos > 1 && ypos < 7){
                  if(board[xpos-2][ypos+1] == 'K')
                         return 2;
          }
          
          if(xpos > 0 && ypos < 6){
                  if(board[xpos-1][ypos+2] == 'K')
                         return 2;
          }
          
          if(xpos < 7 && ypos > 1){
                  if(board[xpos+1][ypos-2] == 'K')
                         return 2;
          }
          
          if(xpos < 6 && ypos > 0){
                  if(board[xpos+2][ypos-1] == 'K')
                         return 2;
          }
          
          if(xpos < 6 && ypos < 7){
                  if(board[xpos+2][ypos+1] == 'K')
                         return 2;
          }
          
          if(xpos < 7 && ypos < 6){
                  if(board[xpos+1][ypos+2] == 'K')
                         return 2;
          }        
          
     }
     else if(kind == 'N'){
          
          if(xpos > 0 && ypos > 1){
                  if(board[xpos-1][ypos-2] == 'k')
                         return 1;
          }
          
          if(xpos > 1 && ypos > 0){
                  if(board[xpos-2][ypos-1] == 'k')
                         return 1;
          }
          
          if(xpos > 1 && ypos < 7){
                  if(board[xpos-2][ypos+1] == 'k')
                         return 1;
          }
          
          if(xpos > 0 && ypos < 6){
                  if(board[xpos-1][ypos+2] == 'k')
                         return 1;
          }
          
          if(xpos < 7 && ypos > 1){
                  if(board[xpos+1][ypos-2] == 'k')
                         return 1;
          }
          
          if(xpos < 6 && ypos > 0){
                  if(board[xpos+2][ypos-1] == 'k')
                         return 1;
          }
          
          if(xpos < 6 && ypos < 7){
                  if(board[xpos+2][ypos+1] == 'k')
                         return 1;
          }
          
          if(xpos < 7 && ypos < 6){
                  if(board[xpos+1][ypos+2] == 'k')
                         return 1;
          }   
          
     }
}
int main(){
    
    int game = 1;
    while(1){ 
    
    bool empty = true;
    for(int i = 0; i < 8; i++){
            for(int j = 0; j < 8; j++){
                    cin >> board[i][j];
                    if(board[i][j] != '.')
                          empty = false; 
            }        
    }
    
    if(empty) return 0; 
    
    int retorno = 0;
    bool whiteMadeCheck = false;
    bool blackMadeCheck = false;
    
    for(int i = 0; i < 8; i++){
            for(int j = 0; j < 8; j++){
                    retorno = isMakingCheck(board[i][j], i, j);
                    
                    if(retorno == 0)
                           continue;
                    else if(retorno == 1){
                           whiteMadeCheck = true;
                          // cout << "posit: " << i << "," << j << endl;
                           }
                    else
                           blackMadeCheck = true;
            }        
    }
    
    if(whiteMadeCheck){
           cout << "Game #" << game++ << ": black king is in check." << endl;            
    }
    else if(blackMadeCheck){
           cout << "Game #" << game++ << ": white king is in check." << endl; 
    }
    else {
           cout << "Game #" << game++ << ": no king is in check." << endl; 
    }  
    }
    return 0;
}


I appreciate your help.

Thanks,

Daniel Ramos

Al3x4nd3r
New poster
Posts: 1
Joined: Mon Sep 22, 2008 6:36 pm

Re: 10196 - Check The Check

Post by Al3x4nd3r » Mon Apr 20, 2009 11:34 pm

In the first paragraph
A king is in check if it's in a square which is attacked by an oponnet's piece (i.e. it's in square which can be taken by an oponnet's piece in his next move).

i don't know where is my problem too =p

[OFF]
Fosse pelo caminho mais dificil... eu fiz o caminho inverso saindo do rei atras dos inimigos e não dos inimigos para o rei.
[/OFF]

danielg_ramos
New poster
Posts: 2
Joined: Mon Apr 20, 2009 10:27 pm

Re: 10196 - Check The Check

Post by danielg_ramos » Tue Apr 21, 2009 12:55 am

Hi,

I just got AC!
I simply erased everything and started from the beginning, following some advices from people here...
I was checking if every piece could make a check, instead of taking the king and look if its in check.

Although i got AC, im still angry about my WA with my first implementation. Even its not the very best way, im pretty sure its right..

Anyway, I have to say that the hints said here are really usefull and avoid a lot of waste of time, like using a 12x12 char array..

Thanks,

Daniel Ramos

MoseHas
New poster
Posts: 1
Joined: Sun Apr 26, 2009 10:49 pm

Re: 10196 - Check The Check

Post by MoseHas » Sun Apr 26, 2009 10:57 pm

hi there i'm a student from Taiwan, so my English is not really good for expressing my problem.
i have a big trouble with 10196 cuz i have tested all the testing data that i could find and i still get WA :(
is there anybody who can give me some advice for my WA code? thx a lot :)

a very long code orz...275 lines

Code: Select all

#include <stdio.h>
#include <stdlib.h>

struct coordinates
{
    int x,y;
}cur;

char chessboard[8][8];
int isWKInCheck(struct coordinates wKing);
int isBKInCheck(struct coordinates bKing);

int main()
{
    int cou=1,i,j,wKingCheck,bKingCheck;
    while (1)
    {
        struct coordinates wKing,bKing;//declare
        wKing.x=wKing.y=bKing.x=bKing.y=-9;//initialize to -9
        for (i=0;i<8;i++) scanf("%s",chessboard[i]);
        int end=1;
        for (j=0;j<8;j++) for (i=0;i<8;i++) if (chessboard[j][i]!='.')
                {
                    end=0;
                    break;
                }
        if (end) break;
        for (j=0;j<8;j++) for (i=0;i<8;i++)
            {
                if (chessboard[j][i]=='K')
                {
                    wKing.x=i;
                    wKing.y=j;
                }
                else if (chessboard[j][i]=='k')
                {
                    bKing.x=i;
                    bKing.y=j;
                }
            }
        wKingCheck=isWKInCheck(wKing);
        bKingCheck=isBKInCheck(bKing);
        if (wKingCheck) printf("Game #%d: white king is in check.\n",cou);
        else if (bKingCheck) printf("Game #%d: black king is in check.\n",cou);
        else printf("Game #%d: no king is in check.\n",cou);
        cou++;
    }
    return 0;
}

int isWKInCheck(struct coordinates wKing)
{
    cur=wKing;//the first assignment
    int _1st_step=0;
    while (1)//left of the above
    {
        --cur.y;
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='b'||curChar=='q') return 1;
        else if (curChar=='p'&&!_1st_step) return 1;//the correct determination of pawns
        else if (curChar=='n'||curChar=='r'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
        _1st_step++;
    }
    cur=wKing;//re-assignment
    while (1)//the above
    {
        --cur.y;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='r'||curChar=='q') return 1;
        else if (curChar=='b'||curChar=='n'||curChar=='p'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    cur=wKing;//re-assignment
    _1st_step=0;
    while (1)//right of the above
    {
        --cur.y;
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='b'||curChar=='q'||(curChar=='p'&&!_1st_step)) return 1;//the correct determination of pawns
        else if (curChar=='n'||curChar=='r'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
        _1st_step++;
    }
    cur=wKing;//re-assignment
    while (1)//left
    {
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='r'||curChar=='q') return 1;
        else if (curChar=='b'||curChar=='n'||curChar=='p'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    cur=wKing;//re-assignment
    while (1)//right
    {
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='r'||curChar=='q') return 1;
        else if (curChar=='b'||curChar=='n'||curChar=='p'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    cur=wKing;//re-assignment
    while (1)//left of the below
    {
        ++cur.y;
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='b'||curChar=='q') return 1;
        else if (curChar=='n'||curChar=='p'||curChar=='r'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    cur=wKing;//re-assignment
    while (1)//the below
    {
        ++cur.y;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='r'||curChar=='q') return 1;
        else if (curChar=='b'||curChar=='n'||curChar=='p'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    cur=wKing;//re-assignment
    while (1)//right of the below
    {
        ++cur.y;
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='b'||curChar=='q') return 1;
        else if (curChar=='n'||curChar=='p'||curChar=='r'||curChar=='k') break;//the enemy's chesses but not the correct one
        else if (curChar<83&&curChar>65) break;//own chesses
    }
    struct coordinates N[8];//assume that the knight are on one of the 8 situation
    N[0].x=wKing.x-2;//initialize
    N[0].y=wKing.y-1;
    N[1].x=wKing.x-1;
    N[1].y=wKing.y-2;
    N[2].x=wKing.x+2;
    N[2].y=wKing.y-1;
    N[3].x=wKing.x+1;
    N[3].y=wKing.y-2;
    N[4].x=wKing.x-2;
    N[4].y=wKing.y+1;
    N[5].x=wKing.x-1;
    N[5].y=wKing.y+2;
    N[6].x=wKing.x+2;
    N[6].y=wKing.y+1;
    N[7].x=wKing.x+1;
    N[7].y=wKing.y+2;
    int i;
    for (i=0;i<8;i++) if (chessboard[N[i].y][N[i].x]=='n') return 1;
    return 0;
}

int isBKInCheck(struct coordinates bKing)
{
    cur=bKing;//the first assignment
    while (1)//left of the above
    {
        --cur.y;
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='B'||curChar=='Q') return 1;
        else if (curChar=='N'||curChar=='P'||curChar=='R'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    while (1)//the above
    {
        --cur.y;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='R'||curChar=='Q') return 1;
        else if (curChar=='B'||curChar=='N'||curChar=='P'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    while (1)//right of the above
    {
        --cur.y;
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='B'||curChar=='Q') return 1;
        else if (curChar=='N'||curChar=='P'||curChar=='R'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    while (1)//left
    {
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='R'||curChar=='Q') return 1;
        else if (curChar=='B'||curChar=='N'||curChar=='P'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    while (1)//right
    {
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='R'||curChar=='Q') return 1;
        else if (curChar=='B'||curChar=='N'||curChar=='P'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    int _1st_step=0;
    while (1)//left of the below
    {
        ++cur.y;
        --cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='B'||(curChar=='P'&&!_1st_step)||curChar=='Q') return 1;
        else if (curChar=='N'||curChar=='R'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
        _1st_step++;
    }
    cur=bKing;//re-assignment
    while (1)//the below
    {
        ++cur.y;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if (curChar=='R'||curChar=='Q') return 1;
        else if (curChar=='B'||curChar=='N'||curChar=='P'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
    }
    cur=bKing;//re-assignment
    _1st_step=0;
    while (1)//right of the below
    {
        ++cur.y;
        ++cur.x;
        char curChar=chessboard[cur.y][cur.x];//declare a char to make statements simpler
        if (cur.x<0||cur.x>7||cur.y<0||cur.y>7) break;//break condition
        if ((curChar=='P'&&!_1st_step)||curChar=='B'||curChar=='Q') return 1;//the correct determination of pawns
        else if (curChar=='N'||curChar=='R'||curChar=='K') break;//the enemy's chesses but not the correct one
        else if (curChar<115&&curChar>97) break;//own chesses
        _1st_step++;
    }
    struct coordinates N[8];//assume that the knight are on one of the 8 situation
    N[0].x=bKing.x-2;//initialize
    N[0].y=bKing.y-1;
    N[1].x=bKing.x-1;
    N[1].y=bKing.y-2;
    N[2].x=bKing.x+2;
    N[2].y=bKing.y-1;
    N[3].x=bKing.x+1;
    N[3].y=bKing.y-2;
    N[4].x=bKing.x-2;
    N[4].y=bKing.y+1;
    N[5].x=bKing.x-1;
    N[5].y=bKing.y+2;
    N[6].x=bKing.x+2;
    N[6].y=bKing.y+1;
    N[7].x=bKing.x+1;
    N[7].y=bKing.y+2;
    int i;
    for (i=0;i<8;i++) if (chessboard[N[i].y][N[i].x]=='N') return 1;
    return 0;
}

ThanatosX
New poster
Posts: 1
Joined: Tue Jun 23, 2009 12:45 am

Re: 10196 - Check The Check

Post by ThanatosX » Tue Jun 23, 2009 12:55 am

jud wrote:here some test cases:

Code: Select all

........
...k....
....P...
........
........
........
.....K..
........

........
...k....
..P.....
........
........
........
.....K..
........

........
...k....
........
........
........
...R....
.....K..
........

........
...k..R.
........
........
........
........
.....K..
........

........
R..k....
........
........
........
........
.....K..
........

........
........
...R....
...k....
........
........
.....K..
........

........
...k....
........
.....B..
........
........
.....K..
........

........
...k....
........
.B......
........
........
.....K..
........

.....B..
........
...k....
........
........
........
.....K..
........

.B......
........
...k....
........
........
........
.....K..
........

........
...k....
........
...p....
........
...R....
.....K..
........

........
...k....
..r.....
.B......
........
........
.....K..
........

........
...k....
........
..N.....
........
........
.....K..
........

........
...k....
...pp...
....N...
........
........
.....K..
........

........
........
........
...k....
........
....p...
.....K..
........

........
........
........
..pk....
.N......
........
.....K..
........

........
........
........
...k.q..
.....N..
........
.....K..
........

........
..N.....
........
...k....
........
........
.....K..
........

........
....N...
........
...k....
........
........
.....K..
........

........
........
.....N..
...k....
........
........
.....K..
........

........
........
.N......
...k....
........
........
.....K..
........

........
q.......
........
...k....
........
....P...
.....K..
........

........
q.......
........
...k....
........
....P.p.
.....K..
........

.....r..
........
........
...k....
........
........
.r...K..
........

........
........
........
...k....
........
........
.....K..
...b....

r......r
........
........
...k....
........
...n....
.....K..
........

K......k
........
........
........
........
........
........
.......b

K......k
........
..p.....
........
........
........
........
.......b

Kp.....k
prn.....
..p.....
........
........
........
........
.......b

K..R...k
........
..p.....
........
........
........
........
.......b

K.pr...k
b....q..
..p..nn.
........
..b.....
........
........
bq.....b

K..RB..k
.....R.P
..p.....
.......Q
........
........
........
.......b

......B.
........
..p.....
........
..Kpk..r
........
........
.......b

......P.
...K.k..
..p.....
........
........
........
........
.......b

........
...K....
pppppppp
..n.n...
........
........
.k......
.......b

........
...K....
pppppppp
........
........
........
k.......
.......b

........
........
...k....
........
.B.R....
........
........
.K......

..k.....
ppp.pppp
........
.R...B..
........
........
PPPPPPPP
K.......

rnbqk.nr
ppp..ppp
....p...
...p....
.bPP....
.....N..
PP..PPPP
RNBQKB.R

........
...k....
....P...
........
........
........
.....K..
........

........
...k....
..P.....
........
........
........
.....K..
........

........
...k....
........
........
........
...R....
.....K..
........

........
...k..R.
........
........
........
........
.....K..
........

........
R..k....
........
........
........
........
.....K..
........

........
........
...R....
...k....
........
........
.....K..
........

........
...k....
........
.....B..
........
........
.....K..
........

........
...k....
........
.B......
........
........
.....K..
........

.....B..
........
...k....
........
........
........
.....K..
........

.B......
........
...k....
........
........
........
.....K..
........

........
...k....
........
...p....
........
...R....
.....K..
........

........
...k....
..r.....
.B......
........
........
.....K..
........

........
...k....
........
..N.....
........
........
.....K..
........

........
...k....
...pp...
....N...
........
........
.....K..
........

........
........
........
...k....
........
........
.....K..
........

........
........
........
..pk....
.N......
........
.....K..
........

........
........
........
...k.q..
.....N..
........
.....K..
........

........
..N.....
........
...k....
........
........
.....K..
........

........
....N...
........
...k....
........
........
.....K..
........

........
........
.....N..
...k....
........
........
.....K..
........

........
........
.N......
...k....
........
........
.....K..
........

........
q.......
........
...k....
........
....P...
.....K..
........

........
q.......
........
...k....
........
....P.p.
.....K..
........

.....r..
........
........
...k....
........
........
.r...K..
........

........
........
........
...k....
........
........
.....K..
...b....

r......r
........
........
...k....
........
...n....
.....K..
........

........
........
........
........
........
........
........
........
Hi, guys.

I got WA.

My program produced this output:

Code: Select all

Game #1: black king is in check.
Game #2: black king is in check.
Game #3: black king is in check.
Game #4: black king is in check.
Game #5: black king is in check.
Game #6: black king is in check.
Game #7: black king is in check.
Game #8: black king is in check.
Game #9: black king is in check.
Game #10: black king is in check.
Game #11: no king is in check.
Game #12: no king is in check.
Game #13: black king is in check.
Game #14: black king is in check.
Game #15: white king is in check.
Game #16: black king is in check.
Game #17: black king is in check.
Game #18: black king is in check.
Game #19: black king is in check.
Game #20: black king is in check.
Game #21: black king is in check.
Game #22: no king is in check.
Game #23: white king is in check.
Game #24: white king is in check.
Game #25: no king is in check.
Game #26: white king is in check.
Game #27: white king is in check.
Game #28: no king is in check.
Game #29: white king is in check.
Game #30: black king is in check.
Game #31: no king is in check.
Game #32: no king is in check.
Game #33: no king is in check.
Game #34: no king is in check.
Game #35: white king is in check.
Game #36: no king is in check.
Game #37: black king is in check.
Game #38: black king is in check.
Game #39: white king is in check.
Game #40: black king is in check.
Game #41: black king is in check.
Game #42: black king is in check.
Game #43: black king is in check.
Game #44: black king is in check.
Game #45: black king is in check.
Game #46: black king is in check.
Game #47: black king is in check.
Game #48: black king is in check.
Game #49: black king is in check.
Game #50: no king is in check.
Game #51: no king is in check.
Game #52: black king is in check.
Game #53: black king is in check.
Game #54: no king is in check.
Game #55: black king is in check.
Game #56: black king is in check.
Game #57: black king is in check.
Game #58: black king is in check.
Game #59: black king is in check.
Game #60: black king is in check.
Game #61: no king is in check.
Game #62: white king is in check.
Game #63: white king is in check.
Game #64: no king is in check.
Game #65: white king is in check.
Is it correct? My code is this one:

Code: Select all

#include <stdio.h>

enum colors
{
  BLACK, WHITE, NONE
};

typedef struct
{
  int x, y;
} piece;

void
readLine(char linha[8])
{
  scanf("\n%[^\n]", (char *) &linha);
}

void
printTable(char board[8][8])
{
  int i, j;
  for (i = 0; i < 8; ++i)
    {
      for (j = 0; j < 8; ++j)
        printf("%c", board[i][j]);
      printf("\n");
    }
}

int
readBoard(char board[8][8])
{
  int i;
  char buff[8];

  if (scanf("\n\n%[^\n]", (char *) &buff) != EOF)
    {
      for (i = 0; i < 8; ++i)
        board[0][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[1][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[2][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[3][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[4][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[5][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[6][i] = buff[i];

      scanf("\n%[^\n]", (char *) &buff);
      for (i = 0; i < 8; ++i)
        board[7][i] = buff[i];

      return 1;
    }
  return 0;
}

int
getKings(char board[8][8], piece* Kings)
{
  int a, b;

  Kings[BLACK].x = 99;
  Kings[WHITE].x = 99;

  for (a = 0; a < 8; ++a)
    for (b = 0; b < 8; ++b)
      {
        if (board[a][b] == 'k')
          {
            Kings[BLACK].x = a;
            Kings[BLACK].y = b;
          }
        else if (board[a][b] == 'K')
          {
            Kings[WHITE].x = a;
            Kings[WHITE].y = b;
          }

      }
  if (Kings[BLACK].x == Kings[WHITE].x && Kings[WHITE].x == 99)
    return 1;
  return 0;
}

int
checkPawn(char board[8][8], piece* Kings)
{
  piece pawn = Kings[BLACK];
  pawn.x += 1;
  pawn.y -= 1;

  if (pawn.x > -1 && pawn.y > -1)
    {
      if (board[pawn.x][pawn.y] == 'P')
        return BLACK;
    }
  pawn.y += 2;
  if (pawn.x > -1 && pawn.y < 8)
    {
      if (board[pawn.x][pawn.y] == 'P')
        return BLACK;
    }

  pawn = Kings[WHITE];
  pawn.x -= 1;
  pawn.y -= 1;

  if (pawn.x < 8 && pawn.y > -1)
    {
      if (board[pawn.x][pawn.y] == 'p')
        return WHITE;
    }
  pawn.y += 2;
  if (pawn.x < 8 && pawn.y < 8)
    {
      if (board[pawn.x][pawn.y] == 'p')
        return WHITE;
    }

  return NONE;
}

int
checkVertical(char board[8][8], piece* Kings)
{
  int i;
  char p;
  /* DOWN */
  for (i = Kings[WHITE].x + 1; i < 8; ++i)
    {
      p = board[i][Kings[WHITE].y];
      if (p == 'q' || p == 'r')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x + 1; i < 8; ++i)
    {
      p = board[i][Kings[BLACK].y];
      if (p == 'Q' || p == 'R')
        return BLACK;
      if (p != '.')
        break;
    }

  /* UP */
  for (i = Kings[WHITE].x - 1; i > -1; --i)
    {
      p = board[i][Kings[WHITE].y];
      if (p == 'q' || p == 'r')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x - 1; i > -1; --i)
    {
      p = board[i][Kings[BLACK].y];
      if (p == 'Q' || p == 'R')
        return BLACK;
      if (p != '.')
        break;
    }

  return NONE;
}

int
checkHorizontal(char board[8][8], piece* Kings)
{
  int i;
  char p;
  /* RIGHT */
  for (i = Kings[WHITE].y + 1; i < 8; ++i)
    {
      p = board[Kings[WHITE].x][i];
      if (p == 'q' || p == 'r')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].y + 1; i < 8; ++i)
    {
      p = board[Kings[BLACK].x][i];
      if (p == 'Q' || p == 'R')
        return BLACK;
      if (p != '.')
        break;
    }

  /* LEFT */
  for (i = Kings[WHITE].y - 1; i > -1; --i)
    {
      p = board[Kings[WHITE].x][i];
      if (p == 'q' || p == 'r')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].y - 1; i > -1; --i)
    {
      p = board[Kings[BLACK].x][i];
      if (p == 'Q' || p == 'R')
        return BLACK;
      if (p != '.')
        break;
    }

  return NONE;
}

int
checkDiagonals(char board[8][8], piece* Kings)
{
  int i, j;
  char p;
  /* DOWN-RIGHT*/
  for (i = Kings[WHITE].x + 1, j = Kings[WHITE].y + 1; i < 8 && j < 8; ++i, ++j)
    {
      p = board[i][j];
      if (p == 'q' || p == 'b')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x + 1, j = Kings[BLACK].y + 1; i < 8 && j < 8; ++i, ++j)
    {
      p = board[i][j];
      if (p == 'Q' || p == 'B')
        return BLACK;
      if (p != '.')
        break;
    }
  /* DOWN-LEFT*/
  for (i = Kings[WHITE].x - 1, j = Kings[WHITE].y + 1; i > -1 && j < 8; --i, ++j)
    {
      p = board[i][j];
      if (p == 'q' || p == 'b')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x - 1, j = Kings[BLACK].y + 1; i > -1 && j < 8; --i, ++j)
    {
      p = board[i][j];
      if (p == 'Q' || p == 'B')
        return BLACK;
      if (p != '.')
        break;
    }

  /* UP-RIGHT */
  for (i = Kings[WHITE].x + 1, j = Kings[WHITE].y - 1; i < 8 && j > -1; ++i, --j)
    {
      p = board[i][j];
      if (p == 'q' || p == 'b')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x + 1, j = Kings[BLACK].y - 1; i < 8 && j > -1; ++i, --j)
    {
      p = board[i][j];
      if (p == 'Q' || p == 'B')
        return BLACK;
      if (p != '.')
        break;
    }
  /* UP-LEFT */
  for (i = Kings[WHITE].x - 1, j = Kings[WHITE].y - 1; i > -1 && j > -1; --i, --j)
    {
      p = board[i][j];
      if (p == 'q' || p == 'b')
        return WHITE;
      if (p != '.')
        break;
    }
  for (i = Kings[BLACK].x - 1, j = Kings[BLACK].y - 1; i > -1 && j > -1; --i, --j)
    {
      p = board[i][j];
      if (p == 'Q' || p == 'B')
        return BLACK;
      if (p != '.')
        break;
    }

  return NONE;
}

int
checkKnight(char board[8][8], piece* Kings)
{
  piece knight;

  knight.x = Kings[WHITE].x + 2;
  knight.y = Kings[WHITE].y + 1;
  if (knight.y < 8 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x + 1;
  knight.y = Kings[WHITE].y + 2;
  if (knight.y < 8 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x - 1;
  knight.y = Kings[WHITE].y + 2;
  if (knight.y > -1 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x - 2;
  knight.y = Kings[WHITE].y + 1;
  if (knight.y > -1 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x - 2;
  knight.y = Kings[WHITE].y - 1;
  if (knight.y > -1 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x - 1;
  knight.y = Kings[WHITE].y - 2;
  if (knight.y > -1 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x + 1;
  knight.y = Kings[WHITE].y - 2;
  if (knight.y < 8 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }
  knight.x = Kings[WHITE].x + 2;
  knight.y = Kings[WHITE].y - 1;
  if (knight.y < 8 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'n')
        return WHITE;
    }



  knight.x = Kings[BLACK].x + 2;
  knight.y = Kings[BLACK].y + 1;
  if (knight.y < 8 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x + 1;
  knight.y = Kings[BLACK].y + 2;
  if (knight.y < 8 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x - 1;
  knight.y = Kings[BLACK].y + 2;
  if (knight.y > -1 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x - 2;
  knight.y = Kings[BLACK].y + 1;
  if (knight.y > -1 && knight.x < 8)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x - 2;
  knight.y = Kings[BLACK].y - 1;
  if (knight.y > -1 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x - 1;
  knight.y = Kings[BLACK].y - 2;
  if (knight.y > -1 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x + 1;
  knight.y = Kings[BLACK].y - 2;
  if (knight.y < 8 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }
  knight.x = Kings[BLACK].x + 2;
  knight.y = Kings[BLACK].y - 1;
  if (knight.y < 8 && knight.x > -1)
    {
      if (board[knight.x][knight.y] == 'N')
        return BLACK;
    }

  return NONE;
}

int
main()
{
  int nGame = 0;
  char board[8][8];
  piece Kings[2];

  while (readBoard(board))
    {
      if (getKings(board, (piece*) Kings))
        break;

      switch (checkPawn(board, (piece*) Kings))
        {
      case BLACK:
        printf("Game #%d: black king is in check.\n", ++nGame);
        continue;
      case WHITE:
        printf("Game #%d: white king is in check.\n", ++nGame);
        continue;
        }

      switch (checkHorizontal(board, (piece*) Kings))
        {
      case BLACK:
        printf("Game #%d: black king is in check.\n", ++nGame);
        continue;
      case WHITE:
        printf("Game #%d: white king is in check.\n", ++nGame);
        continue;
        }

      switch (checkVertical(board, (piece*) Kings))
        {
      case BLACK:
        printf("Game #%d: black king is in check.\n", ++nGame);
        continue;
      case WHITE:
        printf("Game #%d: white king is in check.\n", ++nGame);
        continue;
        }

      switch (checkDiagonals(board, (piece*) Kings))
        {
      case BLACK:
        printf("Game #%d: black king is in check.\n", ++nGame);
        continue;
      case WHITE:
        printf("Game #%d: white king is in check.\n", ++nGame);
        continue;
        }

      switch (checkKnight(board, (piece*) Kings))
        {
      case BLACK:
        printf("Game #%d: black king is in check.\n", ++nGame);
        continue;
      case WHITE:
        printf("Game #%d: white king is in check.\n", ++nGame);
        continue;
        }

      printf("Game #%d: no king is in check.\n", ++nGame);
    }
  return 0;
}
Thank you guys.

Kind Regards,
Luan

Post Reply

Return to “Volume 101 (10100-10199)”