QOJ.ac

QOJ

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

#8679. Tilting Tiles

Statistics

You found a weird puzzle in a box with old toys in your attic. The puzzle forms a rectangular grid board made of $h \times w$ square cells. Some cells in that grid have a colored tile placed on them, as shown in Figure F.1.

problem_8679_141bfaa0ba0c1196198d6deee1100655.png

Figure F.1: Color tiles correspond to the starting arrangement in Sample Input 1.

You are not yet sure what the exact goal of this puzzle is, but you started examining possible ways of rearranging the tiles. Their arrangement can be manipulated by tilting the grid in one of the four cardinal directions: to your left, to your right, towards you, or away from you. Tilting causes all the tiles to slide in the respective direction until they are blocked either by the boundary or by another tile. Given a starting and ending arrangement, determine whether there exists some sequence of tilts that transforms the former into the latter. Figure F.2 illustrates tilting of the puzzle shown in Sample Input 1.

problem_8679_eb20e1509cd290b03249b30fb584f1df.png

Figure F.2: Solution to Sample Input 1.

Input

The first line of input contains two integers $h$ and $w$ ($1 \le h,w \le 500$) representing the height and width of the grid. Then follow $h$ lines giving the starting arrangement from the top row to the bottom row. Each of these lines contains a string of length $w$ describing cells on the row from left to right. If a cell is empty, the corresponding character is a dot (.). If there is a tile, the color of that tile is given, denoted by a lowercase letter (a-z). Different letters represent different colors, and tiles of the same color cannot be distinguished.

After the starting arrangement, there is one empty line and then follows a description of the ending arrangement, consisting of $h$ lines in the same format as for the starting arrangement.

Output

Output yes if a sequence of tilts exists that transforms the starting arrangement to the ending arrangement, and no otherwise.

Examples

Input 1

4 4
.r..
rgyb
.b..
.yr.

yrbr
..yr
...g
...b

Output 1

yes

Input 2

1 7
....x..

..x....

Output 2

no

Input 3

4 3
yr.
..b
ry.
b..

...
..b
.ry
byb

Output 3

no

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.