首页 > 代码库 > uva 101 - The Blocks Problem

uva 101 - The Blocks Problem

用 栈 stack 来模拟

题目意思:http://blog.csdn.net/mobius_strip/article/details/12765319

下面的代码有坑,输出格式需要调。

#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn=25+5;
stack<int >s[maxn];
int pos[maxn];

int main()
{
    int n;
    string s1,s2;
    int a,b;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        s[i].push(i);
        pos[i]=i;
    }
    while(cin>>s1)
    {
        if(s1[0]==‘q‘)
            break;
        cin>>a>>s2>>b;
        if(s1=="move"&&s2=="onto")
        {
            while(s[pos[a]].top()!=a)
            {
                int temp=s[pos[a]].top();
                s[temp].push(temp);
                pos[temp]=temp;
                s[pos[a]].pop();
            }
            while(s[pos[b]].top()!=b)
            {
                int temp=s[pos[b]].top();
                s[temp].push(temp);
                pos[temp]=temp;
                s[pos[b]].pop();
            }
            s[pos[a]].pop();
            s[pos[b]].push(a);
            pos[a]=pos[b];

            printf("1=====\n");
        }
        if(s1=="move"&&s2=="over")
        {
            while(s[pos[a]].top()!=a)
            {
                int temp=s[pos[a]].top();
                s[temp].push(temp);
                pos[temp]=temp;
                s[pos[a]].pop();
            }
            s[pos[a]].pop();
            s[pos[b]].push(a);
            pos[a]=pos[b];
            printf("2=====\n");
        }
        if(s1=="pile"&&s2=="onto")
        {
            while(s[pos[b]].top()!=b)
            {
                int temp=s[pos[b]].top();
                s[temp].push(temp);
                pos[temp]=temp;
                s[pos[b]].pop();
            }
            stack<int >s_temp;
            while(s[pos[a]].top()!=a)
            {
                int temp=s[pos[a]].top();
                s_temp.push(temp);
                pos[temp]=pos[b];
                s[pos[a]].pop();
            }
            s[pos[a]].pop();
            s[pos[b]].push(a);
            pos[a]=pos[b];
            while(!s_temp.empty())
            {
                int temp=s_temp.top();
                s[pos[b]].push(temp);
                s_temp.pop();
            }
            printf("3=====\n");
        }
        if(s1=="pile"&&s2=="over")
        {
            stack<int >s_temp;
            while(s[pos[a]].top()!=a)
            {
                int temp=s[pos[a]].top();
                s_temp.push(temp);
                pos[temp]=pos[b];
                s[pos[a]].pop();
            }
            s[pos[a]].pop();
            s[pos[b]].push(a);
            pos[a]=pos[b];
            while(!s_temp.empty())
            {
                int temp=s_temp.top();
                s[pos[b]].push(temp);
                s_temp.pop();
            }
            printf("4=====\n");
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%d:",i);
        while(!s[i].empty())
        {
            printf(" %d",s[i].top());
            s[i].pop();
        }
        printf("\n");
    }
    return 0;
}