#P15065. [UOI 2024 II Stage] Mole

    ID: 16994 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>2024Special Judge广度优先搜索 BFSUOI(乌克兰)

[UOI 2024 II Stage] Mole

题目描述

After a difficult research, Anton decided to relax at his country house. He has a beautiful garden with many different flowers. But, oh no, upon arrival he saw a significant number of holes in the ground. It's a mole!

Now, armed with a shovel, Anton will be waiting for the mole. The mole can emerge from any hole. Anton wants to choose a position so that in the worst case, he runs to the mole in the minimum\textbf{minimum} amount of time.

The garden can be represented as a matrix n×mn \times m, where nn is the number of rows, and mm is the number of columns. Rows are numbered from top to bottom, from 11 to nn. Columns are numbered from left to right, from 11 to mm. Thus, the cell with index (1;1)(1;1) is located in the upper left corner.

Each cell of the garden ai,ja_{i, j} describes the state of this cell:

  • ai,ja_{i, j} = .\tt{.} --- this cell does not contain flowers or holes;
  • ai,ja_{i, j} = F\tt{F} --- this cell contains flowers;
  • ai,ja_{i, j} = H\tt{H} --- this cell contains a hole.

Anton also knows that the number of holes does not exceed 100100.

As a person who has put a lot of time into these flowers, your heart cannot bear trampling on them. Therefore, you need to find a path in such a way that it does not pass through them.

At any moment in time, Anton can move from position (x,y)(x, y) to any of the following positions: (x1,y)(x-1,y), (x+1,y)(x+1,y), (x,y1)(x,y-1), (x,y+1)(x,y+1), provided that the new position does not contain flowers and is inside the garden.

Find all positions (x;yx;y) from which Anton will run to the moles in the worst case in the minimum amount of time.

输入格式

The first line contains two integers nn, mm (1nm21051 \le n \cdot m \le 2 \cdot 10^5) --- the length and width of the garden.

The next nn lines contain mm characters each --- the description of the garden.

It is guaranteed that from each cell that does not contain flowers, it is possible to reach any other cell that does not contain flowers by moving through cells that do not contain flowers.

It is guaranteed that there is at least one hole, and the number of holes in the garden does not exceed 100100.

输出格式

In the first line, print a single integer xx (1xnm1 \leq x \leq n \cdot m) --- the number of optimal positions.

In each of the next xx lines, print the optimal positions (x;yx;y) for waiting for the mole (1xn1 \le x \le n, 1ym1 \le y \le m).

Positions can be printed in any order.

3 4
HF.F
..HF
FF.F
2
2 1
2 2
4 9
......FFH
.F..FHFF.
HF.......
.FHF..FFF
2
1 6
3 4

提示

:::align{center} :::

Let kk be the number of holes in the garden.

  • (66 points): n=1,m=2n = 1, m = 2;
  • (99 points): n=1n = 1;
  • (1515 points): k=1,nm5103k = 1, n \cdot m \le 5 \cdot 10^3;
  • (2222 points): nm5103n \cdot m \le 5 \cdot 10^3;
  • (1717 points): k=1k = 1;
  • (3131 points): without additional restrictions.