Problem Description
Little Bob is playing a game. He wants to win some candies in it - as many as possible.
There are 4 piles, each pile contains N candies. Bob is given a basket which can hold at most 5 candies. Each time, he puts a candy at the top of one pile into the basket, and if there’re two candies of the same color in it, he can take both of them outside the basket and put them into his own pocket. When the basket is full and there are no two candies of the same color, the game ends. If the game is
played perfectly, the game will end with no candies left in the piles. For example, Bob may play this game like this (N = 5):
Note that different numbers indicate different colors, there are 20 kinds of colors numbered 1..20.
‘Seems so hard…’ Bob got very much puzzled. How many pairs of candies could he take home at most?
Input
The input will contain not more than 10 test cases. Each test case begins with a line containing a single integer n(1 ≤ n ≤ 40) representing the height of the piles. In the following n lines, each line contains four integers xi1 , xi2 , xi3 , xi4 (in the range 1..20). Each integer indicates the color of the corresponding candy. The test case containing n = 0 will terminate the input, you should not give an answer to this case.
Output
Output the number of pairs of candies that the cleverest little child can take home. Print your answer in a single line for each test case.
Sample Input
5
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
1
1 2 3 4
3
1 2 3 4
5 6 7 8
1 2 3 4
0
Sample Output
8 0 3
Problem Report
这道题数据规模很小,因此可以考虑用搜索求解。在搜索的过程中,状态可能重复,例如4组分别剩余1,2,3,4。我在实现这道题的时候用了一些技巧
- 用stack来保存当前糖果堆的状态,用set来保存篮子里的糖(用map的话在初始化上会出一些问题),并用unordered_map来保存口袋里的状态
- 类似状态压缩的处理。但事实证明这样做有导致爆栈的可能(有时甚至会TLE)
My Source Code
// Created by Chlerry in 2015.
// Copyright (c) 2015 Chlerry. All rights reserved.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
#define Size 41
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
int n,input;
stack<int> ts[4],s[4];
set<int> ba;
unordered_map<int,int> po;
#define status s[0].size()*1000000+s[1].size()*10000+s[2].size()*100+s[3].size()
int DFS()
{
//cout<<"status="<<status<<" ba="<<ba.size()<<endl;
if(po[status])
return po[status];
int cur=status;
for(int i=0;i<4;i++) if(s[i].size() && ba.size()<5)
{
int t=s[i].top();s[i].pop();
if(ba.find(t)!=ba.end())
{
ba.erase(t);
po[cur]=max(po[cur],1+DFS());
ba.insert(t);
}
else
{
ba.insert(t);
po[cur]=max(po[cur],DFS());
ba.erase(t);
}
s[i].push(t);
}
return po[cur];
}
int main()
{
freopen("in.txt","r",stdin);
while(cin>>n && n)
{
ba.clear();po.clear();
for(int i=0;i<4;i++)
while(!s[i].empty())
s[i].pop();
for(int j=0;j<n;j++)
for(int i=0;i<4;i++)
cin>>input,ts[i].push(input);
for(int j=0;j<n;j++)
for(int i=0;i<4;i++)
s[i].push(ts[i].top()),ts[i].pop();
cout<<DFS()<<endl;//<<"-----------\n";
}
return 0;
}