Chessboard Game, Again!
Chessboard Game, Again!
+ 0 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 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); } } }
+ 0 comments Here is Chessboard game again! problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-chessboard-game-again-problem-solution.html
+ 0 comments Too difficult for me
+ 1 comment A good one . If You understand grandy theorem of game theory , then it is very easy for you to solve recursively .When our possible moves from a co-ordinate of chess-board has been given .We can compute grandy-value recursively using the help of memoization .
My full solution here https://github.com/joy-mollick/Game-Theory-Problems/blob/master/HackerRank-Chessboard%20Game%2C%20Again!.cpp
Here is the part of my code to compute grandy value of every co-ordinate by given input .
int sg(int x,int y) { if((x==1&&y==1)) { return 0; /// (1,1) it is base case ,because from here there is no move,so grandy value of this state is zero } if(grandy_value[x][y]!=-1) /// if grandy value is already computed just return it , no need to calculate it { return grandy_value[x][y]; } set<int>s; for(int i=0;i<4;i++) { int newx=x+dx[i]; int newy=y+dy[i]; if(newx>=1&&newy>=1&&newx<=15&&newy<=15)///if newx and newy is beyond (1,1) then this recursion will be recursively run ,there is no end .So (1,1) should be the terminal point { s.insert(sg(newx,newy)); } } int mex=0; while(s.find(mex)!=s.end()) mex++; grandy_value[x][y]=mex;///assigning mex [not included minimum element in the set of grandy value of possible co-ordinates] return grandy_value[x][y];/// return grandy_value }
+ 0 comments What? A cell can contain more than one coin. Ok, when can not a player make a new move?
Sort 17 Discussions, By:
Please Login in order to post a comment