RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
문제 COCI00700

Kraljica

설명

Queen Sofia is trapped on a mysterious chessboard of \(n\) rows and \(m\) columns. The
board contains her starting square (labeled "S") and her exit square (labeled "E").
Unfortunately, the path to the exit is not easy - some squares are blocked (labeled
"#"), while others are free (labeled ".").
But the board also hides magical portals! Some of the numbers between 1 and 9
appear exactly twice on the board. If the queen lands on a square with a number,
she can teleport to another square on the board with the same number without
spending extra moves to move through the portal. The queen cannot continue moving in the same
direction after passing through the portal without an additional cost of 1 move.
The queen can move in a chess-like manner - in one move she can jump to any square that is on the same
diagonal, row, or column as her, but only if there are no blocked squares between her and that
square (including the starting and ending squares of the move).
Find and print the smallest number of moves required for the queen to reach the exit square from the
starting square, or print -1 if this is not possible.

제약
입력 형식

The first line contains the integers \(n\) and \(m\) (\(1 \le n\), \(m \le 1000\)), the number of rows and the number of
columns of the board.
Each of the next \(n\) lines contains \(m\) characters \(c_{ij}\), the description of the board. Each character of the
board will be one of S, E, a number between 1 and 9, "." or "#" (without quotes).

출력 형식

In the first and only line, print a single number - the smallest number of moves required for the queen to
reach the exit square.

서브태스크
서브태스크점수설명

1

24점

n, \(m \le 5\)

2

32점

There won’t be any portals on the board.

3

18점

n, \(m \le 500\)

4

36점

No additional constraints.

예제 1
입력
4 4
S..1
####
1.#E
....
출력
4
예제 2
입력
3 3
S..
.#.
..E
출력
2
예제 3
입력
5 4
S.21
####
2##1
###.
E..#
출력
4
설명

Clarification of the third example: Queen Sofia will move to number 1 in the 1st row in her first
move (note that there is no blocked square between S and 1, so this move is indeed possible). She will
then teleport to the second number 1 in the matrix and will make 3 more moves to the exit square. With
these moves, she has indeed made the fewest number of moves needed from the starting to the exit square.

문제 정보

생성자가 기록되지 않았습니다.

출처 COCI 2025/2026 Contest 1

평가 및 의견

Kraljica

개요
출제자 난이도 Unrated 레이팅 미적용 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Kraljica

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8