UVa 127 - "Accordian" Patience
题目:52张扑克,从左到右在平面上排列,按着如下规则处理:
1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面。
2.如果可以移动到多个位置,移动到最左端的牌上面。(匹配:花色或者数值相同)
分析:数据结构、栈、模拟。对于每叠牌建立一个栈,进行模拟即可。
注意:每次只移动每叠牌的最顶上的牌。
#include <iostream>
#include <cstdlib>
#include <cstdio> using namespace std; char Card[54][54][3];
int Top[54];
int Sum;//记录合并的叠数 int match( char* a, char *b )
{
return (a[0] == b[0] || a[1] == b[1]);
} int deal( int now, int s )
{
//判断是否能移动s步长
int count = 0,temp = now;
while ( temp >= 0 && count < s )
if ( Top[-- temp] >= 0 )
count ++;
//判断是否匹配
if ( temp >= 0 && match( Card[now][Top[now]], Card[temp][Top[temp]] ) ) {
Top[temp] ++;
Card[temp][Top[temp]][0] = Card[now][Top[now]][0];
Card[temp][Top[temp]][1] = Card[now][Top[now]][1];
if ( -- Top[now] < 0 ) Sum ++;
return temp;
}else return -1;
} int main()
{
while ( scanf("%s",Card[0][0]) && Card[0][0][0] != '#' ) {
for ( int i = 1 ; i < 52 ; ++ i )
scanf("%s",Card[i][0]);
for ( int i = 0 ; i < 52 ; ++ i )
Top[i] = 0; Sum = 0;
for ( int now = 1 ; now < 52 ; ) {
while ( Top[now] < 0 ) now ++;
//向左移动3步
int save = deal( now, 3 );
if ( save >= 0 )
now = save;
else {
//向左移动1步
save = deal( now, 1 );
if ( save >= 0 )
now = save;
else now ++;
}
} printf("%d pile",52-Sum);
if (51 > Sum) printf("s");
printf(" remaining:");
for ( int i = 0 ; i < 52 ; ++ i )
if ( Top[i] >= 0 )
printf(" %d",Top[i]+1);
printf("\n");
}
return 0;
}
UVa 127 - "Accordian" Patience的更多相关文章
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟
Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...
- ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- UVa 127 - "Accordian" Patience POJ 1214 链表题解
UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...
- 【习题 6-9 UVA - 127】"Accordian" Patience
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...
- UVa 1637 - Double Patience(概率DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 170 - Clock Patience
题目:Clock Patience游戏,将52张扑克牌,按时钟依次分成13组(中心一组),每组4张全都背面向上, 从中间组最上面一张牌開始.翻过来设为当前值,然后取当前值相应组中最上面的背过去的牌翻过 ...
- UVA 1637 Double Patience
题意:36张扑克,平分成9摞,两张数字一样的可以拿走,每次随机拿两张,问能拿光的概率. 解法:记忆化搜索,状态压缩.一开始我想在还没拿的时候概率是1,然后往全拿光推···样例过不去···后来觉得推反了 ...
随机推荐
- ArcGIS学习记录—ArcGIS ArcMap编辑状态中线打断的问题
摘要:在处理数据时,我们经常会遇到线打断的问题,比如需要指定在线上某处打断线,或者新建网络数据集时需要在线的交点处打段线等等.现将桌面版中我所遇到的线打断的工具总结如下: 在ArcGIS矢量处理数据时 ...
- Git教程之撤销修改(7)
自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行:
- 97. Interleaving String
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 如何在VS2010中使用Async功能?
伴随C#5.0的发布,“异步”特性越来越深入人心:在VS2012中早就可以使用它大大简化异步编程的痛苦,那么在VS2010中呢?我们无法尝鲜么?答案是“No”!,其实我们可以这样做: 1)必须把你的V ...
- hdu4631Sad Love Story(多校3)(最接近点对)
http://acm.hdu.edu.cn/showproblem.php?pid=4631 比赛的时候搜到了最接近点对的求法 Nlog(N) 又估摸着依次插入求的话会TLE 想了想觉得可以先把最近的 ...
- poj 1840 Eqs (hash)
题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...
- mvc源码解读(20)-controller和view之查找view
很多时候在mvc项目中我们需要去扩展自己的视图引擎,大概看起来应该下面这个样子的: public class RazorEngineExpand : RazorViewEngine { private ...
- [swustoj 856] Huge Tree
Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.6
If $\sen{A}<1$, then $I-A$ is invertible, and $$\bex (I-A)^{-1}=I+A+A^2+\cdots, \eex$$ aa converg ...
- 命令mv
mv 文件 目标目录如果目标目录改成文件名,mv命令可用于重命名文件.