HDU-3328-Flipper(栈模拟)

本文介绍了一款名为Flipper的单人记忆翻牌游戏,并详细解析了其背后的算法实现过程。游戏通过一系列左翻和右翻操作改变排列的卡片朝向,最终目标是预测指定位置的卡片状态。文章提供了完整的C++代码示例,展示了如何使用栈结构来高效地模拟这些翻转操作。

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

Problem Description
Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts withn cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (Card 1 is on the far left; cardn is on the far right.) Some cards are face up and some are face down. Bobby then performsn - 1 flips — either right flips or left flips. In a right flip he takes the pile to the far right and flips it over onto the card to its immediate left. For example, if the rightmost pile has cards A, B, C (from top to bottom) and card D is to the immediate left, then flipping the pile over onto card D would result in a pile of 4 cards: C, B, A, D (from top to bottom). A left flip is analogous.

The very last flip performed will result in one pile of cards — some face up, some face down. For example, suppose Bobby deals out 5 cards (numbered 1 through 5) with cards 1 through 3 initially face up and cards 4 and 5 initially face down. If Bobby performs 2 right flips, then 2 left flips, the pile will be (from top to bottom) a face down 2, a face up 1, a face up 4, a face down 5, and a face up 3.

Now Bobby is very sharp and you can ask him what card is in any position and he can tell you!!! You will write a program that matches Bobby’s amazing feat.
 

Input
Each test case will consist of 4 lines. The first line will be a positive integern (2 ≤ n ≤ 100) which is the number of cards laid out. The second line will be a string ofn characters. A character U indicates the corresponding card is dealt face up and a character D indicates the card is face down. The third line is a string ofn - 1 characters indicating the order of the flips Bobby performs. Each character is either R, indicating a right flip, or L, indicating a left flip. The fourth line is of the formm q1 q2 . . . qm, where m is a positive integer and 1 ≤qin. Each qi is a query on a position of a card in the pile (1 being the top card,n being the bottom card). A line containing 0 indicates end of input.
 

Output
Each test case should generate m + 1 lines of output. The first line is of the form
Pile t
where t is the number of the test case (starting at 1). Each of the nextm lines should be of the form
Card qi is a face up k.
or
Card qi is a face down k.
accordingly, for i = 1, ..,m, where k is the number of the card.
For instance, in the above example with 5 cards, if qi = 3, then the answer would be
Card 3 is a face up 4.
 

Sample Input
5 UUUDD RRLL 5 1 2 3 4 5 10 UUDDUUDDUU LLLRRRLRL 4 3 7 6 1 0
 

Sample Output
Pile 1 Card 1 is a face down 2. Card 2 is a face up 1. Card 3 is a face up 4. Card 4 is a face down 5. Card 5 is a face up 3. Pile 2 Card 3 is a face down 1. Card 7 is a face down 9. Card 6 is a face up 7. Card 1 is a face down 5.


思路:R操作就是把最右边的一堆牌放到次右边并改变每一张牌的方向,L操作就是把最左边的一堆牌放到次左边并改变每一张牌的方向。注意:每一对牌的最上面一个先取出,因此,直接用栈来模拟比较方便。

#include <cstdio>
#include <stack>
using namespace std;

struct S{
char s;
int num;
}que[101];

stack<struct S>a[101];//用栈模拟

int main()
{
    int n,m,i,top,bottom,casenum=1;
    char c[101];
    struct S temp;

    while(~scanf("%d",&n) && n)
    {
        top=1;
        bottom=n;

        scanf("%s",c);

        for(i=1;i<=n;i++)
        {
            while(!a[i].empty())//读入数据前先清栈
            {
                a[i].pop();
            }

            temp.s=c[i-1];
            temp.num=i;

            a[i].push(temp);//把数据压栈
        }


        scanf("%s",c);

        for(i=1;i<n;i++)
        {
            if(c[i-1]=='R')//模拟R操作
            {
                while(!a[bottom].empty())//把最右边的一个栈数据取出,改变方向,压入次右边的栈。直到最右边的栈为空。
                {
                    temp=a[bottom].top();
                    temp.s=(temp.s=='U'?'D':'U');

                    a[bottom-1].push(temp);
                    a[bottom].pop();
                }

                bottom--;
            }
            else//模拟L操作
            {
                while(!a[top].empty())//把最左边的一个栈数据取出,改变方向,压入次左边的栈。直到最左边的栈为空。
                {
                    temp=a[top].top();
                    temp.s=(temp.s=='U'?'D':'U');

                    a[top+1].push(temp);
                    a[top].pop();
                }

                top++;
            }
        }

        scanf("%d",&m);

        for(i=1;i<=n;i++)//把模拟完成后的所有数据取出,放入que数组
        {
            que[i]=a[top].top();
            a[top].pop();
        }

        printf("Pile %d\n",casenum++);

        while(m--)
        {
            scanf("%d",&i);

            printf("Card %d is a face %s %d.\n",i,que[i].s=='D'?"down":"up",que[i].num);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值