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,然后往全拿光推···样例过不去···后来觉得推反了 ...
随机推荐
- codeforces #313 div1 B
模拟判定就可以了 判定字符串是否相等用hash来判断 QAQ 值得一提的是一开始我交的时候T了 结果我将递归的顺序调整了一下就A了 (并不知道为什么 #include<cstdio> #i ...
- spring 异常管理机制
三.异常处理的几种实现: 3.1.在经典的三层架构模型中,通常都是这样来进行异常处理的: A.持久层一般抛出的是RuntiomeException类型的异常,一般不处理,直接向上抛出. B.业务层一般 ...
- javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- 使用intellij idea搭建MAVEN+springmvc+mybatis框架
原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- git同步远端的分支
如果用命令行,运行 git fetch,可以将远程分支信息获取到本地, 再运行 git checkout -b local-branchname origin/remote_branchname 就 ...
- C语言一维数组中的数据随机排列
#include <stdio.h>#include <stdlib.h> void randomlize(int *a, int n){ int i = 0,j ...
- Android开发之全屏显示的两种方法
1.通过修改清单文件中Theme,实现全屏 <application android:name=".MyApplication" android:allowBackup=&q ...
- mysql运算符的优先级
Operator precedences are shown in the following list, from highest precedence to the lowest. Operato ...
- 学习面试题Day09
一.Java基础部分 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相 ...