【HDU 3328】【数据结构】Flipper

       


就是一个翻牌子的问题,写的略繁琐,因为只是实验室水题,所以不优化了,L是从最左翻面并且覆盖到第二堆上,以此类推,模拟就好。


#include "stdio.h"
#include "stack"
#include "queue"
using namespace std;
int main(int argc,char const *argv[])
{
    int n;
    int cas=1;
    while(~scanf("%d",&n) && n)
    {
        getchar();
        int lnum=1;
        int rnum=n;
        int Poker[105];
        stack <int> s1,s2,s3;
        char sta,ope;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%c",&sta);
            if(sta=='D')
                Poker[i]=0;
            else
                Poker[i]=1;
        }
        getchar();
        s1.push(1);
        s2.push(n);

        for (int i = 1; i < n; ++i)
        {
            scanf("%c",&ope);
            if(ope=='L')
            {
                for (int j = 1; j <= lnum; ++j)
                {
                    if(Poker[j])
                        Poker[j]=0;
                    else
                        Poker[j]=1;
                }
                lnum++;
                if(lnum==rnum)
                {
                    while(!s2.empty())
                    {
                        int temp2=s2.top();
                        s2.pop();
                        s1.push(temp2);
                    }
                    while(!s1.empty())
                    {
                        int temp1=s1.top();
                        s1.pop();
                        s3.push(temp1);
                    }
                    break;
                }
                queue <int> temp;
                temp.push(lnum);
                while(!s1.empty())
                {
                    int temp1=s1.top();
                    s1.pop();
                    temp.push(temp1);
                }
                while(!temp.empty())
                {
                    int temp1=temp.front();
                    temp.pop();
                    s1.push(temp1);
                }
            }
            if(ope=='R')
            {
                for (int j = n; j >= rnum; --j)
                {
                    if(Poker[j])
                        Poker[j]=0;
                    else
                        Poker[j]=1;
                }
                rnum--;
                if(lnum==rnum)
                {
                    while(!s1.empty())
                    {
                        int temp1=s1.top();
                        s1.pop();
                        s2.push(temp1);
                    }
                    while(!s2.empty())
                    {
                        int temp2=s2.top();
                        s2.pop();
                        s3.push(temp2);
                    }
                    break;
                }
                queue <int> temp;
                temp.push(rnum);
                while(!s2.empty())
                {
                    int temp2=s2.top();
                    s2.pop();
                    temp.push(temp2);
                }
                while(!temp.empty())
                {
                    int temp2=temp.front();
                    temp.pop();
                    s2.push(temp2);
                }
            }
        }
        printf("Pile %d\n",cas++);
        int num;
        int a[1000];
        int b[1000];
        int bnum=1;
        while(!s3.empty())
        {
            int temp=s3.top();
            b[bnum++]=temp;
            s3.pop();
        }
        scanf("%d",&num);
        for (int i = 0; i < num; ++i)
        {
            scanf("%d",&a[i]);
        }
        for (int i = 0; i < num; ++i)
        {
            printf("Card %d is a face ",a[i]);
            if(Poker[b[a[i]]])
                printf("up ");
            else
                printf("down ");
            printf("%d.\n",b[a[i]]);
        }


        
    }
    return 0;
}

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...