#P9351. [JOI 2023 Final] Maze

[JOI 2023 Final] Maze

题目描述

President K loves solving mazes. He found cells from which he may create a maze. The cells are rectangular grid with RR horizontal rows and CC vertical columns. Each cell is colored either white or black. The cell in the ii-th row (1iR1 \leqslant i \leqslant R) from the top and the jj-th column (1jC1 \leqslant j \leqslant C) from the left is called Cell (i,j)(i, j).

President K will solve the maze under the condition that he can pass the white cells, but he cannot pass the black cells. More precisely, he will solve the maze in the following way.

  1. Among the white cells, he will choose Cell (Sr,Sc)(S_r, S_c), which is the starting cell of the maze, and Cell (Gr,Gc)(G_r, G_c), which is the goal cell of the maze.
  2. It is possible to move from one cell to another white cell which is adjacent to the current cell in one of the four directions (top, bottom, left, or right). By repeating this, he will find a path from the starting cell to the goal cell.

President K already fixed the starting cell and the goal cell. However, he noticed that in some situations of the colors of the cells, there might not be a path from the starting cell to the goal cell consisting of white cells only. He has a stamp of size N×NN \times N. He will perform the following Operations several times so that there will be a path from the starting cell to the goal cell consisting of white cells only.

Operation. He chooses a square region of N×NN \times N cells, and paint the cells in the region white. More precisely, he chooses integers a,ba, b satisfying 1aRN+11 \leqslant a \leqslant R - N + 1 and 1bCN+11 \leqslant b \leqslant C − N + 1, and for every pair (i,j)(i, j) of integers satisfying aia+N1a \leqslant i \leqslant a + N − 1 and bjb+N1b \leqslant j \leqslant b + N − 1, he paints Cell (i,j)(i, j) white.

Since his hands becomes dirty if he uses the stamp, he wants to minimize the number of operations. Given information of the colors of the cells, the size of the stamp, and the locations of the starting cell and the goal cell, write a program which calculates the minimum number of operations he has to perform so that there will be a path from the starting cell to the goal cell consisting of white cells only.

输入格式

Read the following data from the standard input.

RR CC NN
SrS_r ScS_c
GrG_r GcG_c
A1A_1
A2A_2
..
..
..
ARA_R

Ai(1iR)A_i (1 \leqslant i \leqslant R) is a string of length CC consisting of . or #. The jj-th character (1jC1 \leqslant j \leqslant C) of AiA_i represents the color of Cell (i,j)(i, j). Its color is white if the character is ., and its color is black if the character is #.

输出格式

Write one line to the standard output. The output should contain the minimum number of operations President K has to perform so that there will be a path from the starting cell to the goal cell consisting of white cells only.

题目大意

给定一张 R×CR\times C 的地图,其中 . 可以走,而 # 不能走。一次操作可以将 N×NN \times N 的正方形范围内所有点变成 .,给定起点和终点,求最少需要几次操作使得起点和终点连通(只能上下左右移动)。

R×C6×106R\times C\le 6\times 10^6NRCN\le R\le C

2 4 2
1 1
2 4
.###
###.

1

6 6 1
1 6
6 1
..#.#.
##.###
####.#
...###
##.##.
.#.###

4

6 7 6
6 4
3 1
..#.#.#
##.##..
.######
#..#.#.
.######
..#.##.

1

1 15 1
1 15
1 1
...............

0

提示

【样例解释 #1】

If he chooses (a,b)=(1,2)(a, b) = (1, 2) and perform an operation, Cells (1,2),(1,3),(2,2),(2,3)(1, 2), (1, 3), (2, 2), (2, 3) become white. Then there will be a path from the starting cell to the goal cell consisting of white cells only. For example, the path (1,1)(1,2)(1,3)(2,3)(2,4)(1, 1) → (1, 2) → (1, 3) → (2, 3) → (2, 4) satisfies the condition.

If he does not perform an operation, there is no path from the starting cell to the goal cell consisting of white cells only. Therefore, output 11.

该样例满足子任务 2,3,4,5,6,7,82, 3, 4, 5, 6, 7, 8 的限制。

【样例解释 #2】

该样例满足所有子任务的限制。

【样例解释 #3】

该样例满足子任务 2,3,4,5,6,7,82, 3, 4, 5, 6, 7, 8 的限制。

【样例解释 #4】

Even if he does not perform an operation, there might be a path from the starting cell to the goal cell consisting of white cells only.

该样例满足所有子任务的限制。

【数据范围】

对于所有测试数据,满足 1NRC1 ≤ N ≤ R ≤ C, R×C6×106R × C ≤ 6 \times 10^6, 1SrR1 ≤ S_r ≤ R, 1ScC1 ≤ S_c ≤ C, 1GrR1 ≤ G_r ≤ R, 1GcC1 ≤ G_c ≤ C, (Sr,Sc)(Gr,Gc)(S_r, S_c) \neq (G_r, G_c).

保证 Ai(1iR)A_i (1 ≤ i ≤ R) 是一个长为 CC 且只由 .# 构成的字符串。保证格子 (Sr,Sc)(S_r, S_c) 与格子 (Gr,Gc)(G_r, G_c) 均为白色。

保证 R,C,N,Sr,Sc,Gr,GcR, C, N, S_r, S_c, G_r, G_c 均为整数。

子任务编号 分值 限制
11 88 N=1,R×C1.5×106N = 1, R × C ≤ 1.5 \times 10^6
22 1919 R×C103R × C ≤ 10^3
33 1616 答案不超过 1010, R×C1.5×106R × C ≤ 1.5 \times 10^6
44 1919 R×C6×104R × C ≤ 6 \times 10^4
55 R×C1.5×105R × C ≤ 1.5 \times 10^5
66 1919 R×C1.5×106R × C ≤ 1.5 \times 10^6
77 88 R×C3×106R × C ≤ 3 \times 10^6
88 66