Header Ad

HackerEarth End Game problem solution

In this HackerEarth End Game problem solution, Black and White are playing a game of chess on a chessboard of n X n dimensions. The game is nearing its end. White has his King and a Pawn left. Black has only his King left. So, definitely Black cannot win the game. However, Black can drag the game towards a draw, and, he can do this only if he captures White's only left pawn. White knows exactly what he must do so that he wins the game. He is aware of the fact that if his Pawn reaches Row 'n' (topmost row as seen by White), he is allowed to replace that Pawn with a Queen. Since White plays chess regularly, he knows how to beat the opponent in a situation in which the opponent has only a King left and he (White) has a King as well as a Queen left. The current scenario of the game will be given to you. You must tell whether this game will end with a Draw or will it end with White winning.

If Move = 0, it is currently White's move, and if Move = 1, it is currently Black's move.

Black and White play alternately.

White's piece will be captured by Black if Black's King manages to reach the same square in which White's piece is currently.

White's king is currently placed at the point (1,n). White is in a hurry to convert his pawn into a queen, so, in every move of his, he only plays his pawn forward ( towards row n ), ignoring his king.

So, naturally, Black plays his King towards White's Pawn in order to capture it.

White's Pawn is currently located at the point (a,b).

Black's King is currently located at the point (c,d).

Pawn movement on a chessboard: Pawn can move only one step forward, provided no piece is blocking it from doing so. ie. it can move only from (u,v) to (u+1,v), provided there is no piece (white or black) at (u+1,v)

King movement on a chess board : King can move one step in any direction. ie if it is at (u,v) currently, then it can move to (u+1,v) , (u-1,v) , (u,v+1) , (u,v-1) , (u+1,v+1) , (u+1, v-1) , (u-1,v+1) , (u-1,v-1), provided it remains inside the chess board

If at any moment the White Pawn is blocked by the black king from moving forward, white will be forced to play his king in such a case.


HackerEarth End Game problem solution


HackerEarth End Game problem solution.

#include <stdio.h>
long long int mod(long long int a)
{
if(a<0)
return(-a);
return(a);
}
int main()
{
long long int n,a,b,c,d,t,move;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld %lld %lld %lld %lld",&n,&a,&b,&c,&d,&move);
if(move==0)
{
if(c>=a && mod(d-b)<=(n-a))
{
printf("Draw\n");
}
else
{
printf("White Wins\n");
}
}
else
{
if(c>=a-1 && mod(d-b)<=(n-a+1))
{
printf("Draw\n");
}
else
{
printf("White Wins\n");
}
}
}
return 0;
}


Second solution

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<cassert>
using namespace std;
#define ll long long int
#define rep(i,n) FOR(i,0,n)
#define si(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define highest (ll)(1e9)

int main()
{
int t,n,a,b,val,c,d,move,white,black,val1,val2;
si(t);
assert(t>=1 && t<=100000);
while(t--)
{
white=0;
black=0;
si(n);
si(a);
si(b);
si(c);
si(d);
si(move);
assert(n>=5 && n<=highest);
assert(a>=2 && a<=n);
assert(b>=1 && b<=n);
assert(c>=1 && c<=n);
assert(d>=1 && d<=n);
assert(!(c==1 && d==n));
assert(!(c==1 && d==n-1));
assert(!(c==2 && d==n));
assert(!(c==2 && d==n-1));
assert(!(c==a && d==b));
val=abs(b-d);
black=c+val;
white=a+val;
if(move==1)
white--;
if(white>black || white>n)
printf("White Wins\n");
else
printf("Draw\n");
}
return 0;
}

Post a Comment

0 Comments