您的位置:首页 > 其它

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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: