Skip to content

Instantly share code, notes, and snippets.

@joriki
Created January 9, 2023 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/20fb175c5902023f741b673bcf3ee503 to your computer and use it in GitHub Desktop.
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
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