CF1920F1.Smooth Sailing (Easy Version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The only difference between the two versions of this problem is the constraint on qq . You can make hacks only if both versions of the problem are solved.

Thomas is sailing around an island surrounded by the ocean. The ocean and island can be represented by a grid with nn rows and mm columns. The rows are numbered from 11 to nn from top to bottom, and the columns are numbered from 11 to mm from left to right. The position of a cell at row rr and column cc can be represented as (r,c)(r, c) . Below is an example of a valid grid.

Example of a valid gridThere are three types of cells: island, ocean and underwater volcano. Cells representing the island are marked with a '#', cells representing the ocean are marked with a '.', and cells representing an underwater volcano are marked with a 'v'. It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is also guaranteed that the set of all island cells forms a single connected component ^{\dagger} and the set of all ocean cells and underwater volcano cells forms a single connected component. Additionally, it is guaranteed that there are no island cells at the edge of the grid (that is, at row 11 , at row nn , at column 11 , and at column mm ).

Define a round trip starting from cell (x,y)(x, y) as a path Thomas takes which satisfies the following conditions:

  • The path starts and ends at (x,y)(x, y) .
  • If Thomas is at cell (i,j)(i, j) , he can go to cells (i+1,j)(i+1, j) , (i1,j)(i-1, j) , (i,j1)(i, j-1) , and (i,j+1)(i, j+1) as long as the destination cell is an ocean cell or an underwater volcano cell and is still inside the grid. Note that it is allowed for Thomas to visit the same cell multiple times in the same round trip.
  • The path must go around the island and fully encircle it. Some path pp fully encircles the island if it is impossible to go from an island cell to a cell on the grid border by only traveling to adjacent on a side or diagonal cells without visiting a cell on path pp . In the image below, the path starting from (2,2)(2, 2) , going to (1,3)(1, 3) , and going back to (2,2)(2, 2) the other way does not fully encircle the island and is not considered a round trip.

Example of a path that does not fully encircle the islandThe safety of a round trip is the minimum Manhattan distance ^{\ddagger} from a cell on the round trip to an underwater volcano (note that the presence of island cells does not impact this distance).

You have qq queries. A query can be represented as (x,y)(x, y) and for every query, you want to find the maximum safety of a round trip starting from (x,y)(x, y) . It is guaranteed that (x,y)(x, y) is an ocean cell or an underwater volcano cell.

^{\dagger} A set of cells forms a single connected component if from any cell of this set it is possible to reach any other cell of this set by moving only through the cells of this set, each time going to a cell with a common side.

^{\ddagger} Manhattan distance between cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is equal to r1r2+c1c2|r_1 - r_2| + |c_1 - c_2| .

输入格式

The first line contains three integers nn , mm , and qq ( 3n,m1053 \leq n, m \leq 10^5 , 9nm31059 \leq n \cdot m \leq 3 \cdot 10^5 , 1q51 \leq q \leq 5 ) — the number of rows and columns of the grid and the number of queries.

Each of the following nn lines contains mm characters describing the cells of the grid. The character '#' denotes an island cell, '.' denotes an ocean cell, and 'v' denotes an underwater volcano cell.

It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is guaranteed that the set of all island cells forms a single connected component and the set of all ocean cells and underwater volcano cells forms a single connected component. Also, it is guaranteed that there are no island cells at the edge of the grid (that is, at the row 11 , at the row nn , at the column 11 , and at the column mm ).

The following qq lines describe the queries. Each of these lines contains two integers xx and yy ( 1xn1 \leq x \leq n , 1ym1 \leq y \leq m ) denoting a round trip starting from (x,y)(x, y) .

It is guaranteed that (x,y)(x, y) is an ocean cell or an underwater volcano cell.

输出格式

For each query, output a single integer — the maximum safety of a round trip starting from the specified position.

输入输出样例

  • 输入#1

    9 9 3
    .........
    .........
    ....###..
    ...v#....
    ..###....
    ...##...v
    ...##....
    .........
    v........
    1 1
    9 1
    5 7

    输出#1

    3
    0
    3
  • 输入#2

    3 3 5
    ..v
    .#.
    ...
    1 2
    1 3
    2 3
    2 1
    3 2

    输出#2

    0
    0
    0
    0
    0
  • 输入#3

    14 13 5
    .............
    .............
    .............
    ...vvvvvvv...
    ...v.....v...
    ...v.###.v...
    ...v.#.#.v...
    ...v..v..v...
    ...v..v..v...
    ....v...v....
    .....vvv.....
    .............
    .............
    .............
    1 1
    7 7
    5 6
    4 10
    13 6

    输出#3

    3
    0
    1
    0
    2
  • 输入#4

    10 11 4
    ...........
    ..#######..
    ..#..#..#..
    ..#.....#..
    ..#..v..#..
    ..#.###.#..
    ..#.#.#.#..
    ..#...#.#..
    ..#####.#..
    ...........
    7 6
    3 7
    6 8
    1 1

    输出#4

    1
    2
    3
    4

说明/提示

For the first example, the image below shows an optimal round trip starting from (1,1)(1, 1) . The round trip has a safety of 33 as the minimum Manhattan distance from a cell on the round trip to an underwater volcano is 33 .

Example of an optimal round tripFor the fourth example, remember that it is allowed for Thomas to visit the same cell multiple times in the same round trip. For example, doing so is necessary for the round trip starting from (7,6)(7, 6) .

首页