zoukankan      html  css  js  c++  java
  • UVA127-"Accordian" Patience(模拟)

    Problem UVA127-"Accordian" Patience

    Accept:3260  Submit:16060

    Time Limit: 3000 mSec

    Problem Description

    You are to simulate the playing of games of “Accordian” patience, the rules for which are as follows: Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile. Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.

     Input

    Inputdatatotheprogramspecifiestheorderinwhichcardsaredealtfromthepack. Theinputcontains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a ‘#’ as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2–9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).

     Output

    One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing “Accordian patience” with the pack of cards as described by the corresponding pairs of input lines.

     Sample Input

    QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
    8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
    AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
    AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
    #
     
     

     Sample Ouput

    6 piles remaining: 40 8 1 1 1 1
    1 pile remaining: 52

    题解:纯模拟,有删除、移动、堆牌的操作,因此用STL比较好,这里虽然有删除操作,但是还需要能够便捷地访问下标,权衡之后用vector。

    因为要堆牌,所以嵌套一个stack。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <stack>
     7 using namespace std;
     8 
     9 const int kind = 52;
    10 
    11 struct Card{
    12     int Suit,Rank;
    13     Card(int Suit = 0,int Rank = 0) :
    14         Suit(Suit),Rank(Rank) {}
    15 };
    16 
    17 char q[] = {'C','D','H','S'};
    18 
    19 void change(const char *ss,int &a,int &b){
    20     if(isdigit(ss[0])) a = ss[0]-'0';
    21     else if(ss[0] == 'A') a = 1;
    22     else if(ss[0] == 'T') a = 10;
    23     else if(ss[0] == 'J') a = 11;
    24     else if(ss[0] == 'Q') a = 12;
    25     else if(ss[0] == 'K') a = 13;
    26     b = strchr(q,ss[1])-q;
    27 }
    28 
    29 bool Bingo(const Card a,const Card b){
    30     if(a.Rank==b.Rank || a.Suit==b.Suit) return true;
    31     return false;
    32 }
    33 
    34 int cnt = 1;
    35 
    36 int main()
    37 {
    38     //freopen("input.txt","r",stdin);
    39     char str[10];
    40     while(true){
    41         vector< stack<Card> > pile;
    42         scanf("%s",str);
    43         if(str[0] == '#') break;
    44         Card first;
    45         change(str,first.Rank,first.Suit);
    46         stack<Card> First;
    47         First.push(first);
    48         pile.push_back(First);
    49         for(int i = 2;i <= kind;i++){
    50             Card temp;
    51             stack<Card> Temp;
    52             scanf("%s",str);
    53             change(str,temp.Rank,temp.Suit);
    54             //printf("Rank:%d Suit:%d
    ",temp.Rank,temp.Suit);
    55             Temp.push(temp);
    56             pile.push_back(Temp);
    57         }
    58         while(true){
    59             bool Can_move = false;
    60             for(int i = 0;i < pile.size();i++){
    61                 //printf("%d %d
    ",pile[i].top().Rank,pile[i].top().Suit);
    62                 int j = i-3,k = i;
    63                 if(0 <= j){
    64                     Card now = pile[k].top(),pre = pile[j].top();
    65                     if(Bingo(now,pre)){
    66                         pile[k].pop();
    67                         pile[j].push(now);
    68                         if(pile[k].empty()) pile.erase(pile.begin()+k,pile.begin()+k+1);
    69                         k = j;
    70                         Can_move = true;
    71                         break;
    72                     }
    73                 }
    74                 j = i-1;
    75                 if(0<=j){
    76                     Card now = pile[k].top(),pre = pile[j].top();
    77                     if(Bingo(now,pre)){
    78                         pile[k].pop();
    79                         pile[j].push(now);
    80                         if(pile[k].empty()) pile.erase(pile.begin()+k,pile.begin()+k+1);
    81                         k = j;
    82                         Can_move = true;
    83                         break;
    84                     }
    85                 }
    86             }
    87             if(!Can_move) break;
    88         }
    89         if(pile.size() == 1) printf("%d pile remaining:",pile.size());
    90         else printf("%d piles remaining:",pile.size());
    91         for(int i = 0;i < pile.size();i++){
    92             printf(" %d",pile[i].size());
    93         }
    94         printf("
    ");
    95     }
    96     return 0;
    97 }
  • 相关阅读:
    小程序canvas生成海报-新旧接口
    vue网页小程序实现七牛云图片文件上传以及原生组件video显示不出问题
    【文化课】 一篇魔改英语理解
    python萌新笔记
    版本控制(Version control)
    开源许可证(License)
    agc004c
    python日期时间、时间戳互相转换
    拓展django-haystack全文检索的样式和搜索频率限制
    常用JS代码
  • 原文地址:https://www.cnblogs.com/npugen/p/9531575.html
Copyright © 2011-2022 走看看