Cerinta completa

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:

chess(1)

Given the initial coordinates of the players’ coins, assuming optimal play, determine which player will win the game.

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 .

Constraints

Output Format

On a new line for each test case, print if the first player is the winner. Otherwise, print .

Sample Input

3
5 2
5 3
8 8

Sample Output

Second
First
First

Explanation

In the first case, player1 starts at the red square and can move to any of the blue squares. Regardless of which one is chosen, the player 2 can move to one of the green squares to win the game.

image

In the second case, player 1 starts at the red square and can move to any of the blue squares or the purple one. Moving to the purple one limits player 2 to the yellow square. From the yellow square, player 1 moves to the green square and wins.

image


Limbajul de programare folosit: java8

Cod:

import java.util.Scanner;

public class Solution {
	static final int SIZE = 15;
	static final int[] R_OFFSETS = { -1, 1, 2, 2 };
	static final int[] C_OFFSETS = { 2, 2, 1, -1 };

	static boolean[][] firstWins;

	public static void main(String[] args) {
		buildFirstWins();

		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for (int tc = 0; tc < t; tc++) {
			int x = sc.nextInt();
			int y = sc.nextInt();

			System.out.println(firstWins[x - 1][y - 1] ? "First" : "Second");
		}
		sc.close();
	}

	static void buildFirstWins() {
		firstWins = new boolean[SIZE][SIZE];
		for (int sum = 0; sum <= (SIZE - 1) * 2; sum++) {
			for (int r = 0; r <= sum; r++) {
				int c = sum - r;
				if (isInBoard(r, c) && !firstWins[r][c]) {
					for (int i = 0; i < R_OFFSETS.length; i++) {
						int nextR = r + R_OFFSETS[i];
						int nextC = c + C_OFFSETS[i];

						if (isInBoard(nextR, nextC)) {
							firstWins[nextR][nextC] = true;
						}
					}
				}
			}
		}
	}

	static boolean isInBoard(int r, int c) {
		return r >= 0 && r < SIZE && c >= 0 && c < SIZE;
	}
}

Scor obtinut: 1.0

Submission ID: 464603619

Link challenge: https://www.hackerrank.com/challenges/a-chessboard-game-1/problem

A Chessboard Game