题意:给定4堆糖果,每堆有n颗,有一个最多可以装5个糖果的袋子,每次选择一堆糖果,把最顶上的一颗拿到袋子里,如果袋子里有两颗一样的糖果就拿走。问最多可拿几对。
分析:枚举每次可以从4堆中任意一堆拿走。
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<sstream>
#include<cassert>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 40 + 10;
int d[maxn][maxn][maxn][maxn];
int vis[maxn],n;
int mp[maxn][4];
int top[4];
int dp(int k) {
int &ans = d[top[0]][top[1]][top[2]][top[3]];
if (ans != -1)return ans;
ans = 0;
if (k >= 5)return ans;
for (int i = 0; i < 4; i++) {
if (top[i] == n)continue;
++top[i];
if (vis[mp[top[i]][i]]) {
vis[mp[top[i]][i]] = 0;
ans = max(ans, dp(k - 1) + 1);
vis[mp[top[i]][i]] = 1;
}
else {
vis[mp[top[i]][i]] = 1;
ans = max(ans, dp(k+1));
vis[mp[top[i]][i]] = 0;
}
--top[i];
}
return ans;
}
int main() {
while (cin >> n && n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 4; j++)
cin >> mp[i][j];
}
memset(vis, 0, sizeof(vis));
memset(d, -1, sizeof(d));
memset(top, 0, sizeof(top));
cout << dp(0) << endl;
}
return 0;
}