Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟
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 麻烦的模拟。手写栈,每个pile都是个栈,用pos数组来记录位置和pile编号的关系。
#include<cstdio> struct card
{
char R, s;//Rank,suit
card(){}
card(char R, char s):R(R),s(s){}
bool operator == (const card& rhs) const {// match
return R == rhs.R || s == rhs.s;
}
}; struct Stack
{
card ele[];
int Top; card* top(){ return ele+Top;}
void pop(){Top--;}
void push(const card& x){
ele[++Top] = x;
}
}pile[]; int pileSz; int pos[];//pos 2 pile void Move(int p, int d)
{
int id = pos[p], pid = pos[p-d]; pile[pid].push(*pile[id].top());
pile[id].pop(); if(pile[id].Top == ){
pileSz--;
for(int i = p; i < pileSz; i++)
pos[i] = pos[i+];
}
} bool check()
{
int move3 = -;
for(int i = ;i < pileSz; i++) {
if( *pile[pos[i]].top() == *pile[pos[i-]].top()) { move3 = i; break; }
}
int move1 = -;
for(int i = ;i < pileSz; i++) {
if( *pile[pos[i]].top() == *pile[pos[i-]].top()) { move1 = i; break; }
} if(!~move1 && !~move3) return false; if(~move1&&~move3){
if(move1 < move3) Move(move1,);
else Move(move3,);
}
else {
if(~move1) Move(move1,);
else Move(move3,);
} return true;
} bool read(){
char buf[];
scanf("%s",buf);
if(*buf == '#') return false;
for(int i = ; i < ; i++) pos[i] = i;
pileSz = ;
for(int i = ; i < ; i++) pile[i].Top = ; pile[].push(card(*buf,buf[])); for(int i = ; i < ; i++) {
scanf("%s",buf);
pile[i].push(card(*buf,buf[]));
} return true;
} int main()
{
// freopen("in.txt","r",stdin);
while(read()){
while(check());
/* poj
printf("%d piles remaining:",pileSz);
for(int i = 0; i < pileSz; i++)
printf(" %d",pile[pos[i]].Top);
*/
if(pileSz>){
printf("%d piles remaining:",pileSz);
for(int i = ; i < pileSz; i++)
printf(" %d",pile[pos[i]].Top);
}
else printf("1 pile remaining: 52"); puts("");
}
return ;
}
Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟的更多相关文章
- ACM学习历程——UVA 127 "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做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...
- HDU 2209 翻纸牌游戏 状态BFS
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem De ...
- hdu2209翻纸牌游戏
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 翻纸牌游戏(dfs回溯)
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- HDU 2209 翻纸牌游戏(DFS)
题目链接 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦 ...
- HDU 2209 翻纸牌游戏
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
随机推荐
- 转: Charles 从入门到精通
目录与版权 转载请保留顶部的 Charles 中国特惠内容,本文的内容主要包括: Charles 的简介 如何安装 Charles 将 Charles 设置成系统代理 Charles 主界面介绍 过滤 ...
- CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction Ivan had string s consisting of small English letters. However, his friend Jul ...
- day01-HTML(1)
一. 常用快捷键 Ctrl+c 复制 Ctrl+v 粘贴 Ctrl+x 剪切 Ctrl+a 全选 Ctrl+s 保存 Ctrl+z 撤销一步 Windows+d 返回桌面 Windows+e 我的电脑 ...
- Javascript 返回上一页:选中GridVIew的 Chekcbox
1. 选中GridVIew的值 $("#reverse").click(function () { //$("#checkbox[Num]").attr(&q ...
- Python包管理工具小结
此文已由作者张耕源授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为一名接触Python有一段时间的初学者,越来越体会到Python的方便之处,它使人能更 多的关注业务本身 ...
- 使用Unity实现动态2D水效果
http://forum.china.unity3d.com/thread-16044-1-1.html 在这片教程里面我们将会用简单的物理效果来模拟动态的2D水效果.我们将会使用Line Rende ...
- HTML5math标签
HTML5 MathML 一.HTML5 可以在文档中使用 MathML 元素,对应的标签是 <math>...</math> . MathML 是数学标记语言,是一种基于XM ...
- python——字符编码
Unicode 是字符集 UTF-8 是编码规则 Unicode:给每一个字符分配一个唯一的ID(又称码位). 编码规则:将码位转换为字节序列的规则. 1.什么是字符编码:字符翻译成数字,所遵循的标准 ...
- PAT甲级——1107 Social Clusters (并查集)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90409731 1107 Social Clusters (30 ...
- Leetcode:单调数列
题目 如果数组是单调递增或单调递减的,那么它是单调的. 如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的. 如果对于所有 i <= j,A[i]> ...