Hdu-1565 square number (1) (State compression dp entry

Source: Internet
Author: User

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 <
     
      


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.