984B.md (1975B)
1 Source: https://codeforces.com/problemset/problem/984/B 2 3 B. Minesweeper 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 One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won. 22 23 Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it? 24 25 He needs your help to check it. 26 27 A Minesweeper field is a rectangle $n \times m$, where each cell is either empty, or contains a digit from $1$ to $8$, or a bomb. The field is valid if for each cell: 28 29 * if there is a digit $k$ in the cell, then exactly $k$ neighboring cells have bombs. 30 * if the cell is empty, then all neighboring cells have no bombs. 31 32 Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most $8$ neighboring cells). 33 34 Input 35 36 The first line contains two integers $n$ and $m$ ($1 \le n, m \le 100$) — the sizes of the field. 37 38 The next $n$ lines contain the description of the field. Each line contains $m$ characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from $1$ to $8$, inclusive. 39 40 Output 41 42 Print "YES", if the field is valid and "NO" otherwise. 43 44 You can choose the case (lower or upper) for each letter arbitrarily. 45 46 Examples 47 48 Input 49 50 ``` 51 3 3 52 111 53 1*1 54 111 55 ``` 56 57 Output 58 59 ``` 60 YES 61 ``` 62 63 Input 64 65 ``` 66 2 4 67 *.*. 68 1211 69 ``` 70 71 Output 72 73 ``` 74 NO 75 ``` 76 77 Note 78 79 In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1. 80 81 You can read more about Minesweeper in [Wikipedia's article](https://en.wikipedia.org/wiki/Minesweeper_(video_game)).