• Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟


    Input

    Input data to the program specifies the order in which cards are dealt from the pack. The input contains 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 Output

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

    麻烦的模拟。手写栈,每个pile都是个栈,用pos数组来记录位置和pile编号的关系。

    #include<cstdio>
    
    struct card
    {
       char R, s;//Rank,suit
       card(){}
       card(char R, char s):R(R),s(s){}
       bool operator == (const card& rhs) const {// match
          return R == rhs.R || s == rhs.s;
       }
    };
    
    struct Stack
    {
       card ele[53];
       int Top;
    
       card* top(){ return ele+Top;}
       void pop(){Top--;}
       void push(const card& x){
          ele[++Top]  = x;
       }
    }pile[52];
    
    int pileSz;
    
    int pos[52];//pos 2 pile
    
    void Move(int p, int d)
    {
       int id = pos[p], pid = pos[p-d];
    
       pile[pid].push(*pile[id].top());
       pile[id].pop();
    
       if(pile[id].Top == 0){
          pileSz--;
          for(int i = p; i < pileSz; i++)
             pos[i] = pos[i+1];
       }
    }
    
    bool check()
    {
       int move3 = -1;
       for(int i = 3;i < pileSz; i++) {
          if( *pile[pos[i]].top() == *pile[pos[i-3]].top()) { move3 = i; break; }
       }
       int move1 = -1;
       for(int i = 1;i < pileSz; i++) {
          if( *pile[pos[i]].top() == *pile[pos[i-1]].top()) { move1 = i; break; }
       }
    
       if(!~move1 && !~move3) return false;
    
       if(~move1&&~move3){
          if(move1 < move3) Move(move1,1);
          else Move(move3,3);
       }
       else {
          if(~move1) Move(move1,1);
          else Move(move3,3);
       }
    
       return true;
    }
    
    bool read(){
       char buf[3];
       scanf("%s",buf);
       if(*buf == '#') return false;
       for(int i = 0; i < 52; i++) pos[i] = i;
       pileSz = 52;
       for(int i = 0; i < 52; i++) pile[i].Top = 0;
    
       pile[0].push(card(*buf,buf[1]));
    
       for(int i = 1; i < 52; i++) {
          scanf("%s",buf);
          pile[i].push(card(*buf,buf[1]));
       }
    
    
    
       return true;
    }
    
    int main()
    {
     //  freopen("in.txt","r",stdin);
       while(read()){
          while(check());
       /* poj
          printf("%d piles remaining:",pileSz);
          for(int i = 0; i < pileSz; i++)
          printf(" %d",pile[pos[i]].Top);
       */
          if(pileSz>1){
             printf("%d piles remaining:",pileSz);
             for(int i = 0; i < pileSz; i++)
             printf(" %d",pile[pos[i]].Top);
          }
          else printf("1 pile remaining: 52");
    
          puts("");
       }
       return 0;
    }
     
  • 相关阅读:
    简单爬虫爬取知乎日报并保存日报网页到本地
    从0开始学爬虫6比价工具开发2之图书信息汇总
    从0开始学爬虫6比价工具开发1之爬取当当、京东的数据
    从0开始学爬虫5之优雅的使用字符串
    从0开始学爬虫4之requests基础知识
    从0开始学爬虫3之xpath的介绍和使用
    从0开始学爬虫2之json的介绍和使用
    从0开始学爬虫1之环境搭建篇
    jenkins结合supervisor进行python程序发布后的自动重启
    ansible常用的方法小结
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4619474.html
Copyright © 2020-2023  润新知