#P10578. [蓝桥杯 2024 国 A] 旋转九宫格

    ID: 12059 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>搜索2024广度优先搜索 BFS蓝桥杯国赛

[蓝桥杯 2024 国 A] 旋转九宫格

Problem Description

Given a 3×33 \times 3 grid, each cell contains a number, and all numbers in the grid are distinct. In each step, we may choose any 2×22 \times 2 sub-square and rotate it clockwise, for example:

1 2 3
4 5 6
7 8 9

If we rotate the top-right 2×22 \times 2 sub-square, we get:

1 5 2
4 6 3
7 8 9

Find the minimum number of steps needed to rotate the given state into

1 2 3
4 5 6
7 8 9

Input Format

The first line contains an integer TT, the number of queries.

Then follow TT queries.

Each query consists of three lines, each containing three numbers, describing the current state of the 3×33 \times 3 grid.

Output Format

Output TT lines. Each line contains one integer, the answer for that query.

2
1 2 3
4 5 6
7 8 9
1 5 2
4 6 3
7 8 9
0
3

Hint

For 60%60\% of the testdata, T=1T = 1.
For all testdata, T105T \le 10^5.

Translated by ChatGPT 5