In this HackerRank Vertical Rooks problem solution, HackerChess is a variant of chess played at HackerRank. It is a game played between two players who make moves in turns until one of them cannot make any move. The player who cannot make a move loses the game and the other player is declared the winner. The game is played on a board with N rows and N columns. The only pieces used in the game are rooks. A rook in HackerChess moves only vertically, which means that it never leaves a column to which it belongs. Moreover, in a single move, a rook moves through any number of unoccupied cells. Notice that there are no captures in HackerChess, two rooks cannot occupy the same cell, and a rook cannot jump over another rook. Each player has exactly one rook in each of the N columns of the board.
Given the initial position of the rooks and knowing that the second player makes the first move, decide who will win the game if both players play optimally.
Problem solution in Python.
#!/bin/python3 import os import sys # # Complete the verticalRooks function below. # def verticalRooks(r1, r2): xors = 0 for y1,y2 in zip(r1,r2): xors ^= (abs(y1-y2) - 1) return "player-2" if xors != 0 else "player-1" if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') t = int(input()) for t_itr in range(t): n = int(input()) r1 = [] for _ in range(n): r1_item = int(input()) r1.append(r1_item) r2 = [] for _ in range(n): r2_item = int(input()) r2.append(r2_item) result = verticalRooks(r1, r2) fptr.write(result + 'n') fptr.close()
Problem solution in Java.
import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; public class Solution { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int T = scanner.nextInt(); while(T-- > 0) { int n = scanner.nextInt(); int[] one = new int[n]; for(int i=0; i<n; i++) { one[i] = scanner.nextInt(); } int[] two = new int[n]; for(int i=0; i<n; i++) { two[i] = scanner.nextInt(); } int xor = 0; for(int i=0; i<n; i++) { xor ^= (Math.abs(one[i] - two[i]) - 1); } if(xor != 0) { System.out.println("player-2"); } else { System.out.println("player-1"); } } } scanner.close(); } }
Problem solution in C++.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N,T; int P1[2010]; int P2[2010]; int nimSum ; int heapSize; scanf("%d",&T); while(T--){ nimSum = 0; scanf("%d",&N); for(int i=0;i<N;++i){ scanf("%d",&P1[i]); } for(int i=0;i<N;++i){ scanf("%d",&P2[i]); } heapSize = 0; for(int i=0;i<N;++i){ heapSize = abs(P1[i]-P2[i])-1; nimSum = (heapSize ^ nimSum); } if(nimSum){ printf("player-2n"); } else printf("player-1n"); } return 0; }
Problem solution in C.
#include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> long long i,j,k,l,m,n,a[10000],t,tt; int main() { scanf("%lld",&t); for(tt=0;tt<t;tt++) { scanf("%lld",&n); for(i=0;i<n;i++) scanf("%lld",a+i); m = 0; for(i=0;i<n;i++) { scanf("%lld",&j); j -= a[i]; if(j<0) j = -j; j--; m = m^j; } if(m) printf("player-2n"); else printf("player-1n"); } return 0; }