Created
March 28, 2016 22:14
-
-
Save joriki/d9a9a16102351156bb9a to your computer and use it in GitHub Desktop.
Find a red, green and black fake rock among five of each colour by asking seven questions; see http://math.stackexchange.com/questions/1716750.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Question1716750 { | |
final static int nrocks = 5; | |
final static int ncolours = 3; | |
final static int ntotal = nrocks * ncolours; | |
final static int nquestions = 7; | |
final static int ncases = (int) Math.pow (nrocks,ncolours); | |
final static int nsubsets = 1 << (ntotal - 1); | |
public static void main (String [] args) { | |
List<Integer> cases = new ArrayList<Integer> (); | |
for (int i = 0;i < ncases;i++) | |
cases.add (i); | |
System.out.println (recurse (cases,nquestions,"")); | |
} | |
static boolean recurse (List<Integer> cases,int questionsLeft,String prefix) { | |
if (questionsLeft == 2) | |
return true; | |
System.out.println (prefix + "depth " + questionsLeft + " : " + cases.size ()); | |
questionsLeft--; | |
prefix += " "; | |
for (int subset = 0;subset < nsubsets;subset++) { | |
List<Integer> cases1 = new ArrayList<Integer> (); | |
List<Integer> cases2 = new ArrayList<Integer> (); | |
for (int c : cases) | |
(contains (subset,c) ? cases1 : cases2).add (c); | |
if (cases1.size () <= 1 << questionsLeft && cases2.size () <= 1 << questionsLeft) { | |
System.out.println (prefix + "subset: " + Integer.toBinaryString (subset)); | |
if (recurse (cases1,questionsLeft,prefix) && recurse (cases2,questionsLeft,prefix)) | |
return true; | |
} | |
} | |
return false; | |
} | |
static boolean contains (int subset,int c) { | |
for (int i = 0;i < ncolours;i++,c = c / nrocks) | |
if ((subset & (1 << (i * nrocks + (c % nrocks)))) != 0) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment