Created
October 2, 2025 15:27
-
-
Save nst/1877490070be99d2dc16c7ee8df5c35d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * @file solver.c | |
| * @brief A minimal backtracking solver for a 3x3 square edge-matching puzzle. | |
| */ | |
| /** | |
| gcc emp_solver.c -o emp_solver -Wall && ./emp_solver "A0-grBP,B0-pBPg,C0-bgBR,D0-pGRb,E0-RpgP,F0-brGP,G0-rGRb,H0-gbPR,I0-pgRP" | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| char *b[9], *ps[9]; | |
| int u[9]; | |
| void s(int p) { | |
| if (p > 8) { | |
| for (int i = 0; i < 9; i++) printf("%.2s%s", b[i], i > 7 ? "" : ","); | |
| puts(""); | |
| return; | |
| } | |
| for (int i = 0; i < 9; i++) { | |
| if (u[i]) continue; | |
| char t[8]; | |
| strcpy(t, ps[i]); | |
| for (int r = 0; r < 4; r++) { | |
| t[1] = '0' + r; | |
| if ((p % 3 == 0 || (t[3 + (3 + r) % 4] ^ 32) == b[p - 1][3 + (1 + b[p - 1][1] - '0') % 4]) && | |
| (p < 3 || (t[3 + r % 4] ^ 32) == b[p - 3][3 + (2 + b[p - 3][1] - '0') % 4])) { | |
| u[i] = 1; | |
| b[p] = strdup(t); | |
| s(p + 1); | |
| free(b[p]); | |
| u[i] = 0; | |
| } | |
| } | |
| } | |
| } | |
| int main(int c, char **v) { | |
| if (c != 2) return 1; | |
| char *k = strdup(v[1]); | |
| int i = 0; | |
| for (char *t = strtok(k, ","); t; t = strtok(NULL, ",")) ps[i++] = strdup(t); | |
| free(k); | |
| if (i != 9) return 1; | |
| s(0); | |
| for (i = 0; i < 9; i++) free(ps[i]); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment