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

lazyboy
New poster
Posts: 17
Joined: Tue Jul 08, 2008 3:19 am

Re: 10196 - Check The Check

dear ThanatosX....

I didn't check your code but my ACC code produces output as same as yours.

you used " char board; "
try by increase the size of board[][] >8
Thanks.

chackal_sjc
New poster
Posts: 2
Joined: Wed Jun 09, 2010 5:53 am

Re: 10196 - Check The Check

Hey guys,

My algorithm is getting WA but its output is correct when trying all kinds of inputs comparing to UVa toolkit website.
Here is my code http://ideone.com/GwoGP

Thank you
UNIFEI - Universidade Federal de Itajubá, Brazil

New poster
Posts: 1
Joined: Fri Nov 12, 2010 10:57 am

check the check

in the problem check the check , i keep getting a WA,
i checked every element in the chess bored ,and check the boundaries of each element...
:S any idea ?
here is my code
#include <iostream>

using namespace std;
void Pawn(char game);
void Rook(char game);
void Bishop(char game);
void Knight(char game);
int main(){
int count = 0;
char game;
bool k = false,K = false;
bool con = false;

int i, j;
while(!con ){
count++;
con = false;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
cin>>game[j];
if(game[j] != '.')
con = true;
}
}

if(con == false)
return 0;

Pawn(game);

Rook(game);

Bishop(game);

Knight(game);

for(i =0; i<8; i++)
{

for(j=0; j<8; j++)
{

if(game[j] == 'k')
k = true;
else if(game[j] == 'K')
K = true;

}

}

if( k == true && K == true || k == false && K == false)
cout<<"Game #"<<count<<": no king is in check."<<endl;
else if(k == true && K== false)
cout<<"Game #"<<count<<": white king is in check."<<endl;
else if (K == true && k== false)
cout<<"Game #"<<count<<": black king is in check."<<endl;

}

return 0;

}

void Pawn(char game){

int i, j;
for(i =0; i<8; i++)
for(j=0; j<8; j++)
{
if(game[j] == 'p')
{
if( i<7)
{
if( j > 0 && game[i+1][j-1] == 'K')
game[i+1][j-1] = '*';
if( j < 7 && game[i+1][j+1] == 'K')
game[i+1][j+1] = '*';

}
}

if(game[j] == 'P')
{
if( i>0)
{
if( j > 0 && game[i-1][j-1] == 'k')
game[i-1][j-1] = '*';
if( j < 7 && game[i-1][j+1] == 'k')
game[i-1][j+1] = '*';
}
}

}
}

void Rook(char game)
{
int i, j, k;

for(i =0; i<8; i++)
for(j=0; j<8; j++)
{
if(game[j] == 'R' || game[j] == 'Q')
{
if(i<7)
for( k = i+1; k < 8; k++)
if(game[k][j] == 'k')
game[k][j] = '*';
else if(game[k][j] != '.')
break;

if(i>0)
for( k = i-1; k >= 0; k--)
if(game[k][j] == 'k')
game[k][j] = '*';
else if(game[k][j] != '.')
break;
if(j <7)
for( k = j-1; k >= 0; k--)
if(game[k] == 'k')
game[k] = '*';
else if(game[i][k] != '.')
break;
if(j > 0)
for( k = j+1; k < 8; k++)
if(game[i][k] == 'k')
game[i][k] = '*';
else if(game[i][k] != '.')
break;

}
}

for(i =0; i<8; i++)
for(j=0; j<8; j++)
{
if(game[i][j] == 'r' || game[i][j] == 'q')
{
if(i<7)
for( k = i+1; k < 8; k++)
if(game[k][j] == 'K')
game[k][j] = '*';
else if(game[k][j] != '.')
break;

if(i>0)
for( k = i+1; k >= 0; k--)
if(game[k][j] == 'K')
game[k][j] = '*';
else if(game[k][j] != '.' )
break;
if(j <7)
for( k = j-1; k >= 0; k--)
if(game[i][k] == 'K')
game[i][k] = '*';
else if(game[i][k] != '.')
break;
if(j > 0)
for( k = j+1; k < 8; k++)
if(game[i][k] == 'K')
game[i][k] = '*';
else if(game[i][k] != '.')
break;

}
}
}

void Bishop(char game)
{
int i, j;
int r, c;

for(i =0; i<8; i++)
for(j=0; j<8; j++)
{

if(game[i][j] == 'b' || game[i][j] == 'q')
{
r = i+1; c = j+1;
while( r <= 7 && c <= 7)
{
if(game[r][c] == 'K')
game[r][c] = '*';

else if(game[r][c] != '.')
break;
r++;c++;
}
r = i-1; c = j-1;

while( r >= 0 && c >=0)
{
if(game[r][c] == 'K')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r--;c--;
}
r = i+1; c = j-1;

while( r <= 7 && c >=0)
{
if(game[r][c] == 'K')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r++;c--;
}
r = i-1; c = j+1;

while( r >= 0 && c <=7)
{
if(game[r][c] == 'K')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r--;c++;
}
}

}

for(i =0; i<8; i++)
for(j=0; j<8; j++)
{

if(game[i][j] == 'B' || game[i][j] == 'Q')
{
r = i+1; c = j+1;
while( r <= 7 && c <= 7)
{
if(game[r][c] == 'k')
game[r][c] = '*';

else if(game[r][c] != '.')
break;
r++;c++;
}
r = i-1; c = j-1;

while( r >= 0 && c >=0)
{
if(game[r][c] == 'k')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r--;c--;
}
r = i+1; c = j-1;

while( r <= 7 && c >=0)
{
if(game[r][c] == 'k')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r++;c--;
}
r = i-1; c = j+1;

while( r >= 0 && c <=7)
{
if(game[r][c] == 'k')
game[r][c] = '*';
else if(game[r][c] != '.')
break;
r--;c++;
}
}

}

}
void Knight(char game)
{

int i,j;

for(i=0; i<8; i++)
for(j=0; j<8; j++)
{
if( game[i][j] == 'n')
{
if((i+2) <= 7)
{
if(j+1 <= 7)
if(game[i+2][j+1] == 'K')
game[i+2][j+1] = '*';
else if(game[i+2][j+1] != '.')
break;

if(j-1>=0)
if(game[i+2][j-1] == 'K')
game[i+2][j-1] = '*';
else if(game[i+2][j-1] != '.')
break;

}

if(i-2 <= 0)
{
if(j+1 <= 7)
if(game[i-2][j+1] == 'K')
game[i-2][j+1] = '*';
else if(game[i-2][j+1] != '.')
break;

if(j-1>=0)
if(game[i-2][j-1] == 'K')
game[i-2][j-1] = '*';
else if(game[i-2][j-1] != '.')
break;

}

if(j+2 <= 7)
{
if(i+1 <= 7)
if(game[i+1][j+2] == 'K')
game[i+1][j+2] = '*';
else if(game[i+1][j+2] != '.')
break;

if(i-1>=0)
if(game[i-1][j+2] == 'K')
game[i-1][j+2] = '*';
else if(game[i-1][j+2] != '.')
break;

}

if(j-2 >= 0)
{
if(i+1 <= 7)
if(game[i+1][j-2] == 'K')
game[i+1][j-2] = '*';
else if(game[i+1][j-2] != '.')
break;

if(i-1>=0)
if(game[i-1][j-2] == 'K')
game[i-1][j-2] = '*';
else if(game[i-1][j-2] != '.')
break;
}
}

if( game[i][j] == 'N')
{
if((i+2) <= 7)
{
if(j+1 <= 7)
if(game[i+2][j+1] == 'k')
game[i+2][j+1] = '*';
else if(game[i+2][j+1] != '.')
break;

if(j-1>=0)
if(game[i+2][j-1] == 'k')
game[i+2][j-1] = '*';
else if(game[i+2][j-1] != '.')
break;

}

if(i-2 <= 0)
{
if(j+1 <= 7)
if(game[i-2][j+1] == 'k')
game[i-2][j+1] = '*';
else if(game[i-2][j+1] != '.')
break;

if(j-1>=0)
if(game[i-2][j-1] == 'k')
game[i-2][j-1] = '*';
else if(game[i-2][j-1] != '.')
break;

}

if(j+2 <= 7)
{
if(i+1 <= 7)
if(game[i+1][j+2] == 'k')
game[i+1][j+2] = '*';
else if(game[i+1][j+2] != '.')
break;

if(i-1>=0)
if(game[i-1][j+2] == 'k')
game[i-1][j+2] = '*';
else if(game[i-1][j+2] != '.')
break;

}

if(j-2 >= 0)
{
if(i+1 <= 7)
if(game[i+1][j-2] == 'k')
game[i+1][j-2] = '*';
else if(game[i+1][j-2] != '.')
break;

if(i-1>=0)
if(game[i-1][j-2] == 'k')
game[i-1][j-2] = '*';
else if(game[i-1][j-2] != '.')
break;
}
}

}
}

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 10196 - Check The Check

Nice problem, although it actually tests your patience and how much cautious you are ...May be I enjoyed solving this because I like to play chess If you get Wrong Answer, just try the sample inputs posted by jud (Thanks jud nice I/O) on page 2. Be careful when you are checking whether the pawn threatens the king or not. And the problem does not say anything about cases like:

Code: Select all

...b....
......P.
.....P..
........
....k...
........
........
..p....K
The problem could have been better if it had cases like this. As you can see, the black pawn reaches the last rank and so it can be promoted to a queen. Now if it is promoted to a queen/rook the white king is threatened. However the output for this problem should be no king is in check(There wont be cases like this don't be confused. I'm just trying to say if the problem mentioned that if a pawn reaches the last rank, it would be automatically promoted to a queen.)
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 10196 - Check The Check

And follow stcheung's advice. His 5th advice is very important (Actually it saves you a lot of time and your code will be shorter so there will be lesser probability for error to creep in). I created a 40 x 40 char array and stored the board in the middle of my array.
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10196 - Check The Check

Input:

Code: Select all

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

........
........
........
........
........
........
........
........
Check input and AC output for thousands of problems on uDebug!

kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

Re: 10196 - Check The Check

I tested all the cases but I still got wrong answer..
Here is my code:

Code: Select all

//code accepted
}

can you guys help me find my mistake?
Last edited by kier.guevara on Wed Sep 05, 2012 9:42 am, edited 1 time in total.

kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

Re: 10196 - Check The Check

Tried another solution but still wrong.
What is wrong with this?

Code: Select all

//code accepted
Last edited by kier.guevara on Wed Sep 05, 2012 9:43 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10196 - Check The Check

kier.guevara wrote:Tried another solution but still wrong.
What is wrong with this?
Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10196 - Check The Check

kier.guevara wrote:I tested all the cases but I still got wrong answer..
Here is my code:

can you guys help me find my mistake?
If row and column equal zero don't try to read board[row-1][column-1]
Check input and AC output for thousands of problems on uDebug!

kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

Re: 10196 - Check The Check

Thanks brianfry! my code got accepted!

connor
New poster
Posts: 4
Joined: Wed Jan 09, 2013 1:41 pm

Re: 10196 - Check The Check

brianfry713 wrote:
kier.guevara wrote:Tried another solution but still wrong.
What is wrong with this?
Doesn't match the sample I/O.
i don't why my code got runtime error

plz help 3ks;

got AC with help from brianfry713
Last edited by connor on Sat Jan 12, 2013 4:38 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10196 - Check The Check

Maybe there is an input like:

Code: Select all

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

........
........
........
........
........
........
........
........
Check input and AC output for thousands of problems on uDebug!

connor
New poster
Posts: 4
Joined: Wed Jan 09, 2013 1:41 pm

Re: 10196 - Check The Check

brianfry713 wrote:Maybe there is an input like:

Code: Select all

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

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

!!!great help!!!
got ac finally
printf("thank you ")
for(i=1;;i++)printf("very ");
printf("much!!!");

raj
Learning poster
Posts: 78
Joined: Fri Feb 15, 2013 5:39 pm

Re: 10196 - Check The Check

deleted...
Last edited by raj on Tue Mar 05, 2013 11:41 pm, edited 3 times in total.