## COCI '06 Contest 3 #2 N-puzzle

View as PDF

Points: 5
Time limit: 1.0s
Memory limit: 32M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

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

Write a program that calculates the scatter of the given puzzle.

#### Input Specification

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

#### Output Specification

Output the scatter of the puzzle on a single line.

#### Sample Input 1

ABCD
EFGH
IJKL
M.NO

#### Sample Output 1

2

#### Sample Input 2

.BCD
EAGH
IJFL
MNOK

#### Sample Output 2

6

• commented on April 10, 2018, 8:08 p.m.

~[user: ~]