#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int vis[30],top[5],dp[60][60][60][60],n,d[4][60];
int dfs(int k){
int &t=dp[top[0]][top[1]][top[2]][top[3]];
if(t != -1) return t;
t=0;
if(k >= 5 || top[0]+top[1]+top[2]+top[3] == n * 4) return t;
for(int i=0;i<4;i++){
if(top[i] >= n) continue;
top[i]++;
if(vis[d[i][top[i]]]){
vis[d[i][top[i]]]=0;
t=max(dfs(k-1)+1,t);
vis[d[i][top[i]]]=1;
}
else{
vis[d[i][top[i]]]=1;
t=max(dfs(k+1),t);
vis[d[i][top[i]]]=0;
}
top[i]--;
}
return t;
}
int main(){
while(scanf("%d",&n) == 1 && n){
for(int i = 1;i <= n; i++){
for(int j = 0;j < 4; j++){
scanf("%d",&d[j][i]);
}
}
memset(vis,0,sizeof(vis));
memset(top,0,sizeof(top));
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(0));
}
}
uva 10118 Free Candies
最新推荐文章于 2020-01-22 18:49:53 发布