From c31bc9f50e49403463251bc0034e2b9224be141e Mon Sep 17 00:00:00 2001 From: berarchegas Date: Mon, 19 Aug 2024 20:45:40 -0300 Subject: [PATCH] Puzzle game in cpp with comments and tests --- bfs/puzzle_game/puzzle_game.cpp | 77 ++++++++++++++++++++++ bfs/puzzle_game/{data.in => tests/t0.in} | 0 bfs/puzzle_game/{data.out => tests/t0.out} | 0 bfs/puzzle_game/tests/t1.in | 9 +++ bfs/puzzle_game/tests/t1.out | 2 + bfs/puzzle_game/tests/t2.in | 13 ++++ bfs/puzzle_game/tests/t2.out | 3 + bfs/puzzle_game/tests/t3.in | 13 ++++ bfs/puzzle_game/tests/t3.out | 3 + 9 files changed, 120 insertions(+) create mode 100644 bfs/puzzle_game/puzzle_game.cpp rename bfs/puzzle_game/{data.in => tests/t0.in} (100%) rename bfs/puzzle_game/{data.out => tests/t0.out} (100%) create mode 100644 bfs/puzzle_game/tests/t1.in create mode 100644 bfs/puzzle_game/tests/t1.out create mode 100644 bfs/puzzle_game/tests/t2.in create mode 100644 bfs/puzzle_game/tests/t2.out create mode 100644 bfs/puzzle_game/tests/t3.in create mode 100644 bfs/puzzle_game/tests/t3.out diff --git a/bfs/puzzle_game/puzzle_game.cpp b/bfs/puzzle_game/puzzle_game.cpp new file mode 100644 index 0000000..7bf75b6 --- /dev/null +++ b/bfs/puzzle_game/puzzle_game.cpp @@ -0,0 +1,77 @@ +/* +General solution: Each board gets mapped to an integer in the range [0, 9^9) +We visualize the board as a 9 digit number written in base 9 and then convert it to base 10 +We then simply run a BFS trying to reach the end state +*/ +#include + +using namespace std; + +const int N = 381367045; + +// A bitset to check visited states +bitset vis; +// Precomputate the powers of 9 +int pwr[10], dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; +map primes = {{2, true}, {3, true}, {5, true}, {7, true}, {11, true}, {13, true}, {17, true}}; + +int mapping(vector> &v) { + int x = 0; + for (int i = 2; i >= 0; i--) { + for (int j = 2; j >= 0; j--) { + x = 9 * x + v[i][j] % 9; + } + } + return x; +} + +int main () { + vector> v = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + int goal = mapping(v); + int t; + cin >> t; + while (t--) { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + cin >> v[i][j]; + } + } + queue>, int>> q; + q.push({v, 0}); + int mp = mapping(v); + vis[mp] = true; + vector visited_states = {mp}; + while (!q.empty()) { + v = q.front().first; + int dist = q.front().second; + q.pop(); + if (mapping(v) == goal) { + cout << dist << '\n'; + break; + } + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + for (int i = 0; i < 4; i++) { + int nx = x + dx[i], ny = y + dy[i]; + if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3 && primes[v[x][y] + v[nx][ny]]) { + swap(v[x][y], v[nx][ny]); + int mp = mapping(v); + if (!vis[mp]) { + vis[mp] = true; + visited_states.push_back(mp); + q.push({v, dist + 1}); + } + swap(v[x][y], v[nx][ny]); + } + } + } + } + } + if (!vis[goal]) cout << "-1\n"; + // resetting our bitset for the next test case + for (int x : visited_states) { + vis[x] = false; + } + } + return 0; +} \ No newline at end of file diff --git a/bfs/puzzle_game/data.in b/bfs/puzzle_game/tests/t0.in similarity index 100% rename from bfs/puzzle_game/data.in rename to bfs/puzzle_game/tests/t0.in diff --git a/bfs/puzzle_game/data.out b/bfs/puzzle_game/tests/t0.out similarity index 100% rename from bfs/puzzle_game/data.out rename to bfs/puzzle_game/tests/t0.out diff --git a/bfs/puzzle_game/tests/t1.in b/bfs/puzzle_game/tests/t1.in new file mode 100644 index 0000000..fdbbcb7 --- /dev/null +++ b/bfs/puzzle_game/tests/t1.in @@ -0,0 +1,9 @@ +2 + +5 9 1 +2 8 4 +3 6 7 + +9 8 7 +6 5 4 +3 2 1 \ No newline at end of file diff --git a/bfs/puzzle_game/tests/t1.out b/bfs/puzzle_game/tests/t1.out new file mode 100644 index 0000000..9cfc6a2 --- /dev/null +++ b/bfs/puzzle_game/tests/t1.out @@ -0,0 +1,2 @@ +-1 +48 \ No newline at end of file diff --git a/bfs/puzzle_game/tests/t2.in b/bfs/puzzle_game/tests/t2.in new file mode 100644 index 0000000..15f05fc --- /dev/null +++ b/bfs/puzzle_game/tests/t2.in @@ -0,0 +1,13 @@ +3 + +1 9 2 +8 3 7 +4 6 5 + +4 5 6 +1 2 3 +7 8 9 + +7 8 9 +1 2 3 +4 5 6 \ No newline at end of file diff --git a/bfs/puzzle_game/tests/t2.out b/bfs/puzzle_game/tests/t2.out new file mode 100644 index 0000000..6680978 --- /dev/null +++ b/bfs/puzzle_game/tests/t2.out @@ -0,0 +1,3 @@ +52 +21 +-1 \ No newline at end of file diff --git a/bfs/puzzle_game/tests/t3.in b/bfs/puzzle_game/tests/t3.in new file mode 100644 index 0000000..876f07e --- /dev/null +++ b/bfs/puzzle_game/tests/t3.in @@ -0,0 +1,13 @@ +3 + +4 1 2 +9 3 5 +6 7 8 + +4 3 2 +7 6 5 +1 9 8 + +9 5 1 +2 8 7 +6 4 3 \ No newline at end of file diff --git a/bfs/puzzle_game/tests/t3.out b/bfs/puzzle_game/tests/t3.out new file mode 100644 index 0000000..afb1374 --- /dev/null +++ b/bfs/puzzle_game/tests/t3.out @@ -0,0 +1,3 @@ +24 +-1 +44 \ No newline at end of file