0

所以,我正在做来自 UVa Online Judge的著名的“ The Blocks Problem” 。

我的方法很愚蠢,那是因为我想玩向量。所以,我得到了指向堆中每个块的指针的向量,这些向量存储在一个称为集合的向量中。

为了找到所有块,我有一个名为 blockCollection 的向量,其中存储了指向所有块的指针。

该代码已通过提供的示例。稍后我将尝试编辑并提供评论。

完整来源:

#include <iostream>
#include <vector>

struct Block
{
    int id;
    std::vector<Block*>* where;
};

int positionInVector(Block* b);

int main(int argc, const char * argv[])
{
    std::vector<std::vector<Block*>*> collection;
    std::vector<Block*> blockCollection;
    std::string command = "", command2 = "";
    int blockCount = 0, k = 0, A = 0, B = 0;

    while ( std::cin >> blockCount )
    {
        std::vector<Block*>* vectors = new std::vector<Block*>[blockCount];
        Block* blocks = new Block[blockCount];
        for ( k = 0 ; k < blockCount ; ++ k)
        {
            blocks[k].id = k;
            blocks[k].where = &vectors[k];
            vectors[k].push_back(&blocks[k]);
            blockCollection.push_back(&blocks[k]);
            collection.push_back(&vectors[k]);
        }
        std::cin >> std::ws;
        while ( std::cin >> command )
        {
            if ( command == "quit" ) break;
            std::cin >> A >> command2 >> B;
            Block* blockA = blockCollection[A];
            Block* blockB = blockCollection[B];
            std::vector<Block*>* vectorA = blockA -> where;
            std::vector<Block*>* vectorB = blockB -> where;

            //exception handle
            if ( A > blockCount || B > blockCount ) continue;
            if ( A == B ) continue;
            if ( vectorA == vectorB ) continue;

            if ( command == "move" )
            {
                //move anything on top of A to its original position
                int positionOfBlockAInVectorA = positionInVector(blockA);
                for ( int i = positionOfBlockAInVectorA + 1 ; i < vectorA -> size() ; ++ i )
                {
                    Block* blockToBeMoved = *(vectorA -> begin() + i);
                    std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id];
                    blockToBeMoved -> where = destinationVector;
                    destinationVector -> push_back(blockToBeMoved);
                }
                vectorA -> erase(vectorA -> begin() + positionOfBlockAInVectorA + 1, vectorA -> end());
            }
            if ( command2 == "onto" )
            {
                //move anything on top of B to its original position
                int positionOfBlockBInVectorB = positionInVector(blockB);
                for ( int i = positionOfBlockBInVectorB + 1 ; i < vectorB -> size() ; ++ i )
                {
                    Block* blockToBeMoved = *(vectorB -> begin() + i);
                    std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id];
                    blockToBeMoved -> where = destinationVector;
                    destinationVector -> push_back(blockToBeMoved);
                }
                if (positionOfBlockBInVectorB + 1 > vectorB -> size()) vectorA -> erase(vectorB -> begin() + positionOfBlockBInVectorB + 1, vectorB -> end());
            }
            if ( command == "move" )
            {
                //move block a to the pile containing block b
                vectorA -> pop_back();
                blockA -> where = vectorB;
                vectorB -> push_back(blockA);
            }
            else
            {
                //move block a and those on top of it to the pile containing block b
                std::vector<Block*> temperaryVector;
                int positionOfBlockAInVectorA = positionInVector(blockA);
                for ( int i = (int)vectorA -> size() - 1 ; i >= positionOfBlockAInVectorA ; -- i )
                {
                    temperaryVector.push_back(vectorA -> at(i));
                    vectorA -> erase(vectorA -> begin() + i);
                }
                for ( int i = (int)temperaryVector.size() - 1 ; i >= 0 ; -- i )
                {
                    temperaryVector[i] -> where = vectorB;
                    vectorB -> push_back(temperaryVector[i]);
                }
            }
        }

        for ( k = 0 ; k < blockCount ; ++ k )
        {
            std::vector<Block*>* vector = collection[k];
            std::cout << k << ":";
            if ( !vector -> empty() )
            {
                for ( int i = 0 ; i < vector -> size() ; ++ i )
                {
                    std::cout << " " << vector -> at(i) -> id;
                }
            }
            std::cout << std::endl;
        }
        delete [] blocks;
        delete [] vectors;
    }
    return 0;
}

int positionInVector(Block* block)
{
    std::vector<Block*> vector = *block -> where;
    for ( int i = 0 ; i < vector.size() ; ++ i )
    {
        if ( vector[i] == block ) return i;
    }
    return -1;
}

谢谢!

4

2 回答 2

1

每次向 blockCollection 添加或删除 Block 时,您持有的指向集合中任何 Block 的每个指针都可能失效。

我想这就是我最初需要说的...

于 2013-01-22T14:25:22.423 回答
1

为此:

        A = (int)command[5] - 48;
        B = (int)command[12] - 48;

我们需要确保字符串长度为 5/12 个字符,并且在这些位置有一个数字。代码应在这些地方添加对输入长度和数字有效性的检查。

于 2013-01-22T14:32:38.927 回答