Hello 2025
G
Secret Message
Codeforces
3000
原题
← FH →
翻译中…

Every Saturday, Alexander B., a teacher of parallel X, writes a secret message to Alexander G., a teacher of parallel B, in the evening. Since Alexander G. is giving a lecture at that time and the message is very important, Alexander B. has to write this message on an interactive online board.

翻译中…

The interactive online board is a grid consisting of $n$ rows and $m$ columns, where each cell is $1 \times 1$ in size. Some cells of this board are already filled in, and it is impossible to write a message in them; such cells are marked with the symbol ".", while the remaining cells are called free and are marked with the symbol "#".

翻译中…

Let us introduce two characteristics of the online board:

翻译中…
  • $s$ is the number of free cells.
  • $p$ is the perimeter of the grid figure formed by the union of free cells.
翻译中…

Let $A$ be the set of free cells. Your goal is to find a set of cells $S \subseteq A$ that satisfies the following properties:

翻译中…
  • $|S| \le \frac{1}{5} \cdot (s+p)$.
  • Any cell from $A$ either lies in $S$ or shares a side with some cell from $S$.
翻译中…

We can show that at least one set $S$ satisfying these properties exists; you are required to find any suitable one.

翻译中…

Input

翻译中…

The first line contains the number $t$ ($1 \le t \le 80\,000$) — the number of test cases.

翻译中…

In the first line of each test case, the numbers $n$ and $m$ ($1 \le n, m \le 2 \cdot 10^6$) — the dimensions of the grid are given.

翻译中…

The following $n$ lines contain the description of the grid.

翻译中…

It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $2 \cdot 10^6$.

翻译中…

Output

翻译中…

For each test case, output $n$ lines consisting of $m$ symbols, where each symbol encodes the state of the cell:

翻译中…
  • "#" — the cell is in $A$ but not in $S$;
  • "S" — the cell is in both $A$ and $S$;
  • "." — the cell is neither in $A$ nor in $S$.
翻译中…

Note

翻译中…

In the first example, $s=5$ and $p=12$, thus the number of cells in $S$ must not exceed $\frac{1}{5} \cdot (5+12) = 3.4$, meaning $|S| \le 3$. Note that the presented set $S$ consists of $1$ cell and clearly satisfies all the constraints.

翻译中…

In the second example, $s=12$ and $p=16$, thus the number of cells in $S$ must not exceed $\frac{1}{5} \cdot (12+16)= 5.6$, meaning $|S| \le 5$. Note that the presented set $S$ consists of $4$ cells and clearly satisfies all the constraints.

翻译中…

In the third example, we will explain what perimeter is, as it may not be obvious. Any grid figure has a boundary, which can be represented as a union of segments that do not intersect at interior points. Thus, in the picture below, the thick black line denotes the boundary of the figure formed by the union of free cells. The total length of these segments is $p=24$.

翻译中…

At the same time, the value $s=11$ and the upper limit is $|S| \le 7$, the presented set has a size of $6$ and clearly satisfies all the constraints.

翻译中…

image

翻译中…

样例

Input #1
3
3 3
.#.
###
.#.
2 6
######
######
3 7
###....
#.#.###
###....
Output #1
.#.
#S#
.#.
#S##S#
#S##S#
S#S....
#.#.S#S
S#S....
InputCopy OutputCopy
题目信息
题号
2057G
所属比赛
时间限制
2 s
内存限制
256 MB
标签
构造
DFS 及类似
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。