4986. Taoism follows nature

单点时限: 4.0 sec

内存限制: 256 MB

$\textbf{Because of the long statement, we will offer you a Chinese version of the statement.}$

$\textit{Taiyi religion will always be prosperous.}$

Komorebi is one of the chief apprentice of Taiyi religion, he specially loves studying tactical deployments, so he usually practices them with his younger martial brothers.

A tactical deployment can be regarded as a matrix with $n\times n$ size, everyone can stand in only one of the position of the deployment, and each position of the deployment can only contain no more than one person.

However, his younger martial brothers always forget the deployment, so Komorebi has to move them to correct place. For each operation, Komorebi can move one martial brother to adjacent empty position. Formally, if one of the martial brother is located at $(x,y)(1\leq x,y\leq n)$, there are four options that cost $1$ operation if Komorebi wants to move him:

  1. If $x\gt 1$ and $(x-1,y)$ is empty, Komorebi can move him to $(x-1,y)$.

  2. If $y\gt 1$ and $(x,y-1)$ is empty, Komorebi can move him to $(x,y-1)$.

  3. If $x\lt n$ and $(x+1,y)$ is empty, Komorebi can move him to $(x+1,y)$.

  4. If $y\lt n$ and $(x,y+1)$ is empty, Komorebi can move him to $(x,y+1)$.

One position is empty means that no person stands on it.

Besides, $\textbf{don’t forget that Komorebi is also one member in the deployment}$, so he can stand on $\textbf{any}$ empty position at $\textbf{any}$ time, this costs $0$ operation, but if he wants to move after that, he should follow the rules above and also costs $1$ operation.

They need to practice $T$ tactical deployments, for each case, Komorebi will give you the ideal deployment and the current deployment, please help Komorebi to calculate the minimum number of operations to practice each of them.

以下是中文题面

$\textit{太乙长春。}$

Komorebi是太乙教的首席大弟子,他十分精于演练阵法,也乐在其中,因此他经常与师弟们一起演练阵法。

一个阵法可以看作一个$n\times n$的矩阵,每个人都可以站在其中一个格子里,当然每个格子最多也只能站一个人。

然而,他的师弟们经常会忘记阵法,因此Komorebi就需要将他们移动到正确的位置上。每次操作他都可以将一名师弟移动到一个相邻的格子上。具体来说,如果一名师弟在坐标为$(x,y)$ $(1\leq x,y\leq n)$的格子上,那么Komorebi就可以花费一点操作点选择以下操作中的一项:

  1. 如果 $x\gt 1$ 且 $(x-1,y)$ 这个格子是空的,那么就可以将他移动到$(x-1,y)$.
  2. 如果 $y\gt 1$ 且 $(x,y-1)$ 这个格子是空的,那么就可以将他移动到$(x,y-1)$.
  3. 如果 $x\lt n$ 且 $(x+1,y)$ 这个格子是空的,那么就可以将他移动到$(x+1,y)$.
  4. 如果 $y\lt n$ 且 $(x,y+1)$ 这个格子是空的,那么就可以将他移动到$(x,y+1)$.

如果一个格子上没有人站着,那么我们就认为它是空的。

此外,Komorebi也是阵法中的一员,他可以选择在任意时刻站在任意位置上,这个操作不消耗操作点,但如果他后续还想要移动的话,那么就需要遵循上述操作,此时也会消耗一点操作点。

他们需要演练$T$个阵法,对于每个阵法,Komorebi会告诉你正确的阵法以及目前师弟们排出来的阵法,请你告诉他如果要正确地演练每一个阵法最少各需要多少操作点。

输入格式

The first line contains one integer $T$ $(1\leq T\leq 100)$, indicating the number of tactical deployments.

For the following $T$ cases, the first line of them contains one integer $n$ $(1\leq n\leq 20)$, indicating the size of the deployment, then $n$ lines contain $n$ characters indicating the ideal deployment, and then $n$ lines contain $n$ characters indicating the current deployment.

Each position of each deployment is one of the following:

’.’: indicating an empty position.

’#’: indicating a position with a person.

Notice that the ideal deployment contains Komorebi, but the current one doesn’t, so the number of ‘#’ in the current deployment equals to the number of ‘#’ in the ideal deployment $-1$.

输出格式

For each case, output one line contains one integer indicating the answer.

样例

Input
2
4
#...
....
....
...#
..#.
....
....
....
1
#
.
Output
2
0

37 人解决,99 人已尝试。

58 份提交通过,共有 476 份提交。

6.2 EMB 奖励。

创建: 1 年,3 月前.

修改: 1 年,2 月前.

最后提交: 11 月前.

来源: 2023 年上海市大学生程序设计竞赛 - 一月赛

题目标签