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 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
随机推荐
- C#——传值参数(3)
上篇文章我与大家共同学习了 值参数——引用类型这次与大家共同学习 传值参数--引用类型,不创建新对象,只操作对象这是个思维导图:我们仍需记住:1.值参数创建变量的副本 2.对值参数的改变不会影响变量的 ...
- 19个很有用的 JavaScript库推荐
流行的 JavaScript 库有jQuery,MooTools,Prototype,Dojo和YUI等,这些 JavaScript 库功能丰富,加上它们众多的插件,几乎能实现任何你需要的功能 然而需 ...
- Linux+ant+jmeter+Jenkins接口持续集成自动化框架搭建
Linux下安装ant并配置环境变量 1.从http://ant.apache.org 上下载tar.gz版ant 2.复制到/usr下 3.tar -vxzf apache-ant-1.10.1-b ...
- axios 跨域配置
axios跨域设置 找到项目config文件夹下的index.js文件,将dev中的proxyTable项中添加配置 proxyTable: { '/api': { target: 'https:// ...
- 求n位水仙花数
求n位水仙花数 A.两个关键 1.n位水仙花数的范围是什么? n位水仙花数的范围是[10n-1,10n) 2.如何判断是否为水仙花数 核心操作: 2-1.如何得到每一位? A.核心思想 对得到的数进行 ...
- Centos 自动更新git
首先,要先配置好自己的Git,然后在某一处进行脚本的编写. 比如项目目录为:/home/project,那参考如下来进行 vim /home/project/automatic_git.sh #/bi ...
- 【手撸一个ORM】第四步、Expression(表达式目录树)扩展
到这里,Orm的基架已经搭起来了,接下来就是激动人心的部分,表达式目录树转Sql语句,SqlDataReader转数据实体等等,但是在这之前,我们需要扩展下表达式目录树的方法,以方便后面的相关操作. ...
- Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心
http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...
- 解决win10下python3和python2共存pip的问题
经过在查阅网友的各种经验,发现仍然解决不了问题,python2和python3在win10下的安装就不再演示了,直接在python的官网下载就好,我机器上使用的是python2.7.15和python ...
- 《springcloud 一》搭建注册中心,服务提供者,服务消费者
注册中心环境搭建 Maven依赖信息 <parent> <groupId>org.springframework.boot</groupId> <artifa ...