Skip to content

Commit 34df18a

Browse files
authored
Update 15-puzzle.md
1 parent 962b516 commit 34df18a

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/others/15-puzzle.md

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,39 @@
22

33
# 15 Puzzle Game: Existence Of The Solution
44

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:
66

7-
![some image description](http://e-maxx.ru/tex2png/cache/a20a65d11dd5382b08465279d9bef0ca.png)
7+
![15 puzzle goal position](http://e-maxx.ru/tex2png/cache/a20a65d11dd5382b08465279d9bef0ca.png)
88

99
The game "15 Puzzle” was created by Noyes Chapman in 1880.
1010

11-
12-
1311
## Existence Of The Solution
1412

13+
Let's consider this problem: given position on the board, determine whether a sequence of moves which leads to a solution exists.
1514

15+
Suppose we have some position on the board:
1616

17-
Let's consider this problem: given position on the board to determine whether exists such a sequence of moves, which leads to a solution.
18-
19-
Suppose, we have some position on the board:
20-
21-
![some image description](http://e-maxx.ru/tex2png/cache/a20a65d11dd5382b08465279d9bef0ca.png)
17+
![15 puzzle board position](http://e-maxx.ru/tex2png/cache/4bce648a6f89c9fc25289fa27fd7a107.png)
2218

2319
where one of the elements equals zero and indicates an empty cell $a_z = 0$
2420

2521
Let’s consider the permutation:
2622

27-
![some image description](http://e-maxx.ru/tex2png/cache/37db640bfba4e1e8372e3098c8e0b1f1.png)
23+
$$a_1 a_2 ... a_{z-1} a_{z+1} ... a_{15} a_{16}$$
2824

29-
(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)
3026

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$).
3228

3329
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$).
3430

3531
Then, **the solution exists iff $N + K$ is even**.
3632

37-
38-
3933
## Implementation
4034

41-
42-
4335
The algorithm above can be illustrated with the following program code:
4436

45-
````cpp
37+
```cpp
4638
int a[16];
4739
for (int i=0; i<16; ++i)
4840
cin >> a[i];
@@ -58,14 +50,16 @@ for (int i=0; i<16; ++i)
5850
inv += 1 + i / 4;
5951

6052
puts ((inv & 1) ? "No Solution" : "Solution Exists");
61-
````
62-
53+
```
6354
6455
## Proof
6556
66-
6757
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.
6858
6959
However, all these proofs were quite complex.
7060
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)).
62+
63+
## Practice Problems
64+
65+
* [Hackerrank - N-puzzle](https://www.hackerrank.com/challenges/n-puzzle)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy