Logo
(追記) (追記ここまで)

30367번 - Break a Prison 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB52403777.083%

문제

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 \times m$ grid i.e. it has $n$ rows and $m$ columns of rooms. The room in the $i$-th row and $j$-th column in the prison is denoted as room $(i, j)$. Two rooms $(i_1, j_1)$ and $(i_2, j_2)$ are adjacent if and only if $|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)$ to $(i_2, j_2),ドル and then she moved to room $(i_3, j_3)$. Then, $(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.

$n$ $m$

$c_{1,1}c_{1,2}\dots c_{1,m}$

$c_{2,1}c_{2,2}\dots c_{2,m}$

$\vdots$

$c_{n,1}c_{n,2}\dots c_{n,m}$

$n$ and $m$ represent the size of the prison, each of which is an integer between 2ドル$ and 500ドル$. $c_{i,j}$ (1ドル \le i \le n,ドル 1ドル \le j \le m$) is a character that describes the status of a room in the $i$-th row and $j$-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

2 4
S..#
..E.

예제 출력 1

3

예제 입력 2

2 4
S..#
##E.

예제 출력 2

-1

예제 입력 3

2 4
S...
##E.

예제 출력 3

5

힌트

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

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

출처

Contest > ICPC Japanese Alumni Group > JAG Summer Camp > JAG Summer Camp 2023 Day 3 B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /