luogu#P16358. [BalticOI 2026] Island
[BalticOI 2026] Island
Problem Description
You are given an grid where each cell is either land or water. The rows and columns of the grid are numbered . You can move in the grid by taking steps left, right, up or down. Two cells are connected if you can move between them in one or more steps, while staying on the same type of cell.
The land cells form one connected island and the water cells form one connected ocean. The first and last rows and columns contain only water cells.
Your task is to answer queries: given two land cells and , what is the minimum number of steps needed to move from the first cell to the second one while staying on land?
Input Format
The first line contains two integers and : the size of the grid and the number of queries.
The next lines each have characters describing the grid. "." is a water cell and "#" is a land cell.
The next lines each have four integers , , and : the row and column of the first cell, followed by the row and column of the second cell.
Output Format
Print the answer to each query on separate lines.
8 4
........
..####..
.##.###.
.##.###.
.#......
.#####..
..#####.
........
2 3 3 7
4 5 4 5
4 7 7 7
6 2 3 2
5
0
17
3
Hint
Constraints
- in all queries
Scoring
| Subtask | Constraints | Points |
|---|---|---|
| 1 | , | |
| 2 | No row or column has any water cells between land cells | |
| 3 | No -squares have only land cells | |
| 4 | No row has any water cells between land cells | |
| 5 | No additional constraints |