A Chessboard Game
Problem Statement :
Two players are playing a game on a chessboard. The rules of the game are as follows: The game starts with a single coin located at some coordinates. The coordinates of the upper left cell are , and of the lower right cell are . In each move, a player must move the coin from cell to one of the following locations: Note: The coin must remain inside the confines of the board. Beginning with player 1, the players alternate turns. The first player who is unable to make a move loses the game. The figure below shows all four possible moves using an board for illustration: Function Description Complete the chessboardGame function in the editor below. It should return a string, either First or Second. chessboardGame has the following parameter(s): x: an integer that represents the starting column position y: an integer that represents the starting row position Input Format The first line contains an integer , the number of test cases. Each of the next lines contains space-separated integers and .
Solution :
Solution in C :
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
scanf("%d",&t);
while(t--)
{
int x,y;
scanf("%d %d",&x,&y);
int dp[15][15];
x--;
y--;
dp[1][0]=dp[0][0]=dp[0][1]=dp[1][1]=1;
int i=2;
while(i<15)
{
int temp=0;
while(temp<=i)
{
if((i-2>=0&&temp+1<15?dp[i-2][temp+1]:0)||(i-2>=0&&temp-1>=0?dp[i-2][temp-1]:0)||(i+1<15&&temp-2>=0?dp[i+1][temp-2]:0)||(i-1>=0&&temp-2>=0?dp[i-1][temp-2]:0))
dp[i][temp]=0;
else
dp[i][temp]=1;
if((i-2>=0&&temp+1<15?dp[temp+1][i-2]:0)||(i-2>=0&&temp-1>=0?dp[temp-1][i-2]:0)||(i+1<15&&temp-2>=0?dp[temp-2][i+1]:0)||(i-1>=0&&temp-2>=0?dp[temp-2][i-1]:0))
dp[temp][i]=0;
else
dp[temp][i]=1;
temp++;
}
i++;
}
if(dp[x][y])
printf("Second\n");
else
printf("First\n");
}
return 0;
}
Solution in C++ :
In C++ :
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
long DP[16][16];
long dx[4]={-2,-2,1,-1};
long dy[4]={1,-1,-2,-2};
bool inside(long u,long v)
{
return (u>0 && v>0 && u<=15 && v<=15);
}
bool dp(long x,long y)
{
if (DP[x][y]!=-1) return DP[x][y];
bool res=false;
for (long i=0; i<4; ++i)
{
long xx=x+dx[i],yy=y+dy[i];
if (inside(xx,yy)) res=res|(!dp(xx,yy));
}
DP[x][y]=res;
return res;
}
int main()
{
long nTest,x,y;
memset(DP,-1,sizeof(DP));
DP[1][1]=DP[1][2]=DP[2][1]=DP[2][2]=false;
scanf("%ld",&nTest);
while (nTest--)
{
scanf("%ld%ld",&x,&y);
puts(dp(x,y)?"First":"Second");
}
}
Solution in Java :
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
private static void pasteTessalation(boolean[][] board, int r, int c) {
board[r][c] = true;
board[r][c+1] = true;
board[r+1][c] = true;
board[r+1][c+1] = true;
}
public static void main(String[] args) {
boolean[][] loss = new boolean[16][16];
for (int i=0; i<16; i+=4) {
for (int j=0; j<16; j+=4) {
pasteTessalation(loss,i,j);
}
}
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t=0; t<T; t++) {
int r = sc.nextInt()-1;
int c = sc.nextInt()-1;
if (loss[r][c]) {
System.out.println("Second");
} else {
System.out.println("First");
}
}
}
}
Solution in Python :
in Python3 :
import copy
move = [(-2,1),(-2,-1),(1,-2),(-1,-2)]
grid_copy = set()
for i in range(1,16):
for j in range(1,16):
grid_copy.add((i,j))
First = set()
Second = set()
t=0
while len(First) + len(Second) != 225:
if t%2 ==0:
for i,j in grid_copy:
n=0
for x,y in move:
if (i+x,j+y) not in grid_copy or (i+x,j+y) in First:
n+=1
if n==4:
Second.add((i,j))
else:
for i,j in grid_copy:
for x,y in move:
if (i+x,j+y) in Second:
First.add((i,j))
t+=1
#print('--------')
#print(sorted(list(First)))
#print(len(First))
#print(sorted(list(Second)))
#print(len(Second))
test = int(input())
for _ in range(test):
x,y = map(int,input().strip().split())
if (x,y) in First:
print('First')
else:
print('Second')
View More Similar Problems
Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →Tree: Huffman Decoding
Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t
View Solution →Binary Search Tree : Lowest Common Ancestor
You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b
View Solution →Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →