QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#15149. Knight's Spirit

Statistics

On a $5 \times 5$ chessboard, there are 12 white knights, 12 black knights, and one empty space. At any time, a knight can move into the empty space according to the standard knight's move (it can move to a square where the difference in the x-coordinate is 1 and the difference in the y-coordinate is 2, or the difference in the x-coordinate is 2 and the difference in the y-coordinate is 1).

Given an initial board configuration, how can it be transformed into the following target board through a sequence of moves:

To embody the spirit of the knight, they must complete the task in the minimum number of steps.

Input

The first line contains a positive integer $T$ ($T \le 10$), representing the total number of test cases. This is followed by $T$ $5 \times 5$ matrices, where $0$ represents a white knight, $1$ represents a black knight, and $*$ represents the empty space. There are no empty lines between data sets.

Output

For each test case, output one line. If the target state can be reached within 15 steps (inclusive), output the number of steps; otherwise, output $-1$.

Examples

Input 1

2
10110
01*11
10111
01001
00000
01011
110*1
01110
01010
00100

Output 1

7
-1

Note

The initial configuration of the second data set in the example corresponds to the following figure:

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.