Break a Prison

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

요약
이전 이동 방향에서 오른쪽으로 꺾을 수 없다는 조건 아래 격자에서 S에서 E까지의 최단 이동 횟수를 구한다.
난이도

보통10점 중 7점

유형
BFS, 그래프, 구현, 최단 경로
정답자
아직 제출이 없습니다

문제

Jennifer is a software engineer at a Tech company. Her company decided to join ICPC (Inter-Company Prison breaking Contest) and she was chosen as a representative of the company.

In ICPC, every participant needs to escape from a prison. The prison can be represented as an n×mn \times m grid i.e. it has nn rows and mm columns of rooms. The room in the ii-th row and jj-th column in the prison is denoted as room (i,j)(i, j). Two rooms (i_1,j_1)(i\_1, j\_1) and (i_2,j_2)(i\_2, j\_2) are adjacent if and only if ∣i_2−i_1∣+∣j_2−j_1∣=1|i\_2 - i\_1| + |j\_2 - j\_1| = 1. Weirdly, there is an unlocked door between each pair of adjacent rooms. Some rooms in the prison are under surveillance. Participants can move to a room only if it's not under surveillance. A participant will start from a room. The goal of all participants is to reach an exit. It's guaranteed that the room with the exit and the room that participants start from are not under surveillance.

To show talents in the company, the CEO asked Jeniffer not to turn right during the contest. In other words, there should not be any two consecutive moves between rooms that fulfill the following condition.

Condition: Given that Jeniffer moved from room (i_1,j_1)(i\_1, j\_1) to (i_2,j_2)(i\_2, j\_2), and then she moved to room (i_3,j_3)(i\_3, j\_3). Then, (i_2−i_1)×(j_3−j_2)−(j_2−j_1)×(i_3−i_2)=−1(i\_2 - i\_1) \times (j\_3 - j\_2) - (j\_2 - j\_1) \times (i\_3 - i\_2) = -1 holds.

Figure B.1. Example of allowed and denied moves

For example, in figure B.1., if the last move is along the dashed arrow, you cannot move downward but you can move the other three directions.

Note that U-turns are allowed with this condition.

As a Jeniffer's colleague, your mission is to write a program to find the minimum number of moves between rooms to reach the exit for her.

입력

The input consists of a single test case in the following format.

nn mm

c_1,1c_1,2…c_1,mc\_{1,1}c\_{1,2}\dots c\_{1,m}

c_2,1c_2,2…c_2,mc\_{2,1}c\_{2,2}\dots c\_{2,m}

⋮\vdots

c_n,1c_n,2…c_n,mc\_{n,1}c\_{n,2}\dots c\_{n,m}

nn and mm represent the size of the prison, each of which is an integer between 22 and 500500. c_i,jc\_{i,j} (1≤i≤n1 \le i \le n, 1≤j≤m1 \le j \le m) is a character that describes the status of a room in the ii-th row and jj-th column. The character is either

  • ‘S' which means a start room for a participant,
  • ‘E' which means a room with an exit,
  • ‘.' which means the room is not under surveillance, or
  • ‘#' which means the room is under surveillance.

It is guaranteed that ‘S' and ‘E' appear exactly once in the input respectively.

출력

Print the minimum number of moves between rooms for Jenniffer to reach the exit. If she cannot reach the exit, print −1-1.

힌트

In Sample Input 3, one of the optimal routes is below.

Figure B.2. The optimal route in Sample Input 3

예제3

  1. 예제 1

    입력
    2 4
    S..#
    ..E.
    
    예상 출력
    3
    
  2. 예제 2

    입력
    2 4
    S..#
    ##E.
    
    예상 출력
    -1
    
  3. 예제 3

    입력
    2 4
    S...
    ##E.
    
    예상 출력
    5