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,然后往全拿光推···样例过不去···后来觉得推反了 ...
随机推荐
- 李洪强iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测) 一.重力行为 说明:给定重力方向.加速度,让物体朝着重力方向掉落 1.方法 (1)UIGravityBehavior的初始化 - (inst ...
- 安装 ArcGIS Runtime SDK for Android
ArcGIS for Android 开发:Android 平台搭建 - liyong20080101的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/liyong ...
- Android TextView结合SpannableString使用
super.onCreate(savedInstanceState); TextView txtInfo = new TextView(this); SpannableString ss = new ...
- TSharding:用于蘑菇街交易平台的分库分表组件
tsharding TSharding is the simple sharding component used in mogujie trade platform. 分库分表业界方案 分库分表TS ...
- 新版本的tlplayer for android ,TigerLeapMC for windows发布了
tlplayer for android 新版本修正了图像倾斜等等问题,增加了动态水印功能. 支持hls(m3u8),http,rtsp,mms,rtmp等网络协议. 声明tlplayer 上的变速不 ...
- git同步远端的分支
如果用命令行,运行 git fetch,可以将远程分支信息获取到本地, 再运行 git checkout -b local-branchname origin/remote_branchname 就 ...
- js正则表达式中的特殊字符
正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个 ...
- poj 1062 昂贵的聘礼(最短路 dijk+枚举)
终于A 了,这题做着真麻烦 题目:http://poj.org/problem?id=1062 dijk 一般用于正权有向图 此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等 ...
- bzoj1854
神奇了 朴素的做法不难想,二分图最大匹配(汗,我其实还是想了一会,太弱了) 左边点集为能打的属性值,右边把武器作为一个点 武器和两个属性连边, 然后和superhero那题差不多,顺次找匹配,找不到了 ...
- poj1180
斜率优化dp 据说这题朴素的O(n2)dp也可以A 没试过 朴素的dp不难想:f[i]=min(f[j]+sumtime[i]*sumcost[j+1,i]+c*sumcost[j+1,n]) 稍微解 ...