Number of squares (1)
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4702 Accepted Submission (s): 1782
Problem Description gives you an n * n checkboard. Each checkboard contains a non-negative number.
Take out several numbers from them so that the lattice of any two numbers does not have a public edge. That is to say, the two grids of the obtained number cannot be adjacent and the sum of the obtained numbers is the largest.
Input includes multiple test instances. Each test instance includes an integer n and n x n non-negative numbers (n <= 20)
Output for each test instance, the maximum sum possible for the Output
Sample Input
375 15 21 75 15 28 34 70 5
Sample Output
188
Get started with compressing dp, similar to Poj-3254 Corn Fields.
Use dp [I] [j] to represent the optimal solution for selecting the j state for the first line I,
First, find all the non-conflicting states of the row (that is, this row does not have any adjacent conditions), and store the state array
The value obtained when row I is in the status j [I] [j]
So dp [I] [j] = max {dp [I] [j], dp [I-1] [k] + stn [I] [j]} (k indicates the k State of the I-1 row
The final answer is the maximum value in dp [n] [j.
Code:
# Include
# Include
# Include
Using namespace std; const int hpn = 18000; int state [hpn], stn [25] [hpn], dp [25] [hpn]; // dp [I] [j]: the optimal solution int mst, map [25] when the first I row and the I row select the j state. [25] // inline int bet (int x, int y) {if (x> y) return x; return y ;} void find_all_state (int n) {memset (state, 0, sizeof (state); mst = 0; // the maximum number of States int lin = (1 <
> N) {if (n = 0) {cout <0 <