Main topic:
Give you a eight digital sequence that requires you to revert to the initial shape within 50 steps, and if you can output its path, otherwise output this puzzle are not solvable.
Analytical:
This problem is realized by a * algorithm, online search a lot of information, understand what is a * algorithm.
A * algorithm, in essence, is based on the valuation function of BFS, each time the lowest value of the state, to move.
where f (n) is the valuation function from the initial point through the node N to the target point,
g (n) is the actual cost in the state space from the initial node to the N node,
H (N) is the estimated cost of the best path from N to the target node
F (n) = g (n) + H (n)
g (n) is the current number of moving steps in the question, and H (n) is the distance from the current to the end of the Manhattan
There is how to determine whether the 15 digital can meet the conditions.
Set E indicates the row where the empty slider is located, n is the number of sliders that appear after the current slider that are less than the current slider value, N:
15 15
N =∑ni =∑ni
i = 1 I = 2
If N + E is an even number, the current situation has a solution, otherwise there is no solution. such as the following situation:
13 10 11 6
5 7 4 8
1 12 14 9
3 15 2 0
Then there is less than 13 and after the number of sliders appears to be 12, less than 10 and after the number of sliders appears 9, similar, you can get
The other sliders have n values of 9 (11), 5 (6), 4 (5), 4 (7), 3 (4), 3 (8), 0 (1), 3 (12),
3 (14), 2 (9), 1 (3), 1 (15), 0 (2). All n values of the and n = 59, the empty slider on line 4th, so e = 4, then n + E = 63, is odd, then the above situation is not solvable.
The code was modified by me soon after a total time of 0.068s
#include <string> #include <queue> #include <map> #include <set> #include <cstring># Include <cstdlib> #include <cstdio>using namespace Std;int goal[18][2];set<string> vis;int dx[] = { 0,0,1,-1};int dy[] = {1,-1,0,0};struct state {int H, step, x, y; int board[4][4];string mov;bool operator < (const s Tate &RHS) Const {return H + Step > Rhs.h + rhs.step;}}; inline int Distanc (int x, int y, int xx, int yy) {return abs (X-XX) + ABS (Y-YY);} int get_h (int b[][4]) {int sum = 0;for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J + +) {if (b[i][j] = = 0 | | (i = = Goal[b[i][j]][0] && j = = goal[b[i][j]][1])) Continue;sum + = Distanc (i, J, Goal[b[i][j]][0], goal[b[i][j]][1]);}} return sum*4;} BOOL Isanswer (int b[][4]) {int sum = 0, z = 0, x, y, int tmp[16];for (int i = 0; i < 4; i++) {for (int j = 0; J < 4 ; J + +) {tmp[z++] = b[i][j];if (b[i][j] = = 0) x = i, y = j;}} for (int i = 0; i < n; i++) {for (int j = i + 1; j < +; J + +){if (Tmp[j] < Tmp[i] && tmp[j]) sum++;}} if (sum + x)% 2 = = 0) return False;return true;} BOOL Try_to_insert (int b[][4]) {string st;for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J + +) {st.push_back ((char ) (b[i][j]+ ' 0 '));}} if (Vis.count (ST)) {return false;} return true;} BOOL Astart (state a) {priority_queue <state> q;vis.clear (); int x, Y;state u, V;q.push (a), and while (!q.empty ()) {u = q.to P (); Q.pop (); for (int i = 0; i < 4; i++) {x = u.x + dx[i];y = u.y + dy[i];if (x >= 0 && y >= 0 && X < 4 && y < 4) {v = U;swap (V.board[x][y], v.board[u.x][u.y]); v.step = U.step + 1;if (V.step >) continue if (i = = 0) v.mov.push_back (' R '), else if (i = = 1) v.mov.push_back (' L '), else if (i = = 2) v.mov.push_back (' D '); else if (i = = 3) v.mov.push_back (' U '), if (V.mov.size () >= 2) {int back1 = V.mov.size ()-1;int Back2 = V.mov.size ()-2;if ((v.mov[b ACK1] = = ' U ' && v.mov[back2] = = ' D ') | | (V.mov[back1] = = ' D ' && v.mov[back2] = = ' U ') | | (V.MOV[BACK1] = = ' L ' && v.mov[back2] = = ' R ') | | (V.mov[back1] = = ' R ' && v.mov[back2] = = ' L ')) Continue;} V.h = Get_h (v.board); v.x = X;v.y = y;if (v.h = = 0) {printf ("%s\n", V.mov.c_str ()); return true;} if (Try_to_insert (V.board)) {Q.push (v);}}}} return false;} int main () {int n = 1, T;memset (goal,0,sizeof (goal)), for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J + +) {Goal[n] [0] = i;goal[n++][1] = j;}} scanf ("%d", &t), while (t--) {State start;for (int i = 0; i < 4; i++) {for (int j = 0; J < 4; J + +) {scanf ("%d",& START.BOARD[I][J]); if (start.board[i][j] = = 0) Start.x = i, Start.y = j;}} if (!isanswer (Start.board)) {printf ("This puzzle was not solvable.\n"); continue;} Start.h = 0;start.step = 0;bool final_res = Astart (start), if (!final_res) printf ("This puzzle was not solvable.\n");} return 0;}
UVA-10181 15-puzzle Problem (15 digital *)