ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

Description

 ``Accordian‘‘ Patience 

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

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

这个题目是一道栈模拟的题目,题目还可以,就是写的有点麻烦。

代码:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <algorithm>
  7 #include <set>
  8 #include <map>
  9 #include <queue>
 10 #include <string>
 11 #define inf 0x3fffffff
 12 #define esp 1e-10
 13 #define N 100005
 14
 15 using namespace std;
 16
 17 struct node
 18 {
 19     int top;
 20     char card[53][3];
 21 }s[52];
 22
 23 bool Input ()
 24 {
 25     scanf ("%s", s[0].card[0]);
 26     if (s[0].card[0][0] == ‘#‘)
 27         return 0;
 28     s[0].top = 1;
 29     for (int i = 1; i < 52; ++i)
 30     {
 31         s[i].top = 1;
 32         scanf ("%s", s[i].card[0]);
 33     }
 34     return 1;
 35 }
 36
 37 void Output ()
 38 {
 39     queue <int> q;
 40     for (int i = 0; i < 52; ++i)
 41         if (s[i].top != 0)
 42             q.push(i);
 43     int sum = q.size();
 44     if (sum == 1)
 45         printf ("1 pile remaining: %d\n", s[q.front()].top);
 46     else
 47     {
 48         printf ("%d piles remaining:", sum);
 49         int k;
 50         while (!q.empty())
 51         {
 52             k = q.front();
 53             q.pop();
 54             printf (" %d", s[k].top);
 55         }
 56         printf ("\n");
 57     }
 58 }
 59
 60 void qt ()
 61 {
 62     int p = 0;
 63     for (;;)
 64     {
 65         if (p == 52)
 66             break;
 67         if (s[p].top != 0)
 68         {
 69             int one = -1, two = -1;
 70             int flag = 0, j = p-1;
 71             for (;;)
 72             {
 73                 if (j < 0)
 74                     break;
 75                 if (s[j].top != 0)
 76                 {
 77                     flag++;
 78                     if (flag == 3)
 79                     {
 80                         one = j;
 81                         break;
 82                     }
 83                     if (flag == 1)
 84                         two = j;
 85                 }
 86                 j--;
 87             }
 88             if (one != -1)
 89             {
 90                 if (s[p].card[s[p].top-1][0] == s[one].card[s[one].top-1][0] ||
 91                     s[p].card[s[p].top-1][1] == s[one].card[s[one].top-1][1])
 92                 {
 93                     strcpy (s[one].card[s[one].top], s[p].card[s[p].top-1]);
 94                     s[p].top--;
 95                     s[one].top++;
 96                     p = 0;
 97                     continue;
 98                 }
 99             }
100             if (two != -1)
101             {
102                 if (s[p].card[s[p].top-1][0] == s[two].card[s[two].top-1][0] ||
103                     s[p].card[s[p].top-1][1] == s[two].card[s[two].top-1][1])
104                 {
105                     strcpy (s[two].card[s[two].top], s[p].card[s[p].top-1]);
106                     s[p].top--;
107                     s[two].top++;
108                     p = 0;
109                     continue;
110                 }
111             }
112         }
113         ++p;
114     }
115 }
116
117 int main()
118 {
119     //freopen ("test.txt", "r", stdin);
120     while (Input ())
121     {
122         qt ();
123         Output ();
124     }
125     return 0;
126 }

				
时间: 2024-10-10 21:55:10

ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)的相关文章

uva 127 &quot;Accordian&quot; Patience(手风琴纸牌)

用 栈 stack 来处理. 直接根据题目描述写就可以.不要忘记每组数据最后的清空栈堆. 题目大意: 给定52张的扑克牌,现在要求对扑克牌进行整理,对于每一张扑克牌,如果左边的第三张存在那么就去判断这一张是否和第三张满足花色或卡片值相同,如果满足则把这一张移动到左边的第三张,否则去判断左边的第一张是否和这一张满足条件:如果左边的第三张不存在那么只要去判断左边的第一张即可.最后输出剩下的扑克牌的堆数,并且输出每一堆的牌数. #include<stdio.h> #include<iostre

UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比较简单,这里纯粹使用指针做了,非常麻烦的指针操作,一不小心就错.调试起来还是非常费力的 本题理解起来也是挺费力的,要搞清楚如何模拟也不容易啊,读题要很仔细. 纯指针的操作挺快的吧.不过POJ 0ms,而UVa就0.2左右了. 三相链表: 1 只要有叠起来的牌,那么就使用一个down指针指向下面的牌就可以了. 2 使用双向链表,可以方便前后遍历. 3 记得有了更新牌之后,又要重新开始检查是否需要更新牌,这是

ACM学习历程——UVA11111 Generalized Matrioshkas(栈)

Description Problem B - Generalized Matrioshkas   Problem B - Generalized Matrioshkas  Vladimir worked for years making matrioshkas, those nesting dolls that certainly represent truly Russian craft. A matrioshka is a doll that may be opened in two ha

UVA 127 &quot;Accordian&quot; Patience

题意: 按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠.游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面.如果牌被移动后又出现了上述情况,则需再次向左移动.每叠牌只能移动最上面的一张.如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙.依次将整副牌都发完,并不断的向左合并.如果全部移动结束后整副牌都放成了一叠,则游戏胜利. 分析: 用sum表示有多少个牌挪动了,最后输出52-sum.判断是否能放到左边第三张的时候需要注意再前

UVa OJ 127 - &quot;Accordian&quot; Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience ("手风琴"纸牌) Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows: 模拟玩一个"手风琴"纸牌游戏,规则如下: D

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程——UVA127 &quot;Accordian&quot; Patience(栈, 链表)

Description  ``Accordian'' Patience  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 n