Pick Game
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 119 Accepted Submission(s): 20
Problem Description
This is a pick game.
On a n*m matrix, each gird has a value. The player could only choose the gird that is adjacent to at least two empty girds (A grid outside the matrix also regard as empty). Adjacent means two girds share a common edge. If one play chooses one gird, he will get the value and the gird will be empty. They play in turn.
One day, WKC plays this game with ZJS.
Both of them are clever students, so they will choose the best strategy.
WKC plays first, and he wants to know the maximal value he could get.
On a n*m matrix, each gird has a value. The player could only choose the gird that is adjacent to at least two empty girds (A grid outside the matrix also regard as empty). Adjacent means two girds share a common edge. If one play chooses one gird, he will get the value and the gird will be empty. They play in turn.
One day, WKC plays this game with ZJS.
Both of them are clever students, so they will choose the best strategy.
WKC plays first, and he wants to know the maximal value he could get.
Input
There is a positive integer T(1<=T<=50) in the first line, which specifying the number of test cases to follow.
Each test case begins with two numbers n and m ( 2 <= n, m <= 5 ).
Then n lines follow and each lines with m numbers Vij (0< Vij <=1000).
Each test case begins with two numbers n and m ( 2 <= n, m <= 5 ).
Then n lines follow and each lines with m numbers Vij (0< Vij <=1000).
Output
Output the maximal value WKC could get.
Sample Input
1 2 2 9 8 7 6
Sample Output
16
![快哭了](http://static.blog.csdn.net/xheditor/xheditor_emot/default/fastcry.gif)
#include <iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #define mod 10007 #define N 10010 #include<vector> using namespace std; vector<int> v[N],dp[N]; int n,m,cnt,Map[6][6]; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; bool ok(int x,int y) { if(x<n&&x>=0&&y<m&&y>=0) return true; return false; } int Gets(int st) { int ret=0; for(int i=0; i<cnt; i++) { if(((1<<i)&st)==0) { ret+=Map[i/m][i%m]; } } return ret; } int cal(int st) { int ret=0,i; for(i=0; i<cnt; i++) if((1<<i)&st) ret++; return ret; } int dfs(int st) { int next,ans,i,j,tmp,x,y,xx,yy; x=st%mod; for(y=v[x].size()-1; y>=0; y--) { if(v[x][y]==st) return dp[x][y]; } if(cal(st)==cnt-1) return Gets(st); ans=-20006; for(i=0; i<cnt; i++) { if(((1<<i)&st)==0) { x=i/m; y=i%m; tmp=0; for(j=0; j<4; j++) { xx=x+dir[j][0]; yy=y+dir[j][1]; if(!ok(xx,yy)||(ok(xx,yy)&&(((1<<(xx*m+yy))&st)))) { tmp++; } } if(tmp>=2) { next=(1<<i) | st; ans=max(ans,Map[x][y]-dfs(next)); } } } v[st%mod].push_back(st); dp[st%mod].push_back(ans); return ans; } int main() { int t,i,j; scanf("%d",&t); while(t--) { for(i=0; i<mod; i++) { dp[i].clear(); v[i].clear(); } scanf("%d%d",&n,&m); for(i=0; i<n; i++) for(j=0; j<m; j++) scanf("%d",&Map[i][j]); cnt=n*m; printf("%d ",(dfs(0)+Gets(0))/2); } return 0; }
![吐舌头](http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif)
![哭](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)