311206: CF1949J. Amanda the Amoeba

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

J. Amanda the Amoebatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

This problem has an attachment. You can use it to simulate and visualize the movements of the amoeba.

Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid movement. In each step of such a movement, her body first shrinks by one pixel (one pixel of the body is removed and becomes free), and then grows at a different place (one previously-free pixel is added to the body).

To prevent structural damage, Amanda's body always occupies a connected region of pixels, which means that any pair of pixels forming the body can be connected by a sequence of adjacent pixels without ever leaving the body. Two pixels are considered adjacent if they share a common side (each pixel has at most 4 neighbours). The body remains connected even during the movement, including the moment after removing a pixel and before adding another one.

Your task is to help Amanda find her way around. Given her initial position and desired final position, suggest a sequence of valid moves leading from the former to the latter.

Illustration of sample $1$: The filled shape is the initial position, the dotted region is the final position.
Input

The first line contains two integers $r$ and $c$ ($1\le r,c \le 50$) — the size of the rectangular grid in pixels.

The next $r$ lines contain $c$ characters each, describing the initial position of Amanda. Each of those characters is either a dot $\texttt{.}$ denoting a free pixel, an asterisk $\texttt{*}$ denoting Amanda's body, or an $\texttt{X}$ denoting a blocked pixel which may never be occupied.

The next line is empty.

The next $r$ lines describe the desired final position in the same format as the initial position.

It is guaranteed that:

  • The number of pixels forming Amanda's body is the same in both positions, and it is at least 2.
  • The body of Amanda is connected in the initial position.
  • The body of Amanda is connected in the final position.
  • The blocked pixels do not change between the descriptions of the initial and final position, their placement is exactly the same in both positions.
Output

Print $\texttt{YES}$ if it is possible for Amanda to go from the initial position to the final one. Otherwise, print $\texttt{NO}$.

If it is possible, on the next line print one integer $m$ ($0\le m\le 10\,000$) — the number of moves to execute.

The following $m$ lines must contain four integer coordinates each: $i_1$, $j_1$, $i_2$, $j_2$ ($1\le i_1,i_2\le r$, $1\le j_1,j_2\le c$). These four coordinates specify one move, meaning that the pixel at $i_1$-th row and $j_1$-th column is first removed from the body. Then, $(i_2,j_2)$ must designate a different location where one pixel is added.

The sequence should consist only of valid moves and after the last move, Amanda's body should occupy the desired final position.

If there are multiple solutions, print any of them.

Under the assumptions of this problem, it can be proven that if it is possible for Amanda to go from the initial position to the desired final one, then it is possible to do it with at most $10\,000$ moves.

ExamplesInput
5 8
.******.
**.X**..
*******.
**.X**..
.******.

.******. ...X**** .******* ...X**** .******.
Output
YES
5
3 1 3 8
2 1 2 8
4 1 4 8
2 2 4 7
4 2 2 7
Input
2 5
*.X..
**X..

..X** ..X*.
Output
NO
Note

In the first sample, Amanda executes 5 moves to reach the final position, as shown in the figure below.

Output

题目大意:
阿米巴阿曼达在一个由方形像素组成的矩形网格内生活。她的身体占据了一些像素。其他像素可能是自由的,也可能是被阻挡的。阿曼达通过所谓的阿米巴运动在网格中移动。在这种运动的每一步中,她的身体首先缩小一个像素(身体的一个像素被移除并变为自由),然后在不同的地方生长(一个先前自由的像素被添加到身体中)。

为了防止结构损伤,阿曼达的身体始终占据一个相连的像素区域,这意味着形成身体的任何两个像素都可以通过一系列相邻像素连接起来,而不会离开身体。如果两个像素共享一个公共边(每个像素最多有4个邻居),则认为它们是相邻的。即使在移动过程中,包括移除一个像素之后和添加另一个像素之前的时刻,身体仍然是相连的。

你的任务是帮助阿曼达找到她的路。给定她初始位置和期望的最终位置,建议一个从前者到后者的有效移动序列。

输入输出数据格式:
输入:
- 第一行包含两个整数 r 和 c(1≤r,c≤50)——矩形网格的大小(像素)。
- 接下来的 r 行,每行包含 c 个字符,描述阿曼达的初始位置。每个字符要么是一个点 . 表示自由像素,要么是一个星号 * 表示阿曼达的身体,要么是一个 X 表示被阻挡的像素,这个像素永远不会被占据。
- 下一行是空行。
- 接下来的 r 行以与初始位置相同的方式描述期望的最终位置。

保证:
- 在两个位置中,形成阿曼达身体的像素数量是相同的,至少为2。
- 阿曼达的身体在初始位置是相连的。
- 阿曼达的身体在最终位置是相连的。
- 被阻挡的像素在初始位置和最终位置的描述之间不会改变,它们的位置在两个位置中是完全相同的。

输出:
- 如果阿曼达可以从初始位置移动到最终位置,则打印 YES,否则打印 NO。
- 如果可以移动,则在下一行打印一个整数 m(0≤m≤10,000)——要执行的移动次数。
- 接下来的 m 行必须每行包含四个整数坐标:i1, j1, i2, j2(1≤i1,i2≤r,1≤j1,j2≤c)。这四个坐标指定一个移动,意味着首先移除位于第 i1 行第 j1 列的像素。然后,(i2,j2) 必须指定一个不同的位置,在该位置添加一个像素。
- 序列应该只包含有效的移动,并且在最后一个移动之后,阿曼达的身体应该占据期望的最终位置。
- 如果有多个解决方案,则打印其中任何一个。

根据这个问题的假设,可以证明,如果阿曼达可以从初始位置移动到期望的最终位置,那么她最多可以用 10,000 次移动来完成。题目大意: 阿米巴阿曼达在一个由方形像素组成的矩形网格内生活。她的身体占据了一些像素。其他像素可能是自由的,也可能是被阻挡的。阿曼达通过所谓的阿米巴运动在网格中移动。在这种运动的每一步中,她的身体首先缩小一个像素(身体的一个像素被移除并变为自由),然后在不同的地方生长(一个先前自由的像素被添加到身体中)。 为了防止结构损伤,阿曼达的身体始终占据一个相连的像素区域,这意味着形成身体的任何两个像素都可以通过一系列相邻像素连接起来,而不会离开身体。如果两个像素共享一个公共边(每个像素最多有4个邻居),则认为它们是相邻的。即使在移动过程中,包括移除一个像素之后和添加另一个像素之前的时刻,身体仍然是相连的。 你的任务是帮助阿曼达找到她的路。给定她初始位置和期望的最终位置,建议一个从前者到后者的有效移动序列。 输入输出数据格式: 输入: - 第一行包含两个整数 r 和 c(1≤r,c≤50)——矩形网格的大小(像素)。 - 接下来的 r 行,每行包含 c 个字符,描述阿曼达的初始位置。每个字符要么是一个点 . 表示自由像素,要么是一个星号 * 表示阿曼达的身体,要么是一个 X 表示被阻挡的像素,这个像素永远不会被占据。 - 下一行是空行。 - 接下来的 r 行以与初始位置相同的方式描述期望的最终位置。 保证: - 在两个位置中,形成阿曼达身体的像素数量是相同的,至少为2。 - 阿曼达的身体在初始位置是相连的。 - 阿曼达的身体在最终位置是相连的。 - 被阻挡的像素在初始位置和最终位置的描述之间不会改变,它们的位置在两个位置中是完全相同的。 输出: - 如果阿曼达可以从初始位置移动到最终位置,则打印 YES,否则打印 NO。 - 如果可以移动,则在下一行打印一个整数 m(0≤m≤10,000)——要执行的移动次数。 - 接下来的 m 行必须每行包含四个整数坐标:i1, j1, i2, j2(1≤i1,i2≤r,1≤j1,j2≤c)。这四个坐标指定一个移动,意味着首先移除位于第 i1 行第 j1 列的像素。然后,(i2,j2) 必须指定一个不同的位置,在该位置添加一个像素。 - 序列应该只包含有效的移动,并且在最后一个移动之后,阿曼达的身体应该占据期望的最终位置。 - 如果有多个解决方案,则打印其中任何一个。 根据这个问题的假设,可以证明,如果阿曼达可以从初始位置移动到期望的最终位置,那么她最多可以用 10,000 次移动来完成。

加入题单

上一题 下一题 算法标签: