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

32917번 - Brick in the Wall, Part 2 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 2048 MB84350.000%

문제

Barrett has discovered an ancient maze under his house. It has the shape of an $n \times m$ grid, where some cells are empty, while others are blocked. It is possible to walk from one empty cell to another if they share a side. Two of the empty cells are an entrance and an exit, and it is possible to reach one from the other by walking through empty cells.

Barrett wants to isolate his house by building a wall inside the maze, blocking some cells to make the exit unreachable from the entrance. The wall should be straight and oriented either vertically or horizontally. Specifically, a wall of length $k$ will block a consecutive row or column of exactly $k$ cells. The wall may not contain the entrance, the exit, or any already blocked cells.

Help Barrett determine the minimum possible length of the wall.

입력

Each test contains multiple test cases. The first line contains the number of test cases $t$ (1ドル \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains two integers $n$ and $m,ドル denoting the height and the width of the maze (2ドル \le n, m \le 1000$).

The $i$-th of the following $n$ lines contains $m$ characters and describes the $i$-th row of the maze, where:

  • '.' denotes an empty cell;
  • '#' denotes a blocked cell;
  • 's' denotes an entrance cell;
  • 'f' denotes an exit cell.

The maze contains exactly one entrance cell and exactly one exit cell, and it is possible to reach one from the other by walking through empty cells.

It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed 10ドル^6$.

출력

For each test case, print the minimum length of the wall required to make the exit unreachable from the entrance.

If it is impossible to build such a wall, print $-1$ instead.

제한

예제 입력 1

3
3 3
s.#
...
#.f
6 7
..#.#..
s..#..#
....#f.
#..#...
#......
#.....#
2 2
s.
.f

예제 출력 1

1
2
-1

힌트

출처

ICPC > Regionals > Northern Eurasia > Northwestern Russia Regional Contest > ICPC 2024-2025 Northwestern Russia Regional Contest B번

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

출처

대학교 대회

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

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