#abc300b. B - Same Map in the RPG World

B - Same Map in the RPG World

Score : 200200 points

问题描述

高桥正在开发一款RPG游戏。他决定编写一段代码来检查两个地图是否相等。

我们有网格 AABB,它们都有 HH 行和 WW 列。每个格子上都写有一个符号#.

在网格 AABB 中,从上到下第 ii 行、从左到右第 jj 列的格子上的符号分别表示为 Ai,jA_{i, j}Bi,jB_{i, j}

以下两种操作被称为垂直平移水平平移

  • 对于所有 j=1,2,,Wj=1, 2, \dots, W 同时执行以下操作:
    • 同时将 A1,j,A2,j,,AH1,j,AH,jA_{1,j}, A_{2,j}, \dots, A_{H-1, j}, A_{H,j} 替换为 A2,j,A3,j,,AH,j,A1,jA_{2,j}, A_{3,j}, \dots, A_{H,j}, A_{1,j}
  • 对于所有 i=1,2,,Hi = 1, 2, \dots, H 同时执行以下操作:
    • 同时将 Ai,1,Ai,2,,Ai,W1,Ai,WA_{i,1}, A_{i,2}, \dots, A_{i,W-1}, A_{i,W} 替换为 Ai,2,Ai,3,,Ai,W,Ai,1A_{i, 2}, A_{i, 3}, \dots, A_{i,W}, A_{i,1}

是否存在一对非负整数 (s,t)(s, t) 满足以下条件?如果存在则输出 Yes,否则输出 No

  • 应用 ss 次垂直平移和 tt 次水平平移后,AA 等于 BB

这里,若对于所有满足 1iH1 \leq i \leq H1jW1 \leq j \leq W 的整数对 (i,j)(i, j),均有 Ai,j=Bi,jA_{i, j} = B_{i, j},则称 AA 等于 BB

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

Takahashi is developing an RPG. He has decided to write a code that checks whether two maps are equal.

We have grids AA and BB with HH horizontal rows and WW vertical columns. Each cell in the grid has a symbol # or . written on it.
The symbols written on the cell at the ii-th row from the top and jj-th column from the left in AA and BB are denoted by Ai,jA_{i, j} and Bi,jB_{i, j}, respectively.

The following two operations are called a vertical shift and horizontal shift.

  • For each j=1,2,,Wj=1, 2, \dots, W, simultaneously do the following:
    • simultaneously replace A1,j,A2,j,,AH1,j,AH,jA_{1,j}, A_{2,j}, \dots, A_{H-1, j}, A_{H,j} with A2,j,A3,j,,AH,j,A1,jA_{2,j}, A_{3,j}, \dots, A_{H,j}, A_{1,j}.
  • For each i=1,2,,Hi = 1, 2, \dots, H, simultaneously do the following:
    • simultaneously replace Ai,1,Ai,2,,Ai,W1,Ai,WA_{i,1}, A_{i,2}, \dots, A_{i,W-1}, A_{i,W} with Ai,2,Ai,3,,Ai,W,Ai,1A_{i, 2}, A_{i, 3}, \dots, A_{i,W}, A_{i,1}.

Is there a pair of non-negative integers (s,t)(s, t) that satisfies the following condition? Print Yes if there is, and No otherwise.

  • After applying a vertical shift ss times and a horizontal shift tt times, AA is equal to BB.

Here, AA is said to be equal to BB if and only if Ai,j=Bi,jA_{i, j} = B_{i, j} for all integer pairs (i,j)(i, j) such that 1iH1 \leq i \leq H and 1jW1 \leq j \leq W.

Constraints

  • 2H,W302 \leq H, W \leq 30
  • Ai,jA_{i,j} is # or ., and so is Bi,jB_{i,j}.
  • HH and WW are integers.

Input

The input is given from Standard Input in the following format:

HH WW

A1,1A1,2A1,WA_{1,1}A_{1,2}\dots A_{1,W}

A2,1A2,2A2,WA_{2,1}A_{2,2}\dots A_{2,W}

\vdots

AH,1AH,2AH,WA_{H,1}A_{H,2}\dots A_{H,W}

B1,1B1,2B1,WB_{1,1}B_{1,2}\dots B_{1,W}

B2,1B2,2B2,WB_{2,1}B_{2,2}\dots B_{2,W}

\vdots

BH,1BH,2BH,WB_{H,1}B_{H,2}\dots B_{H,W}

Output

Print Yes if there is a conforming integer pair (s,t)(s, t); print No otherwise.

Sample Input 1

4 3
..#
...
.#.
...
#..
...
.#.
...

Sample Output 1

Yes

By choosing (s,t)=(2,1)(s, t) = (2, 1), the resulting AA is equal to BB.
We describe the procedure when (s,t)=(2,1)(s, t) = (2, 1) is chosen. Initially, AA is as follows.

..#
...
.#.
...

We first apply a vertical shift to make AA as follows.

...
.#.
...
..#

Then we apply another vertical shift to make AA as follows.

.#.
...
..#
...

Finally, we apply a horizontal shift to make AA as follows, which equals BB.

#..
...
.#.
...

Sample Input 2

3 2
##
##
#.
..
#.
#.

Sample Output 2

No

No choice of (s,t)(s, t) makes AA equal BB.

Sample Input 3

4 5
#####
.#...
.##..
..##.
...##
#...#
#####
...#.

Sample Output 3

Yes

Sample Input 4

10 30
..........##########..........
..........####....###.....##..
.....##....##......##...#####.
....####...##..#####...##...##
...##..##..##......##..##....#
#.##....##....##...##..##.....
..##....##.##..#####...##...##
..###..###..............##.##.
.#..####..#..............###..
#..........##.................
................#..........##.
######....................####
....###.....##............####
.....##...#####......##....##.
.#####...##...##....####...##.
.....##..##....#...##..##..##.
##...##..##.....#.##....##....
.#####...##...##..##....##.##.
..........##.##...###..###....
...........###...#..####..#...

Sample Output 4

Yes

update @ 2024/3/10 08:23:16