You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/others/15-puzzle.md
+15-21Lines changed: 15 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -2,47 +2,39 @@
2
2
3
3
# 15 Puzzle Game: Existence Of The Solution
4
4
5
-
This game is played on board, which size is 4×4 cells. On this board there are 15 playing tiles numbered from 1 to 15. One cell is left empty. It is required to come to position presented below by moving some tiles to the free space:
5
+
This game is played on a $4 \times 4$ board. On this board there are 15 playing tiles numbered from 1 to 15. One cell is left empty. You need to get the board to the position presented below by repeatedly moving one of the tiles to the free space:
(i.e. the permutation of numbers corresponding to the position on the board, without a zeroth element)
25
+
(i.e. the permutation of numbers corresponding to the position on the board without a zero element)
30
26
31
-
Let $N$ be a quantity of inversions in this permutation (i.e. the quantity of such elements $a_i$ and $a_j$ that $i < j$, but $a_i > a_j$).
27
+
Let $N$ be the number of inversions in this permutation (i.e. the number of such elements $a_i$ and $a_j$ that $i < j$, but $a_i > a_j$).
32
28
33
29
Suppose $K$ is an index of a row where the empty element is located (i.e. in our indications $K = (z - 1) \ div \ 4 + 1$).
34
30
35
31
Then, **the solution exists iff $N + K$ is even**.
36
32
37
-
38
-
39
33
## Implementation
40
34
41
-
42
-
43
35
The algorithm above can be illustrated with the following program code:
44
36
45
-
````cpp
37
+
```cpp
46
38
int a[16];
47
39
for (int i=0; i<16; ++i)
48
40
cin >> a[i];
@@ -58,14 +50,16 @@ for (int i=0; i<16; ++i)
58
50
inv += 1 + i / 4;
59
51
60
52
puts ((inv & 1) ? "No Solution" : "Solution Exists");
61
-
````
62
-
53
+
```
63
54
64
55
## Proof
65
56
66
-
67
57
In 1879 Johnson proved that if $N + K$ is odd, then the solution doesn’t exist, and in the same year Story proved that all positions when $N + K$ is even have a solution.
68
58
69
59
However, all these proofs were quite complex.
70
60
71
-
In 1999 Archer proposed much more simple proof (you can download his article [here](http://www.cs.cmu.edu/afs/cs/academic/class/15859-f01/www/notes/15-puzzle.pdf)).
61
+
In 1999 Archer proposed a much simpler proof (you can download his article [here](http://www.cs.cmu.edu/afs/cs/academic/class/15859-f01/www/notes/15-puzzle.pdf)).
0 commit comments