I came across this question. According to the question:
‘samu’ and ‘vibhu’ are playing a game where there are N integers from 1 to N lying on the table. In each turn, the player gets to pick one integer to mark as visited from among the previously unvisited ones. If in a turn, the player picks a number which completes the picking of three consecutive numbers, he wins. i.e., say at some stage in the game 2 and 4 are already picked(visited) if the player now picks 3, he wins. Assuming samu starts first and both the players play perfectly optimally, who is the winner.
I tried to apply the WL algorithm (after understanding it properly)described here which is:
boolean isWinning(position pos) {
moves[] = possible positions to which I can move from the position pos;
for (all x in moves)
if (!isWinning(x)) return true;
return false;
}
So, my code is(after suitably modifying the WL algorithm):
public class HelloWorld{
public static boolean[] visited;
public static int n;
public static void main(String []args){
n=12;
visited=new boolean[n];
java.util.Arrays.fill(visited,false);
for(int i=0; i<n; i++){
visited[i]=true;
if(isWinning(i)){
System.out.println("first one wins");
System.exit(0);
}
visited[i]=false;
}
System.out.println("second one wins");
}
public static boolean isWinning(int x){
if(threeStrikes()){
return true;
}
for(int i=0; i<n; i++){
if(!visited[i]){
visited[i]=true;
if(!isWinning(i)){
return true;
}
visited[i]=false;
}
}
return false;
}
public static boolean threeStrikes(){
for(int i=0; i<n-2; i++){
if(visited[i]&&visited[i+1]&&visited[i+2]){
return true;
}
}
return false;
}
}
The problem is that it prints "first one wins" for n=6. In this discussion thread, the op has said that for n=6, the second one should win. I don't know if he is wrong or if my code is missing something. Any help is appreciated.