首页 > ACM题库 > HDU-杭电 > Hdu 1638 Beggar My Neighbour[解题报告] C++
2013
12-21

Hdu 1638 Beggar My Neighbour[解题报告] C++

Beggar My Neighbour

问题描述 :

“Beggar My Neighbour'' (sometimes known as “Strip Jack Naked'') is a traditional card game, designed to help teach beginners something about cards and their values. A standard deck is shuffled and dealt face down to the two players, the first card to the non-dealer, the second to the dealer, and so on until each player has 26 cards. The dealer receives the last card. The non-dealer starts the game by playing the top card of her deck (the second last card dealt) face up on the table. The dealer then covers it by playing her top card face up. Play continues in this fashion until a “face'' card (Ace, King, Queen or Jack) is played. The next player must then “cover'' that card, by playing one card for a Jack, two for a Queen, three for a King and four for an Ace. If a face card is played at any stage during this sequence, play switches and the other player must cover that card. When this sequence has ended, the player who exposed the last face card takes the entire heap, placing it face down under her existing deck. She then starts the next round by playing one card face up as before, and play continues until one player cannot play when called upon to do so, because they have no more cards.

Write a program that will simulate playing this game. Remember that a standard deck (or pack) of cards contains 52 cards. These are divided into 4 suits–Spades (), Hearts(),Diamonds(), and Clubs ().Within each suit there are 13 cards–Ace (A), 2-9, Ten (T), Jack (J), Queen (Q) and King (K).

输入:

Input will consist of a series of decks of cards. Each deck will give the cards in order as they would be dealt (that is in the example deck below, the non-dealer would start the game by playing the H2). Decks will occupy 4 lines with 13 cards on each. The designation of each card will be the suit (S, H, D, C) followed by the rank (A, 2-9, T, J, Q, K). There will be exactly one space between cards. The file will be terminated by a line consisting of a single #.

输出:

Output will consist of a series of lines, one for each deck in the input. Each line will consist of the number of the winning player (1 is the dealer, 2 is the first to play) and the number of cards in the winner's hand (ignoring any on the stack), right justified in a field of width 3.

样例输入:

HA H3 H4 CA SK S5 C5 S6 C4 D5 H7 HJ HQ
D4 D7 SJ DT H6 S9 CT HK C8 C9 D6 CJ C6
S8 D8 C2 S2 S3 C7 H5 DJ S4 DQ DK D9 D3
H9 DA SA CK CQ C3 HT SQ H8 S7 ST H2 D2
#

样例输出:

1 44

題目描述:

這是一個老手介紹撲克牌給新手的小遊戲,發排依序為新手-老手-新手-老手 … 正面朝下。
然後由新手開始,每一回合由先手翻開第一張牌,而後手根據那張牌出牌,

如果前一個人出了 JACK, 而後面那個人要出 1 張牌
如果前一個人出了 QUEEN, 而後面那個人要出 2 張牌
如果前一個人出了 KING, 而後面那個人要出 3 張牌
如果前一個人出了 ACE, 而後面那個人要出 4 張牌

這麼講可能不好,如果後面那個人出牌個數達到後面寫的那個數字,則必須將桌上的牌收起,正面朝下疊到手牌之下。
如果他在翻的過程中翻到 JQKA 其中一個,則換另一個人按照那個牌繼續那個規則,此時桌上的牌會越來越多。

最後會有一個人手上沒牌則獲勝。

#include <stdio.h>
#include <queue>
using namespace std;
int trans(char c) {
    if(c >= '0' && c <= '9')
        return c-'0';
    if(c == 'A')    return 1;
    if(c == 'T')    return 10;
    if(c == 'J')    return 11;
    if(c == 'Q')    return 12;
    if(c == 'K')    return 13;
}
int main() {
    char s[50];
    int card[52];
    int i;
    while(true) {
        for(i = 0; i < 52; i++) {
            scanf("%s", s);
            if(s[0] == '#') return 0;
            card[i] = trans(s[1]);
        }
        queue<int> A, B;
        for(i = 51; i >= 0; i--) {
            if(i%2) A.push(card[i]);
            else    B.push(card[i]);
        }
        int turn = 1;//0 A, 1 B
        int judge = -1;
        queue<int> H;//heap
        while(true) {
            if(turn == 0 && A.empty()) {
                judge = 0;
                break;
            }
            if(turn == 1 && B.empty()) {
                judge = 1;
                break;
            }
            int CARD;
            if(turn == 0)
                CARD = A.front(), A.pop();
            else
                CARD = B.front(), B.pop();
            //printf("%d - %c %d %d\n", CARD, turn+'A', A.size(), B.size());
            H.push(CARD);
            turn = 1-turn;
            int ended = 1;
            while(CARD >= 11 || CARD == 1) {
                ended = 0;
                int paid;
                if(CARD == 1)   paid = 4;
                else if(CARD == 11)   paid = 1;
                else if(CARD == 12)   paid = 2;
                else if(CARD == 13)   paid = 3;
                for(i = 0; i < paid; i++) {
                    if(turn == 0 && A.empty()) {
                        judge = 0;
                        break;
                    }
                    if(turn == 1 && B.empty()) {
                        judge = 1;
                        break;
                    }
                    if(turn == 0)
                        CARD = A.front(), A.pop();
                    else
                        CARD = B.front(), B.pop();
                    H.push(CARD);
                    //printf("%d - %c\n", CARD, turn+'A');
                    if(CARD == 1 || CARD >= 11)//change
                        break;
                }
                if(judge >= 0)  break;
                turn = 1-turn;
            }
            if(judge >= 0)  break;
            if(ended == 0) {
                if(turn == 0) {
                    while(!H.empty()) {
                        A.push(H.front());
                        H.pop();
                    }
                } else {
                    while(!H.empty()) {
                        B.push(H.front());
                        H.pop();
                    }
                }
            }
        }
        printf("%d%3d\n", 2-judge, judge ? A.size() : B.size());
    }
    return 0;
}

 


  1. 在方法1里面:

    //遍历所有的边,计算入度
    for(int i=0; i<V; i++)
    {
    degree = 0;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  2. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。