uva127 "Accordian" Patience poj1214 模拟

本文详细记录了一位程序员在解决UVA127和POJ1214 'Accordion' Patience问题时的经历。作者通过生成随机数据来找出运行错误,并描述了调试过程中的困难与收获。问题在于当只剩下一张牌时,POJ平台的输出要求是'1 pile'而不是'1piles',修复后成功通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***********************************************

这个题目是我感触特别深的了, 第一次刷uva的题。题目不难,但印象颇深。

不得不说,runtime error 这个问题经常会困扰我, 曾经大多数时候是束手无策,只能改代码试试

这次我尝试生成一定数量的随机数据了, 每次输出这组数据,于是终于找到RE数据。

对数据找错误各种地方printf,我的debug能力比较弱,调试就比较慢,希望以后能快一些吧

过程是痛苦的, 收获是丰富的, 虽然只是一次简单的尝试。

最后吐槽一下,代码交poj的时候居然wa了,我还想poj数据就是强,我到底哪错了

结果去discuss一看,尼玛 poj 把 那个只剩一堆牌输出 1 pile 改成了 1piles  无语了都,改了一下也过了

**********************************************/

You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows:

Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left,  it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.

Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.

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
 # include<cstdio>

using namespace std;

typedef struct node{
  int rank;
  char value[52];
  char suit[52];
  struct node * next, *pre;
} card;

void tra(card * head)
{
    card *p=head->next;
    while(p)
    {
        if(p->next)
        printf("%d ", p->rank);
        else
        printf("%d\n", p->rank);
        p=p->next;
    }
}

int match(card * p, card * head)
{
    if(p->pre!=head && p->pre->pre!=head &&p->pre->pre->pre!=head)
    {
        if(p->suit[p->rank]==p->pre->pre->pre->suit[p->pre->pre->pre->rank] || p->value[p->rank]==p->pre->pre->pre->value[p->pre->pre->pre->rank])
        return 3;
    }
    if(p->pre!=head)
    {
      if(p->suit[p->rank]==p->pre->suit[p->pre->rank] || p->value[p->rank]==p->pre->value[p->pre->rank])
        return 1;
    }
    return 0;
}

void operate(card * head)
{
    int k;
    card *p=head->next;
    while(p!=NULL)
    {
        k=match(p, head);
        if(k)
        {
            if(k==3)
            {
                if(p->rank==1)
                {
                  p->pre->pre->pre->value[++(p->pre->pre->pre->rank)]=p->value[p->rank];
                  p->pre->pre->pre->suit[p->pre->pre->pre->rank]=p->suit[p->rank];
                  p->pre->next=p->next;
                  if(p->next)
                  p->next->pre=p->pre;
                  card *q=p;
                  p=p->pre->pre->pre;
                  delete q;
                }
                else
                {
                   p->pre->pre->pre->value[++(p->pre->pre->pre->rank)]= p->value[p->rank];
                   p->pre->pre->pre->suit[p->pre->pre->pre->rank]=p->suit[p->rank];
                   p->rank--;
                   p=p->pre->pre->pre;
                }
            }
            else
            {
                if(p->rank==1)
                {
                    p->pre->value[++(p->pre->rank)]=p->value[p->rank];
                    p->pre->suit[p->pre->rank]=p->suit[p->rank];
                    p->pre->next=p->next;
                    if(p->next)
                    p->next->pre=p->pre;
                    card *q=p;
                    p=p->pre;
                    delete q;
                }
                else
                {
                    p->pre->value[++(p->pre->rank)]=p->value[p->rank];
                    p->pre->suit[p->pre->rank]=p->suit[p->rank];
                    p->rank--;
                    p=p->pre;
                }
            }
        }
        else
        p=p->next;
    }
}
void destory(card * head)
{
    card *p=head->next, *q;
    while(p)
    {
        q=p;
        p=p->next;
        delete q;
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    char s[3];
    while(1)
    {
        scanf("%s", s);
        if(s[0]=='#')break;
        card *p, *q;
        card * head;
        p=new node;
        p->rank=1;
        p->value[1]=s[0];
        p->suit[1]=s[1];
        p->next=NULL;
        head=new node;
        head->next=p;
        p->pre=head;
        for(int i=1; i<52; i++)
        {
            scanf("%s", s);
            q= new node;
            q->rank=1;
            q->value[1]=s[0];
            q->suit[1]=s[1];
            q->next=NULL;
            q->pre=p;
            p->next=q;
            p=q;
        }
        operate(head);
        int count=0;
        p=head->next;
        while(p)
        {
            count++;
            p=p->next;
        }
        if(count==1) printf("1 pile remaining: ");//这个地方poj很坑,poj设定为 1 piles
        else  printf("%d piles remaining: ", count);
        tra(head);
    }

    return 0;
}

//附上生成随机数据的代码
# include<stdio.h>
# include<stdlib.h>
# include<time.h>
struct card{
  char value;
  char suit;
};

int main()
{
    freopen("in.txt", "w", stdout);
    int i, j;
    card a[52], t;
    for(i=0; i<52; i++)
    {
        if(i/13==0) a[i].suit='C';
        else if(i/13==1) a[i].suit='D';
        else if(i/13==2) a[i].suit='H';
        else a[i].suit='S';
        if(i%13==0) a[i].value='A';
        else if(i%13==9) a[i].value='T';
        else if(i%13==10) a[i].value='J';
        else if(i%13==11) a[i].value='Q';
        else if(i%13==12) a[i].value='K';
        else a[i].value=i%13+'1';
    }
    srand((unsigned int)time(NULL));
    for(j=0; j<1000; j++)
    {
        int x=rand()%52, y=rand()%52;
        t=a[x];a[x]=a[y];a[y]=t;
        for(i=0; i<52; i++)
        {
          if(i==26) printf("\n");
          printf("%c%c ", a[i].value, a[i].suit);
        }
        printf("\n");
    }
    printf("#");
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值