ZOJ1227(Free Candies)

Free Candies

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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 Pocket Piles Basket Pocket
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 Pocket Piles Basket Pocket
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 Pocket Piles Basket Pocket
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

8
0
3

#include <iostream>
#include 
<cstring>
#include 
<queue>
#include 
<set>
using namespace std;

const int N = 42;

struct Node
{
    
set<int> basket;//记录basket中的candies号
    int top[4];
    
int pocket;//记录pocket中已有candies数
}
;

int candies[4][N];
bool Visited[N][N][N][N];
int n, ans;

void bfs()
{
    Node a, b;
    
set<int>::iterator p;
    queue
<Node> Q;
    
int i, cc;

    memset(Visited, 
0sizeof(Visited));

    a.pocket 
= 0;
    a.top[
0= a.top[1= a.top[2= a.top[3= 0;
    a.basket.clear();
    Visited[
0][0][0][0= true;
    Q.push(a);

    
while(!Q.empty())
    
{
        a 
= Q.front();
        Q.pop();

        
for(i = 0; i < 4; i++)
        
{
            b 
= a;
            
            
if(b.top[i] < n)
            
{
                cc 
= candies[i][b.top[i]];
                b.top[i] 
++;                
                
                p 
= b.basket.find(cc);
                
if(p != b.basket.end())
                
{
                    b.pocket
++;
                    b.basket.erase(p);
                    
if(b.pocket > ans)
                        ans 
= b.pocket;
                }

                
else
                
{
                    b.basket.insert(cc);
                }


                
if(b.basket.size() < 5)
                
{
                    
if(!Visited[b.top[0]][b.top[1]][b.top[2]][b.top[3]])
                    
{
                        Visited[b.top[
0]][b.top[1]][b.top[2]][b.top[3]] = true;
                        Q.push(b);
                    }

                }

            }

        }

    }

}

int main()
{
    
while(scanf("%d"&n) && n)
    
{
        
for(int i = 0; i < n; i++)
            
for(int j = 0; j < 4; j++)
                scanf(
"%d"&candies[j][i]);
        ans 
= 0;

        bfs();
        printf(
"%d\n", ans);
    }

    
return 0;
}

 

将set改为vector后,其效率会提高很多.

你可能感兴趣的:(free)