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):
Step1 | Initial Piles | Step2 | Take one from pile #2 | ||
Piles | Basket | Piles | Basket | ||
1 2 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | nothing | nothing | 1 3 4 1 5 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 | nothing |
Step3 | Take one from pile #2 | Step4 | Take one from pile #3 | ||
Piles | Basket | Piles | Basket | ||
1 3 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 5 | nothing | 1 4 1 6 7 2 3 3 3 4 9 8 6 8 7 2 1 | 2 3 5 | nothing |
Step5 | Take one from pile #2 | Step6 | put two candies into his pocket | ||
Piles | Basket | Piles | Basket | ||
1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 | 2 3 3 5 | nothing | 1 4 1 6 7 2 3 3 4 9 8 6 8 7 2 1 | 2 5 | a 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?
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.
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
80
3
这道题直接搜索是不行的,因为会搜很多重复的,最后第一组数据都过不了,
所以需要找状态,当4个栈顶在之前遍历过,那么还需要遍历吗,不需要吧,
所以这样就可以采用记忆化搜索保存状态,统计对数最多的就行了。
#include <stdio.h>
#include <string.h>
const int maxn = 25, N = 45;
int n, ans, top[5], map[N][N], cnt[maxn];
bool vis[N][N][N][N];
void init ( )
{
memset ( top, 0, sizeof ( top ) ); //刷新栈顶
memset ( cnt, 0, sizeof ( cnt ) ); //统计数字出现的次数
memset ( vis, 0, sizeof ( vis ) ); //当4个栈顶位置相同时就剪枝
}
void dfs ( int k, int ct )
{
if ( ct > ans )
ans = ct;
if ( k >= 5 )
return ;
bool & tag = vis[ top[0] ][ top[1] ][ top[2] ][ top[3] ];
if ( tag ) //4个栈顶位置遍历过就返回
return ;
tag = true;
for ( int i = 0; i < 4; i ++ )
{
if ( top[i] >= n ) //超过n个
continue ;
int j = map[ top[i] ][i], flag = 1;
if ( cnt[j] == 1 )
{
flag = -1;
cnt[j] = 0;
}
else
cnt[j] = 1;
top[i] ++;
dfs ( k+flag, ct+( flag == -1 ) ); //统计篮子装的个数和可以拿走的对数
if ( flag == 1 ) //注意回溯
cnt[j] = 0;
else
cnt[j] = 1;
top[i] --;
}
}
int main ( )
{
int v;
//freopen ( "in0.out", "w", stdout );
while ( ~ scanf ( "%d", &n ) && n )
{
init ( );
for ( int i = 0; i < n; i ++ )
for ( int j = 0; j < 4; j ++ )
scanf ( "%d", &map[i][j] );
ans = 0;
dfs ( 0, 0 );
printf ( "%d\n", ans );
}
return 0;
}