Created
January 9, 2023 19:05
-
-
Save joriki/20fb175c5902023f741b673bcf3ee503 to your computer and use it in GitHub Desktop.
count the domino tilings of a rectangle; see https://math.stackexchange.com/questions/4613898
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
public class Question4613898 { | |
final static int w = 6; | |
final static int h = 6; | |
static int x,y; | |
static boolean [] [] used = new boolean [w] [h]; | |
static int count; | |
static int deadEnds; | |
public static void main(String [] args) { | |
recurse (); | |
System.out.println(count); | |
System.out.println(deadEnds); | |
} | |
static void recurse () { | |
int oldX = x; | |
int oldY = y; | |
try { | |
while (used [x] [y]) | |
if (++x == w) { | |
x = 0; | |
if (++y == h) { | |
count++; | |
return; | |
} | |
} | |
used [x] [y] = true; | |
if (!(attempt (x,y + 1) | attempt (x + 1,y))) | |
deadEnds++; | |
used [x] [y] = false; | |
} finally { | |
x = oldX; | |
y = oldY; | |
} | |
} | |
static boolean attempt (int nx,int ny) { | |
boolean free = nx < w && ny < h && !used [nx] [ny]; | |
if (free) { | |
used [nx] [ny] = true; | |
recurse (); | |
used [nx] [ny] = false; | |
} | |
return free; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment