HDU 1565 square fetch (1) (pressure DP)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1565


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
 
Authorailyanlu
Sourcehappy 2007
Recommend8600 | we have carefully selected several similar problems for you: 1569 3338 1533 1733 3061

PS: (transfer)

Give you a checkboard of N * n. Each checkboard contains a non-negative number,
So that the lattice of any two numbers does not have a public edge, that is, the two grids of the obtained number cannot be adjacent, and the sum of the retrieved number and the maximum


3

75 15 21

75 15 28


34 70 5

188


For each number, or whether to take or not, 1 is to take this number, and 0 is not to take this number. For the number of each line,
Its status can be described by a binary number. For the first line, if we take 75, 21,
We can describe it in binary 5, that is, 101, because the two grids where the number is located cannot be adjacent,
Therefore, the binary number of each row cannot have an adjacent value of 1. Let's look at the column again. The adjacent two rows cannot have adjacent values,
For two binary values, that is, the relationship between the current row and the previous row is obtained,
DP [I] [J] = DP [I-1] [k] + TT


DP [I] [J] indicates row I in J state, DP [I-1] [k] indicates row I in K State,
TT indicates the sum of the number of th row in status K. Of course, K & J = 0


The Code is as follows:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 26; const int maxn = 18017; int N; int mm [maxn] [maxn]; int num [maxn] = {64,128,256,512,102, 1048576 }; // initialize num. N digits: 2-digit number. A maximum of num [N] int A [maxn], DP [maxn] [maxn], and INT is_use (int I) // determine whether the binary value of this number can be used as a non-adjacent number {int TT = 0; while (I) {If (TT = 1 & I % 2 = 1) // If Adjacent 1. For example, if the binary value of 3 is 011, it cannot be used! {Return 0;} TT = I % 2; I/= 2;} return 1;} int main () {int L = 0; For (INT I = 0; I <num [20]; I ++) // record all the numbers that can be expressed {If (is_use (I) {A [L ++] = I ;}} while (~ Scanf ("% d", & N) {If (n = 0) {printf ("0 \ n"); continue;} memset (DP, 0, sizeof (DP); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {scanf ("% d", & mm [I] [J]) ;}} int Maxx =-1; for (INT I = 0; I <L; I ++) // calculate the first line {if (a [I]> = num [N]) // n-digit two-digit number: a maximum of S [N] Break; For (Int J = 0; j <n; j ++) // sum the number of each State from right to left {if (a [I] & num [J]) // The first bit in the St [I] binary is the correct DP [0] [I] + = mm [0] [J];} if (Maxx <DP [0] [I]) Maxx = DP [0] [I];} int TT = 0; For (INT I = 1; I <N; I ++) {for (Int J = 0; j <L; j ++) {if (a [J]> = num [N]) break; TT = 0; for (int K = 0; k <n; k ++) // sum of all numbers obtained by row I in the J state {if (a [J] & num [k]) // The first bit in the St [J] binary is the TT + = mm [I] [k];} For (int K = 0; k <L; k ++) {if (a [k]> = num [N]) break; If (A [J] & A [k]) = 0) // The upper and lower points are not adjacent {DP [I] [J] = max (DP [I] [J], DP [I-1] [k] + TT );}} if (DP [I] [J]> Maxx) {Maxx = DP [I] [J] ;}} printf ("% d \ n", Maxx );} return 0 ;}


HDU 1565 square fetch (1) (pressure DP)

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.