#P6315. Manhattan Distance on a 4x4 Board

    ID: 19532 Type: Default 1000ms 256MiB

Manhattan Distance on a 4x4 Board

Manhattan Distance on a 4x4 Board

Given a $4 \times 4$ chessboard containing the 15 letters from $A$ to $O$ and one blank represented by ., each letter has its standard position.

The standard configuration is as follows:

$$\begin{array}{|c|c|c|c|} \hline A & B & C & D \\ \hline E & F & G & H \\ \hline I & J & K & L \\ \hline M & N & O & . \\ \hline \end{array} $$

You are given a scrambled board. For each letter $X$ (from $A$ to $O$), let its current position be $(r_1, c_1)$ and its standard (target) position be $(r_2, c_2)$. The Manhattan distance for letter $X$ is defined as:

d(X)=r1r2+c1c2.d(X)=|r_1-r_2|+|c_1-c_2|.

Your task is to calculate the total sum of Manhattan distances for all letters from their current positions to their standard positions.

inputFormat

The input consists of 4 lines. Each line contains 4 tokens (each token is either one of the uppercase letters A to O or a period . representing the empty slot), separated by spaces, describing the board rows.

outputFormat

Output a single integer which is the sum of the Manhattan distances of all letters from their current positions to their standard positions.

sample

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