#P1123. 取数游戏
取数游戏
Description
Given an matrix of non-negative integers, you need to select some numbers such that no two selected numbers are adjacent. Two numbers are considered adjacent if one is in any of the 8 neighboring cells of the other (including diagonals). Find the maximum possible sum of the selected numbers.
Input Format
The first line contains a positive integer , indicating there are test cases.
For each test case, the first line contains two positive integers and , indicating the matrix has rows and columns.
The next lines each contain non-negative integers, describing the matrix.
Output Format
Output lines, each containing a single non-negative integer: the required answer.
3
4 4
67 75 63 10
29 29 92 14
21 68 71 56
8 67 91 25
2 3
87 70 85
10 3 17
3 3
1 1 1
1 99 1
1 1 1
271
172
99
Hint
Sample Explanation
For the first test case, a valid selection is as follows:
$$\begin{matrix} [67] & 75 & 63 & 10 \\ 29 & 29 & [92] & 14 \\ [21] & 68 & 71 & 56 \\ 8 & 67 & [91] & 25 \\ \end{matrix}$$Constraints
- For 20% of the testdata, .
- For 40% of the testdata, .
- For 60% of the testdata, .
- For 100% of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号