您的位置:首页 > 其它

uva 10118 (Free Candies)(DP)

2014-01-09 00:11 316 查看

Problem B. Free Candies

The Problem

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 outsidethe 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):
Step1Initial PilesStep2Take one from pile #2
PilesBasketPocketPilesBasketPocket
1 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
nothingnothing
1   3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 1
2nothing
Step3Take one from pile #2Step4Take one from pile #3
PilesBasketPocketPilesBasketPocket
1   3 4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 5nothing
1     4
1   6 7
2 3 3 3
4 9 8 6
8 7 2 1
2 3 5nothing
Step5Take one from pile #2Step6put two candies into his pocket
PilesBasketPocketPilesBasketPocket
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 3 3 5nothing
1     4
1   6 7
2   3 3
4 9 8 6
8 7 2 1
2 5a pair of 3
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?

The Input

The input will contain no 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.

The 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

51 2 3 4
1 5 6 7
2 3 3 3
4 9 8 6
8 7 2 111 2 3 431 2 3 45 6 7 81 2 3 40

Sample Output

8
0
3
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <stack>using namespace std;const int maxn = 50;int dp[maxn][maxn][maxn][maxn] , dep[4];vector<int> v[4];int n;void initial(){memset(dp , -1 , sizeof dp);for(int i = 0;i < 4;i++){v[i].clear();dep[i] = 0;}}void readcase(){int a;for(int i = 0;i < n;i++){for(int j = 0;j < 4;j++){scanf("%d" , &a);v[j].push_back(a);}}}int DP(stack<int> basket){//cout << ans << endl;if(basket.size() >= 5)	return 0;if(dp[dep[0]][dep[1]][dep[2]][dep[3]] != -1) return dp[dep[0]][dep[1]][dep[2]][dep[3]];int tt = 0;for(int i = 0;i < 4;i++){if(dep[i] < n){//cout << "dep[i]=" << i << endl;basket.push(v[i][dep[i]]);int T = 1;stack<int> tem;int f = basket.top();basket.pop();while(!basket.empty()){int f1 = basket.top();basket.pop();if(f1 != f){tem.push(f1);}else{//cout << ans << endl;T = 0;}}while(!tem.empty()){basket.push(tem.top());tem.pop();}if(T){basket.push(f);}dep[i]++;tt = max(tt , DP(basket)+!T);dep[i]--;if(T) basket.pop();else basket.push(v[i][dep[i]]);}}//cout << dep[0] << " " << dep[1] << " " << dep[2] << " " << dep[3] << " =:" << tt <<endl;return dp[dep[0]][dep[1]][dep[2]][dep[3]] = tt;}int main(){while(scanf("%d" , &n) == 1 && n){initial();readcase();stack<int> te;cout << DP(te) << endl;}return 0;}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: