We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static HashMap<HashMap<Integer,Integer>,Integer> hm = new HashMap<>();
public static int grundy(int x,int y){
HashMap<Integer,Integer> p = new HashMap<>();
p.put(x,y);
if(hm.containsKey(p)) return hm.get(p);
HashSet<Integer> set = new HashSet<>();
if(x>=1 && y>=1 && x<=15 && y<=15){
set.add(grundy(x-2, y+1));
set.add(grundy(x-2, y-1));
set.add(grundy(x+1, y-2));
set.add(grundy(x-1, y-2));
}
int mex;
for(int i = 0; ; i++)
{
if(set.contains(i)){
continue;
}
mex = i;
break;
}
// g = !g;
hm.put(p, mex);
return mex;
}
public static String chessboardGame(int[][] coins) {
// Write your code here
int xorsum = 0;
for(int i = 0; i < coins.length; i++){
int x = coins[i][0];
int y = coins[i][1];
// System.out.println("x="+x+" y ="+y);
int g = grundy(x, y);
xorsum ^= g;
// System.out.println(g);
}
// System.out.println(hm);
return xorsum == 0?"Second":"First";
}
}
public class Solution {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int k = sc.nextInt();
int[][] arr = new int[k][2];
for(int i = 0 ; i < k; i++){
arr[i][0] = sc.nextInt();
arr[i][1] = sc.nextInt();
}
String result = Result.chessboardGame(arr);
System.out.println(result);
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Chessboard Game, Again!
You are viewing a single comment's thread. Return to all comments →
Can anyone help me below mentioned code showing wrong result for last 3 test case.
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
// class Result {
}
public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int k = sc.nextInt(); int[][] arr = new int[k][2]; for(int i = 0 ; i < k; i++){ arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } String result = Result.chessboardGame(arr); System.out.println(result); } } }