Created
November 11, 2017 17:34
-
-
Save amitport/32dcb977280315c3b16cc1ba3059ebe4 to your computer and use it in GitHub Desktop.
snake cube puzzle solver
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.Stack; | |
public class Solver { | |
static boolean[][][] cube = new boolean[4][4][4]; | |
static int[] segments = {3,1,2,1,1,3,1,2,1,2}; | |
static enum Dir { | |
UP,DOWN,RIGHT,LEFT,IN,OUT | |
} | |
static Stack<String> trace = new Stack<String>(); | |
/* | |
* x z | |
* | / | |
* | / | |
* |/ | |
* -----y | |
*/ | |
public static boolean tryMove(boolean changedDir,Dir dir, int x, int y, int z, int steps, int segmentsLeft){ | |
if (steps==0) { | |
if (segmentsLeft > 0){ | |
int newSteps = segments[segments.length-segmentsLeft]; | |
switch (dir){ | |
case UP: | |
case DOWN: | |
return | |
(tryMove(true,Dir.RIGHT,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.LEFT,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.IN,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.OUT,x,y,z,newSteps,segmentsLeft-1)); | |
case RIGHT: | |
case LEFT: | |
return | |
(tryMove(true,Dir.UP,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.DOWN,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.IN,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.OUT,x,y,z,newSteps,segmentsLeft-1)); | |
case IN: | |
case OUT: | |
return | |
(tryMove(true,Dir.UP,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.DOWN,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.RIGHT,x,y,z,newSteps,segmentsLeft-1) | |
|| | |
tryMove(true,Dir.LEFT,x,y,z,newSteps,segmentsLeft-1)); | |
} | |
} else return true; | |
} else { | |
if (changedDir) { | |
trace.push(dir.toString() + " " + steps); | |
} | |
} | |
switch (dir) { | |
case UP: | |
if (x<3 && cube[x+1][y][z] == false) { | |
cube[x+1][y][z] = true; | |
boolean res = tryMove(false,dir, x+1, y, z, steps-1, segmentsLeft); | |
if (res == false) cube[x+1][y][z] = false; | |
return res; | |
} | |
break; | |
case DOWN: | |
if (x>0 && cube[x-1][y][z] == false){ | |
cube[x-1][y][z] = true; | |
boolean res = tryMove(false,dir, x-1, y, z, steps-1, segmentsLeft); | |
if (res == false) cube[x-1][y][z] = false; | |
return res; | |
} | |
break; | |
case RIGHT: | |
if (y<3 && cube[x][y+1][z] == false){ | |
cube[x][y+1][z] = true; | |
boolean res = tryMove(false,dir, x, y+1, z, steps-1, segmentsLeft); | |
if (res == false) cube[x][y+1][z] = false; | |
return res; | |
} | |
break; | |
case LEFT: | |
if (y>0 && cube[x][y-1][z] == false){ | |
cube[x][y-1][z] = true; | |
boolean res = tryMove(false,dir, x, y-1, z, steps-1, segmentsLeft); | |
if (res == false) cube[x][y-1][z] = false; | |
return res; | |
} | |
break; | |
case IN: | |
if (z<3 && cube[x][y][z+1] == false){ | |
cube[x][y][z+1] = true; | |
boolean res = tryMove(false,dir, x, y, z+1, steps-1, segmentsLeft); | |
if (res == false) cube[x][y][z+1] = false; | |
return res; | |
} | |
break; | |
case OUT: | |
if (z>0 && cube[x][y][z-1] == false){ | |
cube[x][y][z-1] = true; | |
boolean res = tryMove(false,dir, x, y, z-1, steps-1, segmentsLeft); | |
if (res == false) cube[x][y][z-1] = false; | |
return res; | |
} | |
break; | |
} | |
trace.pop(); | |
return false; | |
} | |
public static void main(String[] args) { | |
for (int x=0; x<4; x++) | |
for (int y=0; y<4; y++) | |
for (int z=-1; z<3; z++){ | |
if (tryMove(true,Dir.IN, x, y, z, segments[0], segments.length-1) == true){ | |
System.out.println(""+x + y + z + " " + trace); | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment