QOJ.ac

QOJ

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

#3172. 古墓丽影

統計

古墓丽影 Hu 进入了一座新的古墓!里面充满了石像鬼、镜子和障碍物。前方有一扇门,门后藏着宝藏。Hu 必须解开守护宝藏的门。门上用古老的语言写着开启大门的秘诀:

Every face of every gargoyle shall see a face of a gargoyle. (每个石像鬼的每一张脸都必须看到另一张石像鬼的脸。)

这意味着必须旋转石像鬼,使得光束能够连接每一个石像鬼的脸到另一个石像鬼的脸(可能是其自身)。光束可以在镜子中反射。

古墓的平面图可以用一个 $n \times m$ 的网格来描述:

  • 点(.)表示空单元格。
  • 井号(#)表示障碍物。
  • 斜杠(/)表示双面镜,反斜杠(\)也表示双面镜。
  • 字符 V 表示一个两张脸分别朝向顶部和底部的石像鬼。
  • 字符 H 表示一个两张脸分别朝向左侧和右侧的石像鬼。

除了 \/ 镜子外,古墓周围还环绕着镜子墙。关于光线,遵循以下常识:

  1. 光线在空单元格中沿直线传播。
  2. 两条光束可以相交而不互相干扰。
  3. \ 镜子将来自顶部/底部/左侧/右侧的光反射到右侧/左侧/底部/顶部。 / 镜子将来自顶部/底部/左侧/右侧的光反射到左侧/右侧/顶部/底部。
  4. 当光线撞击墙壁(墙壁均为镜子)时,会发生 180 度反射。
  5. 光线会被障碍物和石像鬼阻挡。

Hu 可以将任何石像鬼旋转 90 度。由于时间紧迫,他想知道为了打开宝藏门,最少需要旋转多少个石像鬼。

输入格式

第一行包含两个空格分隔的整数 $n$ 和 $m$ ($1 \leq n, m \leq 500$),表示古墓的尺寸。

接下来的 $n$ 行,每行包含一个长度为 $m$ 的字符串 $s$,表示古墓的平面图。

输出格式

输出一个整数,表示为了打开宝藏门最少需要旋转的石像鬼数量。如果谜题无解,则输出 $-1$。

说明

上图展示了样例 1 的初始配置(左)和解法(右)。旋转了三个石像鬼以解开谜题。

样例

输入 1

5 5
/.V.\
./.V.
..#..
.V.#.
\.V./

输出 1

3

输入 2

2 5
V...\
H...V

输出 2

-1

输入 3

2 2
VV
VV

输出 3

0

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.