This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_puzzle.cpp
86 lines (80 loc) · 2.44 KB
/
15_puzzle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using state = tuple<int, int, vector<int>, int, int>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
int lut[16][16] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6},
{1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4, 4, 3, 4, 5},
{2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3, 5, 4, 3, 4},
{3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2, 6, 5, 4, 3},
{1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5},
{2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3, 3, 2, 3, 4},
{3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2, 4, 3, 2, 3},
{4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1, 5, 4, 3, 2},
{2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3, 1, 2, 3, 4},
{3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2, 2, 1, 2, 3},
{4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1, 3, 2, 1, 2},
{5, 4, 3, 2, 4, 3, 2, 1, 3, 2, 1, 0, 4, 3, 2, 1},
{3, 4, 5, 6, 2, 3, 4, 5, 1, 2, 3, 4, 0, 1, 2, 3},
{4, 3, 4, 5, 3, 2, 3, 4, 2, 1, 2, 3, 1, 0, 1, 2},
{5, 4, 3, 4, 4, 3, 2, 3, 3, 2, 1, 2, 2, 1, 0, 1}};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
inline int heuristic(int depth, const vector<int> &board)
{
for (int i = 1; i < 16; ++i)
{
depth += lut[board[i]][i];
}
return depth;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> initial(16);
int x0, y0;
for (int i = 0; i < 16; ++i)
{
cin >> initial[i];
if (initial[i] == 0)
{
x0 = i % 4;
y0 = i / 4;
}
}
min_heap<state> pq;
set<vector<int>> set;
set.emplace(initial);
pq.emplace(heuristic(0, initial), 0, initial, x0, y0);
while (!pq.empty())
{
auto [cost, depth, board, x, y] = pq.top();
pq.pop();
if (depth == cost)
{
cout << depth << '\n';
break;
}
for (int i = 0; i < 4; ++i)
{
int nx = x + dx[i], ny = y + dy[i];
if (0 <= nx && nx < 4 && 0 <= ny && ny < 4)
{
swap(board[4 * ny + nx], board[4 * y + x]);
if (set.find(board) == set.end())
{
pq.emplace(heuristic(depth + 1, board), depth + 1, board, nx, ny);
set.emplace(board);
}
swap(board[4 * ny + nx], board[4 * y + x]);
}
}
}
}