UVA101 HDU1612 POJ1208 The Blocks Problem

问题链接:UVA101 HDU1612 POJ1208 The Blocks Problem

这是一个模拟题,程序过程都是套路。

程序中用到了STL的容器类vector。

这个程序在UVA和POJ中都AC,可是在HDU中是“Presentation Error”。

AC通过的C++语言程序如下:

/* UVA101 HDU1612 POJ1208 The Blocks Problem */

#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int MAXN = 25;

vector<int> pile[MAXN];

// 找木块block的堆
void find_block(int block, int& p, int& h, int n)
{
    for(p=0; p<n; p++)
        for(h=0; h<(int)pile[p].size(); h++)
            if(pile[p][h] == block)
                return;
}

// 把p堆高度h上方的块归位
void clear_above(int p, int h)
{
    for(int i=h+1; i<(int)pile[p].size(); i++) {
        int block = pile[p][i];
        pile[block].push_back(block);
    }
    pile[p].resize(h+1);
}

// 把pfrom堆高度为h及其上方的所有木块移动到pto堆
void pile_onto(int pfrom, int h, int pto)
{
    for(int i=h; i<(int)pile[pfrom].size(); i++)
        pile[pto].push_back(pile[pfrom][i]);

    pile[pfrom].resize(h);
}

// 输出结果
void output_result(int n)
{
    for(int i=0; i<n; i++) {
        printf("%d:", i);
        for(int j=0; j<(int)pile[i].size(); j++)
            printf(" %d", pile[i][j]);
        printf("\n");
    }
}

int main()
{
    int n, from, to;
    string command, action;

    cin >> n;

    for(int i=0; i<n; i++)
        pile[i].push_back(i);

    while(cin >> command && command != "quit") {
        cin >> from >> action >> to;

        int frompile, fromheight, topile, toheight;

        find_block(from, frompile, fromheight, n);
        find_block(to, topile, toheight, n);

        if(frompile == topile)
            continue;

        if(command == "move")
            clear_above(frompile, fromheight);
        if(action == "onto")
            clear_above(topile, toheight);

        pile_onto(frompile, fromheight, topile);
    }

    output_result(n);

    return 0;
}
时间: 2024-10-29 19:07:23

UVA101 HDU1612 POJ1208 The Blocks Problem的相关文章

uva101木块问题The Blocks Problem

背景:一看这道题,再看书前面的知识点,我只能说一点不会,于是我将上面的代码在电脑上面打了一遍,这才会一点点新知识的运用,然后我就按照自己的想法,说实话已经参照了书上的一些东西,然后自己去用新知识写代码,当我满心欢喜的去测试的时候,啊,wrong,仔细一读题,才发现自己题意都理解错了. 思路:模拟四条指令即可.由于每个木块堆的高度不确定,所以用vector来保存很合适. 学习:vector就是一个不定长数组.若a是一个vector,可以用a.size()读取他的大小,用a.resize()改变他的

poj1208 The Blocks Problem,模拟,vector

题意: 从左到右有n个积木,依次编号0~n-1,要求模拟以下4种操作. 1.move a onto b a和b都是积木的编号,先将a和b上面所有的积木都放回原处,再将a放在b上. 2.move a over b a和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上.(b上原有积木不动) 3.pile a onto b a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上.在移动前要先将b上面所有的积木都放回原处.移动的一摞积木要保持原来的顺序不变. 4.pile a

UVA101 The Blocks Problem

 The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AIstudy of planning and robotics (STRIPS) used a block world in which arobot arm performed ta

POJ 1208 The Blocks Problem

The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5397   Accepted: 2312 Description Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of

UVA - 101 The Blocks Problem(STL,模拟)

The Blocks Problem Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an e

101 - The Blocks Problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37   The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empiric

uva 101 The Blocks Problem (模拟)

uva 101  The Blocks Problem Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm per

uva 101 The Blocks Problem (基本算法-模拟)

 The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm performed

The Blocks Problem(vector)

题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5004   Accepted: 2119 Description Many areas of Computer Science use simple, abstract domains for both analytical and empirical studie