Problem list
Log in
#coci00014 · COCI 2006/2007 Contest 3

N-puzzle

Unrated 1.0 s 32 MB

$N$-puzzle is a puzzle that goes by many names and has many variants. In this problem we will use the $15$-puzzle. It consists of a $4$-by-$4$ grid of sliding squares where one square is missing. The squares are labeled with uppercase letters A through O, with the desired layout as follows:

A    B    C    D
E    F    G    H
I    J    K    L
M    N    O    .

It can be useful (for example, when solving the puzzle using a computer) to define the scatter of a puzzle as the sum of distances between each square's current position and its position in the desired layout. The distance between two squares is their Manhattan distance (the absolute value of the sum of differences between the two rows and the two columns).

Input

Four lines of input contain four characters each, representing the state of the puzzle.

Output

Output the scatter of the puzzle on a single line.

Example 1

Input
ABCD
EFGH
IJKL
M.NO
Output
2

Example 2

Input
.BCD
EAGH
IJFL
MNOK
Output
6
Source
COCI 2006/2007 Contest 3
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 2 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code