Plan
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 54
Problem DescriptionOne day, Resty comes to an incredible world to seek Eve -- The origin of life. Lilith, the sister of Eve, comes with him. Although Resty wants to find Eve as soon as possible, Lilith likes to play games so much that you can't make
her make any move if you don't play with her.
Now they comes to the magical world and Lilish ask Resty to play with her.
The game is following :
Now the world is divided into a m * n grids by Lilith, and Lilith gives each grid a score.
So we can use a matrix to describe it.
You should come from cell(0, 0) to cell(m-1, n-1) (Up-Left to Down-Right) and try to colloct as more score as possible.
According to Lilish's rule, you can't arrive at each cell more than once.
Resty knows that Lilish will be easy to find the max score, and he doesn't want to lose the game.
So he want to find the game plan to reach the max score.
Your task is to calculate the max score that Lilish will find, the map is so small so it shouldn't be difficult for you, right?
InputThe input consists of more than one testdata.
Process to the END OF DATA.
For each test data :
the first live give m and n. (1<=m<=8, 1<=n<=9)
following m lines, each contain n number to give you the m*n matrix.
each number in the matrix is between -2000 and 2000
OutputOutput Format is "Case ID: ANS" one line for each data
Don't print any empty line to the output
Sample Input
2 21 23 13 30 -20 1001 -20 -201 1 1
Sample Output
Case 1: 5Case 2: 61
AuthorResty
SourceHDOJ Monthly Contest – 2010.04.04
Recommendlcy
分析:這題類似樓教主那題,只是可以有的格不訪問,只要加上空插頭不延伸出插頭這種情況,還有狀態轉移是改為取最大值,然後還是套用模板,水~~~
代碼:
#include<cstdio><br />#include<cstring><br />using namespace std;<br />const int mm=15511;<br />struct data<br />{<br /> int s[mm],h[mm],p[mm],t,d[mm];<br /> int hash(int x,int dd)<br /> {<br /> int i,c=x%mm;<br /> for(i=h[c];i>=0;i=p[i])<br /> if(s[i]==x)<br /> {<br /> if(dd>d[i])d[i]=dd;<br /> return i;<br /> }<br /> s[t]=x,p[t]=h[c],h[c]=t,d[t]=dd;<br /> return t++;<br /> }<br /> void clear()<br /> {<br /> t=0,memset(h,-1,sizeof(h));<br /> }<br />}f[2];<br />int i,j,g1,g2,u,k,n,m,a,b,a1,a2,b1,b2,g[13][13];<br />int eat(int s,int a,int b,int c,bool f)<br />{<br /> int n=1,x;<br /> while(n)<br /> {<br /> if(f)a<<=2,b<<=2,c<<=2;<br /> else a>>=2,b>>=2,c>>=2;<br /> x=s&c;<br /> if(x==a)++n;<br /> if(x==b)--n;<br /> }<br /> return (s^b)|a;<br />}<br />void work(int S,int d)<br />{<br /> int x,y,dd=d+g[i][j];<br /> x=a&S,y=b&S;<br /> if(x==0&&y==0)<br /> {<br /> if(i<n&&j<m)f[g2].hash(S|a1|b2,dd);<br /> f[g2].hash(S,d);<br /> }<br /> else if(x==0&&y==b1)<br /> {<br /> if(j<m)f[g2].hash(S,dd);<br /> if(i<n)f[g2].hash((S^b1)|a1,dd);<br /> }<br /> else if(x==a1&&y==0)<br /> {<br /> if(i<n)f[g2].hash(S,dd);<br /> if(j<m)f[g2].hash((S^a1)|b1,dd);<br /> }<br /> else if(x==a1&&y==b1)f[g2].hash(eat(S^a1^b1,b1,b2,b,1),dd);<br /> else if(x==0&&y==b2)<br /> {<br /> if(j<m)f[g2].hash(S,dd);<br /> if(i<n)f[g2].hash((S^b2)|a2,dd);<br /> }<br /> else if(x==a2&&y==0)<br /> {<br /> if(i<n)f[g2].hash(S,dd);<br /> if(j<m)f[g2].hash((S^a2)|b2,dd);<br /> }<br /> else if(x==a2&&y==b2)f[g2].hash(eat(S^a2^b2,a2,a1,a,0),dd);<br /> else if(x==a2&&y==b1)f[g2].hash(S^a2^b1,dd);<br />}<br />int DP()<br />{<br /> int ans=-111111111;<br /> f[0].clear();<br /> f[0].hash(1,0);<br /> for(--n,--m,g1=1,g2=i=0;i<=n;++i)<br /> {<br /> for(k=0;k<f[g2].t;++k)f[g2].s[k]<<=2;<br /> a=3,b=3<<2,a1=1,a2=2,b1=1<<2,b2=2<<2;<br /> for(j=0;j<=m;a<<=2,b<<=2,a1<<=2,a2<<=2,b1<<=2,b2<<=2,++j)<br /> if(g[i][j])for(g1=!g1,g2=!g2,f[g2].clear(),k=0;k<f[g1].t;++k)<br /> {<br /> if(i==n&&j==m)<br /> {<br /> if((f[g1].s[k]==b1||f[g1].s[k]==a1)&&f[g1].d[k]>ans)ans=f[g1].d[k];else continue;<br /> }<br /> work(f[g1].s[k],f[g1].d[k]);<br /> }<br /> }<br /> return ans+g[n][m];<br />}<br />int main()<br />{<br /> int t=0;<br /> while(scanf("%d%d",&n,&m)!=-1)<br /> {<br /> for(i=0;i<n;++i)<br /> for(j=0;j<m;++j)scanf("%d",&g[i][j]);<br /> printf("Case %d: %d/n",++t,DP());<br /> }<br /> return 0;<br />}