标签:
题解:
记忆化搜索
如何判断一个数是否已经出现?应用位运算即可....
代码:
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,y,sizeof(x)) #define SZ(x) ((int)(x).size()) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef pair<int,int> P; const double eps=1e-9; const int maxn=200100; const int mod=1e9+7; const int INF=1e9; int n; int mat[4][45]; int dp[45][45][45][45]; int DP(int *top,int st,int k) { int &m=dp[top[0]][top[1]][top[2]][top[3]]; if(m>=0) return m; if(top[0]==n&&top[1]==n&&top[2]==n&&top[3]==n||k==5) return m=0; for(int i=0;i<4;i++) if(top[i]<n) { int bit=1<<mat[i][top[i]]; top[i]++; if(bit&st) m=max(m,DP(top,st-bit,k-1)+1); else m=max(m,DP(top,st+bit,k+1)); top[i]--; } return m; } int main() { while(~scanf("%d",&n)&&n) { memset(dp,-1,sizeof(dp)); for(int i=0;i<n;i++) scanf("%d%d%d%d",&mat[0][i],&mat[1][i],&mat[2][i],&mat[3][i]); int top[5]={0}; printf("%d\n",DP(top,0,0)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/byene/p/5950685.html