hpu 1454: Connecting bamboo poles (the use of c++ queue)

question:

A person played with a bamboo pole.

The rule is that when a person plays a card with the same number in the cards above, he can take those two cards and the cards between the two cards, and then play a poker.

The left hand plays the cards first , and the winning cards are played in the previous order.

For example : there are two cards left in the left hand, 1 and 2, and the previous move was 2, 3, 4, 2, and his order of cards is 1, 2, 2, 3, 4, 2.

When one person has no cards in his hand, the other person wins.

enter:

The first line is a number T, played T times

Every time the game starts with a number n, only numbers <= n of poker are used,

There are two lines of numbers behind, each line has 2*n numbers,

The first row represents left-handed poker.

The second row represents right hand poker.

1 <= T <= 10

1 <= n <= 13

output:

For each game, output the result of the game,

(The left and right hands accumulate 100,000 cards, regardless of whether they win or lose)

Each game output occupies one line,

Left hand wins and outputs 1,

The right hand wins and outputs 2,

A draw outputs 0

analyze:

The left-hand and right-hand cards can be recorded in a queue respectively, which is convenient for deleting from the front, inserting from the back, and judging whether it is empty.

The cards played can be recorded in an array.

If you don't understand the definition of queue, you can take a look.

"A Preliminary Study of Queue" written by me

Code:

#include<queue>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std; //Left-hand first play
intmain()
{
    int T;
    cin>>T; //input T
    while (T--)
        {
        int n,a; //Use poker cards with points <=n, each hand has 2n cards

        int ping[20]; // This array is used to record the cards that have been played by the left and right hands

        int lp,step;
        cin>>n; //input n
        queue<int> left, right; //Define left and right queues.

        for (int i=0;i<2*n;i++) // 2n cards from left hand
        {
            cin>>a;
            left.push(a); // Insert elements into the queue from the back
        }


        for (int i=0;i<2*n;i++) // 2n cards in the right hand
        {
            cin>>a;
            right.push(a); // insert element into the queue from the back
        }
 
        step = 0; //Record the number of cards played
        lp = 0; //lp is used to record the serial number of the card that has been played

        int man=1; // man is a variable used to help determine whether the card is left-handed or right-handed. .

        int ans = 0 ; // variable to judge winning or losing, 0 for draw, 1 for left win, 2 for right win

        while (step<100000)
        {
            if (man%2==1)
            { //Left hand play first
                while (true)
                {
                    step++;
                    ping[lp++] = left.front(); //Record the cards played by the left hand
                    left.pop(); //The left hand card deletes the top one
                    bool fafe = true;               //
                    for (int i=0;i<lp-1;i++)
                    {
                        if (ping[i]==ping[lp-1]) //ping[lp-1] is the card just played. Start to judge whether the card played by the left hand is the same as the previous one
                        {
                            fafe = false;
                            for (int j=i;j<lp;j++) //If there are the same, add the middle cards to the back of the left team.
                                left.push(ping[j]);

                            lp = i; // Since it is taken from the i card, the sequence number of the next deal is the i th card.
                            break;
                        }
                    }
                    if (fafe)
                        break; // if there is the same, break out of the loop
                }
                if (left.empty()) // if left is empty
                {
                    ans = 2; // win right
                    break;
                }
                man^=1; // ^ XOR operation, the same is 0, the difference is 1.. . To be able to get to this point means that there is no such thing as the same. man becomes 0
            } // Then the next loop starts from the right hand


            else //Right hand (same as left hand)
            {
                 while (true)
                {
                    step++;
                    ping[lp++] = right.front();
                    right.pop();
                    bool fafe = true;
                    for (int i=0;i<lp-1;i++)
                    {
                        if (ping[i]==ping[lp-1]){
                            fafe = false;
                            for (int j=i;j<lp;j++)
                                right.push(ping[j]);
                            lp = i;
                            break;
                        }
                    }
                    if (fafe) break;
                }
                if (right.empty()) // If the right hand is empty, the left wins.
                {
                    years = 1;
                    break;
                }
                man^=1; //Being able to get to this point means that there is no same. man becomes 1. Then the next cycle starts from the left hand
        }
    }
        if (step>=100000)
        {
            ans = 0; // 100,000 plays without a winner, it's a draw
            cout<<ans<<endl;
        }
        else
        {
            cout<<ans<<endl;
        }
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483592&siteId=291194637