Sicily 1003. Hit or Miss

1003. Hit or MissConstraints

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 Input24 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 134 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 1Sample OutputCase 1: 13 13 13 13Case 2: unwinnable

凶残的模拟题,,胜过阅读理解,代码如下:

#include <iostream>#include <queue>#include <string.h>using namespace std;#define MAX_TIMES 10000//最大的轮数,这个只能猜了int main() {int case_num, i, j, player_num;cin >> case_num;for (j = 1; j <= case_num; j++) {cin >> player_num;queue<int> deck[player_num];//用队列来模拟int temp;for (i = 0; i < 52; i++) {cin >> temp;deck[0].push(temp);//刚开始只有第一个人受伤有牌}int last_deck[player_num];//用来记录每个人拥有的最后一张牌memset(last_deck, 0, sizeof(last_deck));int count[player_num];//用来给每个人计数memset(count, 0, sizeof(count));int times = 0;//轮数int discard = 0;//游戏中最后一个人抛弃的牌数bool is_ok = false;while (times <= MAX_TIMES && !is_ok) {for (i = 0; i < player_num && !is_ok; i++) {if (deck[i].empty())continue;count[i] = count[i] == 13 ? 1 : count[i] + 1;temp = deck[i].front();deck[i].pop();//无论能否匹配,第一张牌总是要改变位置的if (temp == count[i]) {//匹配了if (deck[i].empty()) {//如果这个人手上拿走了这张牌之后没有牌,记录这张牌为最后牌last_deck[i] = temp;}if (i == player_num – 1) {discard++;//最后一个人抛弃的牌数if (discard == 52) {//牌都抛弃完了is_ok = true;}} else {deck[i + 1].push(temp);//给下一个人牌的最底}} else {deck[i].push(temp);//给自己最底}}times++;}cout << "Case " << j << ":";if (is_ok) {for (i = 0; i < player_num; i++) {cout << " " << last_deck[i];}} else {cout << " unwinnable";}cout << endl;}return 0;}

要么读书、要么旅行,灵魂和身体,必须有一个在路上。

Sicily 1003. Hit or Miss

相关文章:

你感兴趣的文章:

标签云: