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 这个题目是一道栈模拟的题目,题目还可以,就是写的有点麻烦。 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 100005 using namespace std; struct node
{
int top;
char card[][];
}s[]; bool Input ()
{
scanf ("%s", s[].card[]);
if (s[].card[][] == '#')
return ;
s[].top = ;
for (int i = ; i < ; ++i)
{
s[i].top = ;
scanf ("%s", s[i].card[]);
}
return ;
} void Output ()
{
queue <int> q;
for (int i = ; i < ; ++i)
if (s[i].top != )
q.push(i);
int sum = q.size();
if (sum == )
printf ("1 pile remaining: %d\n", s[q.front()].top);
else
{
printf ("%d piles remaining:", sum);
int k;
while (!q.empty())
{
k = q.front();
q.pop();
printf (" %d", s[k].top);
}
printf ("\n");
}
} void qt ()
{
int p = ;
for (;;)
{
if (p == )
break;
if (s[p].top != )
{
int one = -, two = -;
int flag = , j = p-;
for (;;)
{
if (j < )
break;
if (s[j].top != )
{
flag++;
if (flag == )
{
one = j;
break;
}
if (flag == )
two = j;
}
j--;
}
if (one != -)
{
if (s[p].card[s[p].top-][] == s[one].card[s[one].top-][] ||
s[p].card[s[p].top-][] == s[one].card[s[one].top-][])
{
strcpy (s[one].card[s[one].top], s[p].card[s[p].top-]);
s[p].top--;
s[one].top++;
p = ;
continue;
}
}
if (two != -)
{
if (s[p].card[s[p].top-][] == s[two].card[s[two].top-][] ||
s[p].card[s[p].top-][] == s[two].card[s[two].top-][])
{
strcpy (s[two].card[s[two].top], s[p].card[s[p].top-]);
s[p].top--;
s[two].top++;
p = ;
continue;
}
}
}
++p;
}
} int main()
{
//freopen ("test.txt", "r", stdin);
while (Input ())
{
qt ();
Output ();
}
return ;
}
ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)的更多相关文章
- UVa 127 - "Accordian" Patience
题目:52张扑克,从左到右在平面上排列,按着如下规则处理: 1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面. 2.如果可以移动到多个位置,移动到最左端的牌上面. ...
- ACM学习历程——UVA11111 Generalized Matrioshkas(栈)
Description Problem B - Generalized Matrioshkas Problem B - Generalized Matrioshkas Vladimir wo ...
- ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)
Description Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest Unive ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
随机推荐
- java面向对象编程知识点总结
一:今天完成 上午详细了解了java面向对象编程的一些细节,记录如下. 1)类 是一种引用类型,包含一个签名和一个主体,主体是放在花括号里面的成员,成员包括字段和方法,还有构造方法.初始化程序和嵌套类 ...
- 1065. [Nescafe19] 绿豆蛙的归宿(概率)
1065. [Nescafe19] 绿豆蛙的归宿 ★ 输入文件:ldfrog.in 输出文件:ldfrog.out 简单对比时间限制:1 s 内存限制:128 MB [背景] 随着新版 ...
- ASP.NET MVC + ADO.NET EF 项目实战(一):应用程序布局设计
什么叫上下文? 在你设计一个方法的时候,无法直接从方法参数或实例成员(字段或属性)获得的所有信息都是上下文.例如: 当前用户是谁? 刚才提供操作的数据库连接实例从哪里拿到? 这个方法从哪个 View ...
- 记录-Maven下载jar包失败解决办法
maven从nexsu上面拉jar包,有时会因为网络问题导致下不了包,这时候文件夹内会个*lastUpdated.properties的文件,而这文件的存在会导致下次服务器不会去下载这个包,这时候要删 ...
- PHP开发环境搭建(转载)
转载自:http://blog.csdn.net/rosetta/article/details/53967215 前言 最近学了n种语言,学每种语言的套路无非就是先搭建一个开发环境,再找本书或者 ...
- HackerRank - beautiful-binary-string 【字符串】
题意 给出一个 N 位的 01 串 然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0) 然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 ...
- ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)
1.首先检测支付账户是否开通快捷支付服务,如果开通后,那么公钥是否上传(支付宝问题一定要找支付宝客服解决,找其他人没有用,支付宝客服可以帮你分析底层原因) 2.修改app配置文件:alipay_cal ...
- LintCode:链表操作(合并与反转)
描述: (1)翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null ********************** ...
- apache中配置php支持模块模式、cgi模式和fastcgi模式的实验
首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...
- EntityFramework 学习 一 Explicit Loading with DBContext
即使延迟加载不能使用,也可以通过明确的调用来延迟加载相关实体 使用DBEntryEntity来完成 using (var context = new SchoolDBEntities()) { //D ...