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 :



title-img


                            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

Dynamic Array

Create a list, seqList, of n empty sequences, where each sequence is indexed from 0 to n-1. The elements within each of the n sequences also use 0-indexing. Create an integer, lastAnswer, and initialize it to 0. There are 2 types of queries that can be performed on the list of sequences: 1. Query: 1 x y a. Find the sequence, seq, at index ((x xor lastAnswer)%n) in seqList.

View Solution →

Left Rotation

A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. Given an integer, d, rotate the array that many steps left and return the result. Example: d=2 arr=[1,2,3,4,5] After 2 rotations, arr'=[3,4,5,1,2]. Function Description: Complete the rotateLeft function in the editor below. rotateLeft has the following parameters: 1. int d

View Solution →

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →