問題一覧 > 通常問題

No.402 The Place Farthest from the Sea

レベル : / AI推定難易度 : 1100 / 実行時間制限 : 1ケース 3.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 172
作問者 : AreTrash / テスター : Tawara
お気に入りにしたユーザー ProblemId : 1152 / 自分の提出
問題文最終更新日: 2026-06-25 04:44:07
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 143 (順位表) の他の問題:

NOTE

It has been confirmed that the author's solution passes all test cases in C++, C#, Java, and PyPy, but it may be difficult to pass in other, slower languages.

Problem Statement

There is a map of height $H$ and width $W$. The map gives information on whether each location is land or sea. All areas outside the map are sea.
Find the Chebyshev distance from the sea of the place that is farthest from the sea in terms of this distance.

(Addendum)
1. The Chebyshev distance between two points $(x_1, y_1), (x_2, y_2)$ is expressed as $max(|x_1-x_2|, |y_1-y_2|)$.
2. The distance between a land cell and the sea is the minimum value among the Chebyshev distances between that land cell and all sea cells.

Input

$H$ $W$
$S_1$
$S_2$
$\vdots$
$S_H$

The first line contains $H$ and $W$, the size of the map, separated by a space.
Each of the following $H$ lines contains a string of $W$ characters representing the map.
"#" represents land and "." represents sea. There is always at least one land cell.

$H$ and $W$ are integers, $1 \le H,W \le 3000$
$S_i$ is a string of $W$ characters consisting of "#" and "."

Output

Output the Chebyshev distance from the sea of the place that is farthest from the sea in terms of this distance.

Sample

Sample 1
Input
8 8
.#..#...
.######.
#####...
#####.#.
########
######.#
#####...
...####.
Output
3

With the green parts as land and the light blue parts as sea, the Chebyshev distance from the sea of each land cell is as shown in the figure below.
The value at the place with the largest Chebyshev distance is 3, so output it.

Sample 2
Input
7 7
#######
#######
#######
#######
#.#####
#..####
#######
Output
3

There may be sea inside an island.
Also, note that everything outside the range of the map is sea.

Sample 3
Input
60 60
............................................................
............................................................
........................................#...............##..
.......................................####............##...
.........................................##...........##....
.........................................###........#.......
.........................................#####...#..#.......
.........................................#########....#.....
........................................###########.........
........................................#############.......
.....................................##.##########..........
......................................#########.............
.....................................#########..............
....................................##.#...###..............
...................................###.......#..............
.....................................###....................
.....................................#......................
.......................................##...................
......................................#.#...................
.....................................####...................
.....................................####...................
.....................................#####..................
.....................................######.................
.....................................######.................
.....................................######.................
.....................................#####..................
....................................#####...................
....................................#####...................
....................................######..................
...................................#####....................
................................#.######....................
.................................#######....................
............................#...########....................
............................#.##########....................
...........................############.....................
...........................############.....................
..........................#############.....................
.........................###############....................
....................###..##############.....................
...............#####################.##.....................
..............###################.#..#......................
.....#......###############.####..#.........................
..........########......###.................................
..........####....##.#.#####................................
......#.##...#.######..###..................................
.....##.####...######..##...................................
......######..####.#........................................
...#...######.###...........................................
......##.####..#............................................
.......#.###................................................
........####................................................
.......####.................................................
.......####.................................................
.......#.##.................................................
............................................................
..........#.................................................
........#.#.................................................
............................................................
............................................................
............................................................
Output
4

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。