uva 10118 Free Candies
2015-10-26 22:33
471 查看
Problem Description
Little Bob is playing a game. He wants to win some candies in it - asmany 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.
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
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
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int maxn = 50; int n; int f[maxn][maxn][maxn][maxn]; int piles[maxn][maxn]; int dp(int* r,int s,int* t) { if(s==5) return f[t[0]][t[1]][t[2]][t[3]]=0; if(f[t[0]][t[1]][t[2]][t[3]] >= 0)return f[t[0]][t[1]][t[2]][t[3]]; f[t[0]][t[1]][t[2]][t[3]] = 0; for(int i = 0; i < 4 ; i++ ) { if(t[i]==n)continue; if(r[piles[i][t[i]]]==1) { int &v = f[t[0]][t[1]][t[2]][t[3]]; r[piles[i][t[i]]]=0; t[i]++; v = max(v,dp(r,s-1,t) + 1); t[i]--; r[piles[i][t[i]]]=1; } else { int& v = f[t[0]][t[1]][t[2]][t[3]]; r[piles[i][t[i]]]=1; t[i]++; v = max(v,dp(r,s+1,t)); t[i]--; r[piles[i][t[i]]]=0; } } return f[t[0]][t[1]][t[2]][t[3]]; } int main() { while((cin >> n)&&n) { memset(f,-1,sizeof(f)); for(int i = 0 ; i < n ; i++ ) for(int j = 0 ; j < 4 ; j++ ) cin >> piles[j][i]; int r[25],top[4]={0,0,0,0}; memset(r,0,sizeof(r)); int ans = dp(r,0,top); cout << ans << endl; } return 0; }
相关文章推荐
- wamp offline:在浏览器中输入localhost提示not found
- 对C++中Boost库的认识
- 数学函数/统计函数/概率分布/字符串函数
- 易云盘(nester)-面对企业的和团队文档管理的平台
- 如何安装设置opencart插件(插件安装)
- C头文件
- 准确理解javascript中的this的指向
- 如何设置eclipse下查看java源码
- 高项2015年下半年培训随堂作业(10月日)
- vlsm的划分
- Alpha-Blending 技术简介
- 将表达式转换为逆波兰表达式
- 微信聊天记录数据存储机制
- CentOS6.5系统挂载NTFS分区的硬盘
- 2015年10月26日作业
- Android SurfaceFlinger and its' client
- PeekMessage和GetMessage
- 变更管理、安全管理和风险管理作业
- h264 NAL头解析
- 向量与因子操作