QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 2048 MB 満点: 100

#9817. 粘合网格

統計

滑动拼图由矩形网格上的方形图块组成。网格中恰好有一个位置是空的,因此你可以将空位上方、下方、左侧或右侧的图块移动到空位中。

每个图块都标有一个唯一的数字,如图 G.1 所示。要解决滑动拼图问题,你需要找到一系列移动步骤,将图块数字按从左到右、从上到下的顺序排列为升序,并使空方块最终位于网格的右下角,如图 G.2 所示。并非所有的滑动拼图都能通过一系列移动来解决。

图 G.1:3x4 滑动拼图示例,对应样例输入 1。

图 G.2:图 G.1 的滑动拼图解开后的状态。

图 G.3:带有胶水固定图块的滑动拼图示例,覆盖着绿色粘液。此示例可以解开,对应样例输入 3。

在清理车库时,你与车库地精为了一个装满好东西的发光盒子发生了争执。他们提出了一个赌局:如果你能解开他们所有的滑动拼图,盒子就归你。你接受了挑战,却发现车库地精把一些图块粘在了原地!被粘住的图块无法移动,如图 G.3 所示。

然而,所有的滑动拼图都具有以下共同属性: 空方块位于右下角位置。 每个被粘住的图块都处于其正确位置。 对于每个可以移动的图块,都存在一系列移动步骤,使得空方块最终回到该图块当前的位置。 从任何被粘住的图块出发,仅通过被粘住的图块,总能找到一条到达滑动拼图边界的路径(每一步只能向上、下、左、右移动)。也就是说,没有被粘住的图块被可移动的图块包围。

确定给定的滑动拼图是否可以解开。

输入格式

输入包含: 一行,包含两个整数 $h$ 和 $w$ ($1 \le h, w \le 500$),表示滑动拼图网格的高度和宽度。 $h$ 行,每行包含 $w$ 个字符,每个字符为 ‘.’ 或 ‘#’。右下角的字符(空方块的位置)为 ‘.’。此外,‘.’ 表示可以移动的图块,‘#’ 表示被粘住的图块。 * $h$ 行,每行包含 $w$ 个整数 $a_{i,j}$ ($1 \le i \le h, 1 \le j \le w, 0 \le a_{i,j} \le h \cdot w - 1$)。右下角的整数为 $a_{h,w} = 0$,代表空方块。否则,$a_{i,j}$ 是位置 $(i, j)$ 处图块上的标签。

所有 $a_{i,j}$ ($1 \le i \le h, 1 \le j \le w$) 的集合恰好包含 $0, 1, \dots, h \cdot w - 1$ 中的每个数字一次。

输出格式

如果滑动拼图可以解开,输出 possible,否则输出 impossible

样例

样例输入 1

3 4
....
....
....
10 8 2 3
6 7 5 9
11 1 4 0

样例输出 1

possible

样例输入 2

2 4
....
....
2 1 3 4
5 6 7 0

样例输出 2

impossible

样例输入 3

3 4
..#.
....
#...
5 1 3 4
2 6 7 8
9 10 11 0

样例输出 3

possible

样例输入 4

3 4
..#.
....
#...
7 1 3 4
5 6 2 8
9 10 11 0

样例输出 4

impossible

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.