UVa 127 "Accordian" Patience (模拟链表&栈)

127 - "Accordian" Patience Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=63

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

题意比较难懂,英语不好是硬伤。试着翻译一下吧!

让你来模拟一种纸牌游戏,规则如下:

纸牌从左到右排成一排,没有覆盖,当一张纸牌匹配它左边第一张或者左边第三张,就要移动到那张牌上面去,两张纸牌当花色或者点数相同时可以匹配,移动之后,查看是否还有新的移动,每一堆牌只有最上面那一张可以移动,如果两堆牌之间有空缺,那么所有堆牌需要从右向左移动一个位置,尽可能让所有牌向左移动,当还剩一堆牌时游戏就赢了。

当两张牌都可以移动时,应该优先移动最左边那张,当一张牌可以移到不止一个位置时,优先移到左边第三个位置。

输入包括两行,每一行26张牌,每张牌第一个数是点数,第二个数是花色。

输出最后剩下几堆牌,以及每一堆的数量。

 

思路:用数组模拟链表和栈,链表来控制合并操作,每一堆牌都是一个栈。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define N 100005
using namespace std;

const int len = 53;

struct node
{
    char value,suit;
    bool operator < (const node a)const  //重载操作符,花色或者点数相同返true。
    {
        return value==a.value||suit==a.suit? true : false;
    }
}card[len][len]; //第一维代表每一叠牌,第二维是这一叠牌的数量。每一堆都是一个栈,每次只能取栈顶元素。

char s[160];
int in[len]; //每一堆牌的高度(栈顶元素)
inline void move(int i,int &n) //第i张牌可以移动到前面,有空缺那么后面i+1到n堆牌必须往前移动
{
    int j,k;
    for(j=i;j<n;j++)
    {
        for(k=0;k<=in[j+1];++k) //一堆牌往前移动,并且总共有in[j+1]张牌
            card[j][k]=card[j+1][k];
        in[j]=in[j+1];
    }
    --n; //表示少了一堆
}

int main()
{
    int i,n;
    bool flag;
    while(gets(s),s[0]!='#')
    {
        s[77]=' ';
        gets(s+78); //第二行从78开始
        for(i=0;i<155;i+=3)
        {
            card[i/3][0].value = s[i];
            card[i/3][0].suit = s[i+1];
        }
        memset(in,0,sizeof(in));//每一堆牌的初始高度为0
        for(i=1,n=52;i<n;i++)
        {
            flag=true;
            if(i>=3 && card[i][in[i]]< card[i-3][in[i-3]]) //如果当前这张牌能够移动到左边第三堆牌上去。
            {
                card[i-3][++in[i-3]]=card[i][in[i]--]; //把当前牌移动到上面,in[i]表示  第i堆牌有多少张牌,并且card[i][in[i]]代表当前堆的最上面的牌
                flag = false;
            }
            if(flag && card[i][in[i]] < card[i-1][in[i-1]]) //当前牌可以移动到左边的第一堆上去
            {
                card[i-1][++in[i-1]] = card[i][in[i]--];
                flag = false;
            }
            if(in[i] < 0) //当前堆没有牌,出现空缺,i后面的需要从右往左移
            {
                move(i,n);
                i=0;      //移动后可能 还能在移动,
                continue;
            }
            if(!flag) i=0; //当前堆还有牌
        }
       printf("%d %s remaining: ",n,n==1 ? "pile":"piles");
       for(i = 0; i < n; i++)
            printf("%d%c",in[i]+1,i == n - 1 ? '\n':' ');
    }
    return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值