UVA101 HDU1612 POJ1208 The Blocks Problem【模拟】

问题链接UVA101 HDU1612 POJ1208 The Blocks Problem

问题简述:参见上述链接。


问题分析:这是一个模拟题,程序过程都是套路。

程序说明

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

开始的时候,编写的程序在UVA和POJ中都AC,可是在HDU中是“Presentation Error”。问题出在输出格式上,针对HDU另外写了一个程序就AC了,需要修改45行。这个格式问题很坑啊!!!


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

/* UVA101 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("
");
    }
}

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;
}


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

/* HDU1612 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("%2d: ", i);
        for(int j=0; j<(int)pile[i].size(); j++)
            printf(" %d", pile[i][j]);
        printf("
");
    }
}

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;
}




原文地址:https://www.cnblogs.com/tigerisland/p/7564486.html