7019.Swap Game

Time Limit: 2s Memory Limit: 512MB

You are given a $3 \times 3$ grid containing the numbers $1,2,\dots,9$ . Your task is to perform a sequence of moves so that the grid will look like this:
$$
\begin{matrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{matrix}
$$
On each move, you can swap the numbers in any two adjacent squares (horizontally or vertically). What is the minimum number of moves required?

Input Format(From the terminal/stdin)

The input has three lines, and each of them has three integers.

Output Format(To the terminal/stdout)

Print one integer: the minimum number of moves.

Sample Input

Copy
2 1 3
7 5 9
8 4 6
 · · \n
 · · \n
 · · \n

Sample Output

Copy
4
 \n
Source: CSES, Additional Problems I, 1670

Submit

请先 登录

© 2025 FAQs