Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟
2015-07-03 18:57
246 查看
Input
Inputdatatotheprogramspecifiestheorderinwhichcardsaredealtfromthepack.Theinputcontainspairsoflines,eachlinecontaining26cardsseparatedbysinglespacecharacters.Thefinallineoftheinputfilecontainsa#asitsfirstcharacter.Cardsarerepresentedasatwocharactercode.Thefirstcharacteristheface-value(A=Ace,2-9,T=10,J=Jack,Q=Queen,K=King)andthesecondcharacteristhesuit(C=Clubs,D=Diamonds,H=Hearts,S=Spades).Output
Onelineofoutputmustbeproducedforeachpairoflines(thatbetweenthemdescribeapackof52cards)intheinput.Eachlineofoutputshowsthenumberofcardsineachofthepilesremainingafterplaying``Accordianpatience''withthepackofcardsasdescribedbythecorrespondingpairsofinputlines.SampleInput
QDAD8H5S3H5HTC4DJHKS6H8SJSACAS8D2HQSTS3SAH4HTHTD3C6S 8C7D4C4S7S9H7C5D2SKD2DQHJD6D9DJC2CKH3DQC6C9SKC7H9C5C AC2C3C4C5C6C7C8C9CTCJCQCKCAD2D3D4D5D6D7D8DTD9DJDQDKD AH2H3H4H5H6H7H8H9HKH6SQHTHAS2S3S4S5SJH7S8S9STSJSQSKS #
SampleOutput
6pilesremaining:4081111 1pileremaining:52 麻烦的模拟。手写栈,每个pile都是个栈,用pos数组来记录位置和pile编号的关系。
#include<cstdio> structcard { charR,s;//Rank,suit card(){} card(charR,chars):R(R),s(s){} booloperator==(constcard&rhs)const{//match returnR==rhs.R||s==rhs.s; } }; structStack { cardele[53]; intTop; card*top(){returnele+Top;} voidpop(){Top--;} voidpush(constcard&x){ ele[++Top]=x; } }pile[52]; intpileSz; intpos[52];//pos2pile voidMove(intp,intd) { intid=pos[p],pid=pos[p-d]; pile[pid].push(*pile[id].top()); pile[id].pop(); if(pile[id].Top==0){ pileSz--; for(inti=p;i<pileSz;i++) pos[i]=pos[i+1]; } } boolcheck() { intmove3=-1; for(inti=3;i<pileSz;i++){ if(*pile[pos[i]].top()==*pile[pos[i-3]].top()){move3=i;break;} } intmove1=-1; for(inti=1;i<pileSz;i++){ if(*pile[pos[i]].top()==*pile[pos[i-1]].top()){move1=i;break;} } if(!~move1&&!~move3)returnfalse; if(~move1&&~move3){ if(move1<move3)Move(move1,1); elseMove(move3,3); } else{ if(~move1)Move(move1,1); elseMove(move3,3); } returntrue; } boolread(){ charbuf[3]; scanf("%s",buf); if(*buf=='#')returnfalse; for(inti=0;i<52;i++)pos[i]=i; pileSz=52; for(inti=0;i<52;i++)pile[i].Top=0; pile[0].push(card(*buf,buf[1])); for(inti=1;i<52;i++){ scanf("%s",buf); pile[i].push(card(*buf,buf[1])); } returntrue; } intmain() { //freopen("in.txt","r",stdin); while(read()){ while(check()); /*poj printf("%dpilesremaining:",pileSz); for(inti=0;i<pileSz;i++) printf("%d",pile[pos[i]].Top); */ if(pileSz>1){ printf("%dpilesremaining:",pileSz); for(inti=0;i<pileSz;i++) printf("%d",pile[pos[i]].Top); } elseprintf("1pileremaining:52"); puts(""); } return0; }
相关文章推荐
- NYOJ 673 悟空的难题
- js控制不同的时间段显示不同的css样式的实例代码
- Clustered Index & Non Clustered Index(聚簇索引和非聚簇索引)
- 【剑指offer】面试题23:从上往下打印二叉树
- NYOJ 46 最少乘法次数
- CSS透明属性详解代码
- 【剑指offer】面试题22:栈的压入、弹出序列
- 设计模式之单例模式:懒汉&饿汉
- 快速排序的非递归实现
- Linux中三种SCSI target的介绍之LIO
- android反编译-smali语法
- JSP,EL,JSTL快速入门
- apktool 反编译 Input file was not found or was not readable.问题解决方案
- 如何为ios酷我音乐盒下载导出的音乐文件(使用Java程序设计)
- Implement strStr()
- Ngx_Lua使用分享
- 正确定义Objective-C常量
- CSS透明属性详解代码
- Python笔记:_mysql常用操作
- app store上传图片显示错误:未能创建 屏幕快照