codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1375B.md (2780B)


      1 Source: https://codeforces.com/problemset/problem/1375/B
      2 
      3 B. Neighbor Grid
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given a grid with $n$ rows and $m$ columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number $k \gt 0$ written on it, then exactly $k$ of its neighboring cells have a number greater than $0$ written on them. Note that if the number in the cell is $0$, there is no such restriction on neighboring cells.
     22 
     23 You are allowed to take any number in the grid and increase it by $1$. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.
     24 
     25 Two cells are considered to be neighboring if they have a common edge.
     26 
     27 Input
     28 
     29 The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 5000$)  — the number of test cases. The description of the test cases follows.
     30 
     31 The first line of each test case contains two integers $n$ and $m$ ($2 \le n, m \le 300$)  — the number of rows and columns, respectively.
     32 
     33 The following $n$ lines contain $m$ integers each, the $j$-th element in the $i$-th line $a_{i, j}$ is the number written in the $j$-th cell of the $i$-th row ($0 \le a_{i, j} \le 10^9$).
     34 
     35 It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$.
     36 
     37 Output
     38 
     39 If it is impossible to obtain a good grid, print a single line containing "NO".
     40 
     41 Otherwise, print a single line containing "YES", followed by $n$ lines each containing $m$ integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero).
     42 
     43 If there are multiple possible answers, you may print any of them.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 5
     51 3 4
     52 0 0 0 0
     53 0 1 0 0
     54 0 0 0 0
     55 2 2
     56 3 0
     57 0 0
     58 2 2
     59 0 0
     60 0 0
     61 2 3
     62 0 0 0
     63 0 4 0
     64 4 4
     65 0 0 0 0
     66 0 2 0 1
     67 0 0 0 0
     68 0 0 0 0
     69 ```
     70 
     71 Output
     72 
     73 ```
     74 YES
     75 0 0 0 0
     76 0 1 1 0
     77 0 0 0 0
     78 NO
     79 YES
     80 0 0
     81 0 0
     82 NO
     83 YES
     84 0 1 0 0
     85 1 4 2 1
     86 0 2 0 0
     87 1 3 1 0
     88 ```
     89 
     90 Note
     91 
     92 In the first test case, we can obtain the resulting grid by increasing the number in row $2$, column $3$ once. Both of the cells that contain $1$ have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid
     93 
     94  $$0\;1\;0\;0$$ $$0\;2\;1\;0$$ $$0\;0\;0\;0$$ 
     95 
     96 All of them are accepted as valid answers.
     97 
     98 In the second test case, it is impossible to make the grid good.
     99 
    100 In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.