UVA - 127 - "Accordian" Patience

题目链接

/*注意每次移动后都从头开始!*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int top[52];
char stack[52][52][3];
int Match(char a[],char b[])
{
    if(a[0]==b[0]||a[1]==b[1])return 1;
    return 0;
} 
int find(int i,int n)
{
    int j,tag;
    tag=0;
    for(j=i-1;j>=0;j--)
    {
        if(top[j]>=0)
        {
            tag++;
        }
        if(tag==n)break; 
    }
    if(j>=0)return j;
    else return -1; 
}            
int Play(char a[][3])
{
    int i,j,k,count=0,t;
    for(i=0;i<52;i++)
    {
        ++top[i];
        strcpy(stack[i][0],a[i]);
    }    
    for(i=1;i<52;i++)
    {
        if(i>2&&top[i]>=0)
        {
            t=find(i,3);  
            if(t>=0)
            {
                if(Match(stack[i][top[i]],stack[t][top[t]]))
                {
                    ++top[t];
                    strcpy(stack[t][top[t]],stack[i][top[i]]);
                    top[i]--;
                    i=0;            
                    continue;
                }  
            }      
        }
        if(top[i]>=0)
        {
            t=find(i,1);
            if(t>=0)
            {
                if(Match(stack[i][top[i]],stack[t][top[t]]))
                {
                    ++top[t];
                    strcpy(stack[t][top[t]],stack[i][top[i]]);
                    top[i]--;
                    i=0;
                }    
            }    
        }    
    }  
    for(i=0;i<52;i++)
    {
        if(top[i]>=0)count++;
    }                                                             
    return count;
}    
int main()
{
    int i,j,k,pile,t;
    char pork[52][3],c;
    i=j=0;
    while((c=getchar())&&c!='#')
    {
        if(isalpha(c)||isdigit(c))
        {
            pork[i][j++]=c;
        }
        else
        {
            pork[i][2]='\0';
            i++;
            j=0;
        }    
        if(i==52)
        {
            for(k=0;k<52;k++)
            {
                top[k]=-1;
            }                 
            i=0;
            j=0;    
            pile=Play(pork);
            if(pile==1)printf("%d pile remaining:",pile);
            else printf("%d piles remaining:",pile);
            for(k=0;k<52;k++)
            {
                if(top[k]>=0)printf(" %d",top[k]+1);
            }                   
            printf("\n");
        }    
    } 
    return 0;
}           
    
  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值