C - 隣接カウント Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

注意

この問題に対する言及は、2020/11/8 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

NM 列のマス目が与えられます。それぞれのマスには #. が書かれており、上から i 行目、左から j 列目のマスには s_{i,j} が書かれています。 それぞれのマスについて、そのマス、およびそのマスと上下左右斜めに隣接したマス (最大で合計 9 マス) のうち # が書かれたマスがいくつあるかを数えてください。

制約

  • 1 \leq N,M \leq 30
  • s_i (1 \leq i \leq N) の長さは M
  • s_{i,j}#. のどちらか

入力

入力は以下の形式で標準入力から与えられる。

N M
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

出力

以下のフォーマットで N 行に渡って、長さ M の文字列を出力せよ。 i 行目の先頭から j 文字目は上から i 行目、左から j 列目のマスについて、そのマス、およびそのマスと上下左右斜めに隣接したマスのうち # が書かれたマスの個数を 0 から 9 までの数字で出力せよ。

a_{1,1}\cdots a_{1,M}
\vdots
a_{N,1}\cdots a_{N,M}

入力例 1

3 4
#.##
..#.
#...

出力例 1

1333
2433
1211
  • 例えば上から 1 行目、左から 3 列目のマスは、そのマス、およびそのマスと上下左右斜めに隣接したマスのうち、3 つに # が書かれています。
  • よって、1 行目の先頭から 3 文字目は 3 となります。

入力例 2

10 12
#.##..#...##
#..#..##...#
##.#....##.#
.#..###...#.
#..#..#...##
###...#..###
.###.#######
.#..#....###
.#.##..####.
.###....#..#

出力例 2

233322331133
455432343354
444344443343
444344332454
454335431465
466434554686
466434445796
346554457885
346542135664
235431134432

Score : 8 points

Warning

Do not make any mention of this problem until November 8, 2020, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a grid with N rows and M columns. Each square has # or . written on it. More specifically, the square at the i-th row from the top and j-th column from the left has s_{i,j} written on it. For each square T, count the number of squares with # among the following squares (at most nine in total): T itself, and the squares that are horizontally, vertically, or diagonally adjacent to T.

Constraints

  • 1 \leq N,M \leq 30
  • The length of s_i (1 \leq i \leq N) is M.
  • s_{i,j} is # or ..

Input

Input is given from Standard Input in the following format:

N M
s_{1,1}\cdots s_{1,M}
\vdots
s_{N,1}\cdots s_{N,M}

Output

Print N lines, each containing a string of length M. In the i-th line, the j-th character from the beginning should be a digit between 0 and 9 (inclusive) representing the number of squares with # among the following squares: the square at the i-th row from the top and j-th column from the left, and the squares that are horizontally, vertically, or diagonally adjacent to that square.

a_{1,1}\cdots a_{1,M}
\vdots
a_{N,1}\cdots a_{N,M}

Sample Input 1

3 4
#.##
..#.
#...

Sample Output 1

1333
2433
1211
  • For example, consider the square at the 1-st row from the top and 3-rd column from the left. Among that square and the ones that are horizontally, vertically, or diagonally adjacent to that square, there are 3 squares with #.
  • Thus, the 3-rd character from the beginning in the 1-st line should be 3.

Sample Input 2

10 12
#.##..#...##
#..#..##...#
##.#....##.#
.#..###...#.
#..#..#...##
###...#..###
.###.#######
.#..#....###
.#.##..####.
.###....#..#

Sample Output 2

233322331133
455432343354
444344443343
444344332454
454335431465
466434554686
466434445796
346554457885
346542135664
235431134432