QOJ.ac

QOJ

Time Limit: 8 s Memory Limit: 2048 MB Total points: 100

#5135. 售货亭建设

Statistics

你计划开办一个“布置精美的宁静露营地”。你已经买下了一块场地,并将其划分为一个 $h \times w$ 的网格,每个地块分别标有从 $1$ 到 $h \cdot w$ 的不同数字 $a_{i,j}$。然而,你遗漏了一件事:你还需要在其中一个地块上设置接待亭。你希望最小化任何客人从接待亭走到他们目的地块的最大距离。然而,客人不会走去往目的地块的最短路径,而是从接待亭出发,遵循以下步骤:

  • 查看四个相邻地块上的数字。
  • 前往数字与目的地数字最接近的地块。如果出现平局,则在两个平局的地块中,前往数字与当前地块数字最接近的地块。
  • 重复上述步骤,直到到达目的地。

注意,该过程在某些情况下可能不会终止。

给定地块的编号,找出接待亭的最佳位置(地块编号)以及从该接待亭到任何地块的最大步行距离。如果对于每一个可能的接待亭位置,都至少存在一个地块使得上述过程无法终止,则输出该情况为不可能。

图 K.1 展示了第三个样例:一种解决方案是将接待亭放在地块 4,这样到其他所有地块的距离最多为 3。将接待亭放在地块 7 则不可行,因为从那里无法到达地块 9。

图 K.1:第三个样例的可视化。

输入格式

输入包含:

  • 一行,包含两个整数 $h$ 和 $w$ ($2 \le h, w \le 40$),表示露营地的尺寸。
  • $h$ 行,第 $i$ 行包含 $w$ 个整数 $a_{i,1}, \dots, a_{i,w}$ ($1 \le a_{i,j} \le h \cdot w$),表示地块编号。保证从 $1$ 到 $h \cdot w$ 的所有数字各出现一次。

输出格式

如果存在一个接待亭位置使得其他所有地块都能到达,则输出接待亭的最佳位置(地块编号)以及相应的最大步行距离。否则,输出 “impossible”。

如果存在多个有效的解决方案,你可以输出其中任意一个。

样例

样例输入 1

2 3
1 2 3
6 5 4

样例输出 1

2 2

样例输入 2

3 3
1 4 8
7 5 2
3 9 6

样例输出 2

impossible

样例输入 3

3 3
9 3 1
4 7 2
8 6 5

样例输出 3

4 3

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.