아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Tilting Tiles

시간 제한3초메모리 제한1024 MB

요약
네 방향으로 판을 기울여 색 타일을 밀 때, 시작 배치에서 목표 배치에 도달할 수 있는지 판정한다.
난이도

보통10점 중 6점

유형
시뮬레이션, BFS, 해시맵
정답자
아직 제출이 없습니다

문제

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

Figure 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 2 illustrates tilting of the puzzle shown in Sample Input 1.

Figure 2: Solution to Sample Input 1.

입력

The first line of input contains two integers hh and ww (1≤h,w≤5001≤h,w≤500) representing the height and width of the grid. Then follow hh lines giving the starting arrangement from the top row to the bottom row. Each of these lines contains a string of length ww 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 hh lines in the same format as for the starting arrangement.

출력

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

예제3

  1. 예제 1

    입력
    4 4
    .r..
    rgyb
    .b..
    .yr.
    
    yrbr
    ..yr
    ...g
    ...b
    
    예상 출력
    yes
    
  2. 예제 2

    입력
    1 7
    ....x..
    
    ..x....
    
    예상 출력
    no
    
  3. 예제 3

    입력
    4 3
    yr.
    ..b
    ry.
    b..
    
    ...
    ..b
    .ry
    byb
    
    예상 출력
    no