HDU 2167 Pebbles (pressure DP)

Source: Internet
Author: User

Title Link: Pebbles

Time limit:3000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1504 Accepted Submission (s): 865


Problem Descriptionyou ' re given an unlimited number of pebbles to distribute across a N x N game board (n drawn from [3, ), where each square on the board contains some positive point value between and the inclusive. A 6 x 6 board might look like this:

The player distributes pebbles across the board so:

? At the most one pebble resides in any given square.
? No. Pebbles is placed on adjacent squares. Squares is considered adjacent if they are horizontal, vertical, or even diagonal neighbors. There's no board wrap, so and all of the row three aren ' t neighbors. Neither is and 92.

The goal is to maximize the number of points claimed by your placement of pebbles.

Write a program This reads in a sequence of boards from an input file and prints to stdout the maximum number of points at Tainable by a optimal pebble placement for each.

Inputeach Board is expressed as a series of lines, where each line is a space-delimited series of numbers. A Blank line marks the end of each board (including the last one)

Outputthen your program would print the maximum number of points one can get in optimally distributing pebbles while Respe Cting the rules, which would is this (each output should is printed on a single line and followed with a newline):

The puzzle : Although the code is long, but I tuned out four hours. QaqThe binary represents the state of each row, initializes a value for each state of the entire row, denoted by cnt[i][j], and J indicates the state of the line. Then DP begins to transfer, for line I, enumerate each state J, for State J, first analyze whether J satisfies a maximum of one stone adjacent, then consider I-1 bit, analyze i-1 possible state, DFS look for i-1 row maximum Value Maxx, so dp[i][j] = Maxx + cnt[ I][J].
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespacestd;intdp[ -][1<< -];intcnt[ -][1<< -];inta[ -][ -],n;Chars[ -];intvis[ -];intMaxx =0;voidDfsintIintNowintStateintsumstate) {Maxx=Max (maxx,dp[i][sumstate]); if(vis[now]==0)    {        if(now<n-1) {DFS (I,now+1,1, sumstate); }        Else        {            return; }    }    Else if(vis[now]==1)    {        if(now>=n-1&&state==1) DFS (i,now+1,0, Sumstate+ (1<<Now )); Else if(now>=n-1&&state==0)return ; Else if(now<n-1&&state==1) {DFS (I,now+1,0, Sumstate+ (1<<Now )); DFS (I,now+1,1, sumstate); }        Else if(now<n-1&&state==0) {DFS (I,now+1,1, sumstate); }    }    return;}intCalintC1,intC2) {    return((c1-'0')*Ten+c2-'0');}BOOLJudgeintState//determine if this state conforms to{     for(intI=0; i<n-1; i++)    {        if(((1<<i) &state) >0&& (1<< (i+1)) > &state)0)return 0; }    return 1;}intMain () { while(gets (s)) {intLen =strlen (s); N= (len+1)/3; intAns =0;  for(intI=0; i<len;i+=3) {a[0][ans++] = cal (s[i],s[i+1]); }         for(intI=1; i<n;i++)        {             for(intj=0; j<n;j++) {scanf ("%d",&A[i][j]); }        }         for(intI=0; i<n;i++)//preprocessing Records CNT[I][J]        {             for(intj=0;j< (1<< (n)); J + +)            {                intsum =0;  for(intk=0; k<n;k++)                {                    if(((1<<k) &j) >0) {sum+=A[i][k]; }} Cnt[i][j]=sum; }        }         for(intj=0;j< (1<<n), J + +) dp[0][J] = cnt[0][j];  for(intI=1; i<n;i++)//enumerate each row, enumerate each state, and judge the feasibility.         {             for(intj=0;j< (1<<n); j + +)            {                if(!judge (j))Continue; memset (Vis,0,sizeof(VIS));  for(intk=0; k<n;k++)                {                    if(k==0)                    {                        if(((1<<k) &j) = =0&& (1<< (k +1)) (&j) = =0) {Vis[k]=1; }                    }                    Else if(k==n-1)                    {                        if(((1<<k) &j) = =0&& (1<< (K-1)) (&j) = =0) {Vis[k]=1; }                    }                    Else                    {                        if(((1<<k) &j) = =0&& (1<< (K-1)) (&j) = =0&& (1<< (k +1)) (&j) = =0) {Vis[k]=1; } }} Maxx=0;//Dfs looks for the maximum value of i-1 rows                if(vis[0]==0) DFS (i-1,0,0,0); Else{DFS (i-1,0,1,0); DFS (i-1,0,0,0); } Dp[i][j]= maxx+Cnt[i][j];        }} getchar ();        Gets (s); intMAXN =0;  for(intI=0;i< (1<<n); i++) MAXN = max (maxn,dp[n-1][i]); printf ("%d\n", MAXN); Memset (DP,0,sizeof(DP)); }    return 0;}/*Ten 3010 3010*/

HDU 2167 Pebbles (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.