"Bzoj 1187" [HNOI2007] Magical Amusement Park

Source: Internet
Author: User

1187: [HNOI2007] Magical Amusement Park

Time Limit:10 Sec Memory limit:162 MB
submit:706 solved:358
[Submit] [Status] [Discuss]
Description

After a hard journey, the protagonist's little p rides back in the airship. On the way back, small p found in the rambling desert, there is a narrow green space is particularly conspicuous. Look down carefully, only to find this is a playground, designed for the tired people on the road. The casino can be seen as an area of nxm size, and the NXM area is divided into nxm small squares, each with an entertaining item. However, little p did not like all of the entertainment items, so he gave each project a satisfaction level. Satisfaction is positive when the small P likes this item, the larger the value the more like it. Negative means he doesn't like it, the greater the absolute value of the negative number means the less he likes it. When he was 0, he was not fond of the project. Little P decided to park the airship in a small lattice, and then each step he could move to a grid of four squares adjacent to each other. Small p wants to find a path, starting from the airship, and finally back to the grid. Little P has a habit, never like wasting time. So he wants to go through every lattice that makes sense: when he's in a place, he must feel the thrills and thrills of the following, whether he likes the entertainment there or not. Moreover, in addition to the airship in the grid, the other lattice he would not want to pass two times. Little P wants to go through at least four squares of his own. In the case of satisfying these conditions, little p wants to have the highest degree of satisfaction with the entertainment items he has played. Can you help him find the highest degree of satisfaction?

Input

The first behavior in the input file is two positive integers n and m, indicating that the playground size is NXM. Because this casino is very narrow, so N and M meet: 2<=n<=100,2<=m<=6. In the next n rows, each line has m integers, and row i, Column J, indicates the satisfaction of the amusement item in the small lattice of the amusement park in row I, Column J, and this satisfaction range is [ -1000,1000]. Two integers in the same row are separated by a space.

Output

Only one behavior in the output file is an integer representing the highest degree of satisfaction.

Sample Input

4 4

100 300-400 400

-100 1000 1000 1000

-100-100-100-100

-100-100-100 1000
Sample Output

4000
HINT

Let's all figure this out. 5 5 1 1-100 3 3 1 1-100 3 3 1 1-100 3 3 1 1-100 3 3 1 1-100 3 3 The result is 30?

Plug DP.

A templated transfer.

The solution is that the current grid plug is (), and there is no other plug on the entire contour line. (Because if there is a plug in other places, it will inevitably appear in the next few lines () plug situation, will be more forget)

You can speed up your code by using a hash table to store the four-state.

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <cstdio>#define INF 0x3f3f3f3f#define MODusing namespace STD;inttot=0, state[2][ the],a[205][205],h[505],f[2][ the],total[2],now,pre,n,m;intbit[ -];structedge{intNE;} e[ the];voidSolve (intSintNUM) {intX=s%mod; for(intI=h[x];i;i=e[i].ne)if(state[now][i]==s) {if(Num>f[now][i]) f[now][i]=num;return;    } tot++;    State[now][tot]=s;    F[now][tot]=num;    E[TOT].NE=H[X]; H[x]=tot;}voidPLUGDP () {now=0; state[now][1]=0; total[now]=1; f[now][1]=0;intAns=-inf; for(intI=1; i<=n;i++) { for(intj=1; j<=total[now];j++) state[now][j]<<=2; for(intj=1; j<=m;j++) {tot=0;memset(H,0,sizeof(h)); pre=now,now=pre^1; total[now]=0;memset(f[now],-127,sizeof(F[now])); for(intk=1; k<=total[pre];k++) {intS=STATE[PRE][K];intNUM=A[I][J]+F[PRE][K];intP= (s>>bit[j-1])%4, q= (S>>bit[j])%4;if(p+q==0) {Solve (s,num-a[i][j]);if(j!=m) Solve (S+ (1<<bit[j-1])+2*(1&LT;&LT;BIT[J]), num); }Else if(!P&AMP;&AMP;Q) {if(j!=m)                    Solve (S,num); Solve (s-q* (1&LT;&LT;BIT[J]) +q* (1<<bit[j-1]), num); }Else if(P&AMP;&AMP;!Q) {Solve (s,num);if(j!=m) Solve (s-p* (1<<bit[j-1]) +p* (1&LT;&LT;BIT[J]), num); }Else if(p+q==2)                {intb=1; for(intt=j+1; t<=m;t++) {intv= (S>>bit[t])%4;if(v==1) b++;if(v==2) b--;if(!B) {s=s-(1<<bit[t]); Break; }} Solve (S-(1<<bit[j-1])-(1&LT;&LT;BIT[J]), num); }Else if(p+q==4)                {intb=-1; for(intt=j-2; t>=0; t--) {intv= (S>>bit[t])%4;if(v==1) b++;if(v==2) b--;if(!B) {s=s+ (1<<bit[t]); Break; }} Solve (S-2*(1<<bit[j-1])-2*(1&LT;&LT;BIT[J]), num); }Else if(p==1&&q==2)                {if(s== (p<<bit[j-1]) + (Q<<bit[j]) &&ans<num) Ans=num; }Else if(p==2&&q==1) {Solve (S-2*(1<<bit[j-1])-(1&LT;&LT;BIT[J]), num);        }} Total[now]=tot; }    }printf("%d\n", ans);}intMain () {scanf("%d%d", &n,&m); for(intI=1; i<=n;i++) for(intj=1; j<=m;j++)scanf("%d", &a[i][j]); for(intI=0; i<= the; i++) bit[i]=i<<1; PLUGDP ();return 0;}

Sentiment:
1.RE is because the last one in a row continues to add __+ Such a plug, the next line of processing when the direct <<2, resulting in the final state is too large.

2. Always submit and find that a contour line has a valid status of no more than 105.

"Bzoj 1187" [HNOI2007] Magical Amusement Park

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.