Sicily 1003. Hit or Miss

来源:互联网 发布:萝莉云软件解压密码 编辑:程序博客网 时间:2024/05/17 04:16

CSDN博客征程正式开始!


Sicily 1003题 Link Text

///

Time Limit: 1 secs, Memory Limit: 32 MB

Description

One very simple type of solitaire game known as “Hit or Miss” (also known as “Frustration,” “Harvest,” “Roll-Call,” “Talkative”, and “Treize”) is played as follows: take a standard deck of 52 playing cards four sets of cards numbered 1 through 13 (suits do not matter in this game) which have been shuffled and start counting through the deck 1, 2, 3, … , and so on. When your count reaches 13, start over at 1. Each time you count, look at the top card of the deck and do one of two things: if the number you count matches the value of the top card, discard it from the deck; if it does not match it, move that card to the bottom of the deck. You win the game if you are able to remove all cards from the deck (which may take a very long time). A version of this game can be devised for two or more players. The first player starts as before with a 52 card deck, while the other players have no cards initially. As the first player removes cards from her deck, she gives them to the second player, who then starts playing the same game, starting at count 1. When that player gets a match, he passes his card to the third player, and so on. The last player discards matches rather than passing them to player 1. All players who have cards to play with perform the following 2-step cycle of moves in lockstep: 1. Each player says his or her current count value and checks for a match. If there is no match, the top card is moved to the bottom of the deck; otherwise it is passed to the next player (or discarded if this is the last player). 2. Each player except the first takes a passed card (if there is one) and places it at the bottom of his or her deck. These rules are repeated over and over until either the game is won (all the cards are discarded by the last player) or an unwinnable position is reached. If any player ever runs out of cards, he waits until he is passed a card and resumes his count from where he left off. (e.g., if player 3 passes his last card on a count of 7, he waits until he receives a card from player 2 and resumes his count with 8 at the beginning of the next 2-step cycle).

Input

Input will consist of multiple input sets. The first line of the file will contain a single positive integer nindicating the number of input sets in the file. Each input set will be a single line containing 53 integers: the first integer will indicate the number of players in the game and the remaining 52 values will be the initial layout of the cards in the deck, topmost card first. These values will all lie in the range 1 … 13, and the number of players will lie in the range 1… 10.

Output

For each input set, output the input set number (as shown below, starting with 1) and either the phrase “unwinnable” or a list showing the last card discarded by each player. Use a single blank to separate all outputs.

Sample Input

2
4 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13
4 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1

Sample Output

Case 1: 13 13 13 13
Case 2: unwinnable


解题思路 : 本题题目比较长,不过大意还是比较好理解的,什么?不知道题目意思?本题模拟的游戏大意是玩家进行手牌和计数器的匹配,当发生匹配时丢弃手牌,如有多个玩家时将手牌传给下一玩家。最后一个玩家丢弃手牌而不是传给第一个玩家。当不匹配是则将手牌放入牌堆底部。继续游戏,直至所有手牌均被丢弃,游戏结束。

本题需要注意的一点是如果游戏无法结束,即进入死循环后,随着计数器的更新无法发生匹配,此时需要结束游戏并判定游戏失败;有很多种方法来判定游戏是否无法完成,一种方法是设定循环上限,如果游戏循环超过这一上线即判定游戏失败。另一种方法是对于进行到某些状态下的牌堆进行判定,看看其是否能够进行匹配。get?

#include <iostream>#include <vector>#include <queue>using namespace std;/************************************************************//*  N为测试数据数量                                         *//*  num为玩家数量                                           *//*  answer向量用于记录各个玩家最后一张手牌                  *//*  count为各个玩家计数器,将其大小数值分别初始化为num+1和0 *//*  cards为玩家手中的手牌,cards为什么大小为num + 2?       *//*  使用cards[num+1]记录丢弃的手牌                          *//************************************************************/int main(){    int N; cin >> N;                // N    for (int hk = 1; hk <= N; hk++)    {        int num; cin >> num;        // num        vector<int> answer, count;          answer.resize(num + 1, 0); // answer        count.resize(num + 1, 0);  // count        vector<queue<int> > cards; // cards        cards.resize(num + 2);          for (int i = 0; i < 52; i++) // 初始化cards        {            int x; cin >> x;            cards[1].push(x);        }        bool skyDrive = false; int deadbeef = 5000; // 什么?为什么是5000?        while (deadbeef--)      // 模拟游戏,超过循环限度后即认为游戏结束        {            for (int i = 1; i <= num; i++)              {                if (!cards[i].empty())  //  对于每个手牌玩家进行模拟操作                {                    count[i]++;                     if (count[i] == 14) // 更新计数器                        count[i] = 1;                    int temp = cards[i].front();                    cards[i].pop();                    if (count[i] == temp)                    {                        answer[i] = (temp); // 更新玩家的最后一张手牌                        cards[i + 1].push(temp);                    }                    else                        cards[i].push(temp);                }            }            if (cards[num + 1].size() == 52)    // 丢弃手牌数为52时,跳出循环            {                skyDrive = true;                break;            }               }        if (skyDrive)        {            cout << "Case " << hk << ": ";            for (int i = 1; i < answer.size() - 1; i++)                cout << answer[i] << " ";            cout << answer[answer.size() - 1] << endl; // 最后一个数输出无空格        }        else            cout << "Case " << hk << ": unwinnable" << endl;    }    return 0;}
2 0
原创粉丝点击