P26645 [AtCoder] 10 puzzle
普及+
题目描述

Problem Description

There is a grid of height \(H\\) and width \(W\\). Initially, each cell at row \(i\\) and column \(j\\) contains a number \(A\_{i,j}\\) which is between \(0\\) and \(9\\) inclusive. Cells in the grid are adjacent if they share an edge. A subset of cells is considered "connected" if, for every pair of cells in the subset, it is possible to travel between them by repeatedly moving to adjacent cells within the subset.

Kato can perform the following operation zero or more times:

  • Select a "connected" subset of cells, and let \(M\\) be the maximum number written in any cell of this subset. Replace the number in all cells of this subset with the remainder of \(2 \\times M\\) divided by \(10\\).

Determine if Kato can make all cells in the grid contain the number \(0\\). If possible, find the minimum number of operations required to achieve this.

Input Format

The input is given from the standard input in the following format:

\(H\) \(W\)
\(A_{1,1}\ A_{1,2}\ \dots\ A_{1,W}\)
\(A_{2,1}\ A_{2,2}\ \dots\ A_{2,W}\)
\(\vdots\)
\(A_{H,1}\ A_{H,2}\ \dots\ A_{H,W}\)

Output Format

If it is possible to make all cells contain the number \(0\\), output Yes followed by the minimum number of operations \(N\\) in the following format:

Yes \(N\)

If it is not possible, output No.

Sample Input and Output

Sample Input #1

2 3
0 1 2
3 4 5

Sample Output #1

Yes 1

Sample Input #2

2 2
1 2
2 1

Sample Output #2

No

Sample Input #3

5 3
6 6 6
6 5 5
6 6 6
6 5 6
6 6 6

Sample Output #3

Yes 2

Sample Input #4

3 3
1 2 3
4 5 6
7 8 9

Sample Output #4

Yes 4

Notes/Hints

Constraints

  • \(1 \\leq H,W \\leq 100\\)
  • \(0 \\leq A\_{i,j} \\leq 9\\)
  • All values input are integers.

Sample Explanation 1

For example, if you select all cells as a subset, \(M=5\\). \(2 \\times M=10\\) and the remainder when divided by \(10\\) is \(0\\), so you can replace all numbers in the cells with \(0\\). Thus, the goal can be achieved with \(1\\) operation.

Sample Explanation 2

No matter how you perform the operations, you cannot achieve the goal.

Sample Explanation 3

For example, initially select all cells with \(6\\) as a subset (which is "connected") and perform the operation, then select all cells and perform the operation again. The goal can be achieved with \(2\\) operations.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 3 0 1 2 3 4 5
样例输出
Yes 1