sicily 1003. Hit or Miss

1003. Hit or Miss

Constraints

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

题目分析

又臭又长的题目
a)第一个玩家拥有全部的52张牌,而其他的玩家暂时没有牌在手上
b)每一个玩家各自报数,若报的数与牌堆顶上的牌数字相同即为match
c)每个玩家各自报数,各自匹配;
也即是说,A报一个数并记住,比较牌堆顶上的牌,
接着是B报自己的数,比较自己牌堆顶上的牌,以此下去
d)报数均从1开始,若没有牌在手上则不报数,
待至有牌在手上时将上次所报之数加一作为此次的报数
e)若某个玩家(非最后一个玩家)匹配了一次,也就是说报得数跟牌堆上的数字一样时,
将此牌放入下一个玩家的牌堆的最底层;  
如果是最后一个玩家,则丢弃该张牌。  
若玩家不匹配,也就是说报得数跟牌堆上的数字不一样时,将该牌放入自己牌堆的最底层
f)记录由最后一个玩家丢弃了的牌数,到达52即结束;
g)如果循环到一定次数游戏还没有结束,就可认定无法胜出,跳出。


#include <iostream>
#include <deque>
#include <memory.h>
#include <cstdio>

int main()
{
  int test;
  std::cin >> test;
  for (int i = 1; i <= test; ++i) {
    int num;
    std::cin >> num; //the total of players
    std::deque <int> players[num]; //the cards of the player
    int key;
    for (int c = 0; c < 52; ++c) {
      std::cin >> key;
      players[0].push_back(key);
    }  
    int count[num], last[num];
    memset(count, 0, sizeof(count));
    int flag = 0; 
    int times = 20000;
    while (times) {
      for (int c = 0; c < num; ++c) {
        if (flag == 52)
          break;
        if (!players[c].empty()) {
          count[c]++;
          if (count[c] == 14)
            count[c] = 1;
          last[c] = players[c].at(0);
          players[c].pop_front();
          if (count[c] == last[c]) {
            if (c != num - 1)           
              players[c + 1].push_back(last[c]);
            else 
              flag++;
          } else {  
            players[c].push_back(last[c]);                         
          }
        }
        --times;
      }
      
      if (flag == 52)
        break;
    }
    bool judge = true;
    for (int c = 0; c < num; ++c)
      if (!players[c].empty()) {
        judge = false;
        break;                         
      }
    printf("Case %d:", i);
    if (judge) {
      for (int c = 0; c < num; ++c)
        printf(" %d", last[c]);         
    } else {
      printf(" unwinnable");       
    }
    printf("\n");   
  }   
  //std::cin >> test; 
  return 0;   
}                                 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值