2014年2月8日 星期六

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 performed tasks involving the manipulation of blocks.
In this problem you will model a simple block world under certain rules and constraints. Rather than determine how to achieve a specified state, you will ``program'' a robotic arm to respond to a limited set of commands.

The Problem 

The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks that lie on a flat table. Initially there are nblocks on the table (numbered from 0 to n-1) with block bi adjacent to block bi+1 for all $0 \leq i < n-1$ as shown in the diagram below:

\begin{figure}
\centering
\setlength{\unitlength}{0.0125in} %
\begin{picture}
(2...
...raisebox{0pt}[0pt][0pt]{$\bullet
\bullet \bullet$ }}}
\end{picture}
\end{figure}
Figure: Initial Blocks World

The valid commands for the robot arm that manipulates blocks are:
  • move a onto bwhere a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions.
  • move a over bwhere a and b are block numbers, puts block a onto the top of the stack containing block b, after returning any blocks that are stacked on top of block a to their initial positions.
  • pile a onto bwhere a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are moved to their initial positions prior to the pile taking place. The blocks stacked above block aretain their order when moved.
  • pile a over bwhere a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto the top of the stack containing block b. The blocks stacked above block a retain their original order when moved.
  • quitterminates manipulations in the block world.
Any command in which a = b or in which a and b are in the same stack of blocks is an illegal command. All illegal commands should be ignored and should have no affect on the configuration of blocks.

The Input 

The input begins with an integer n on a line by itself representing the number of blocks in the block world. You may assume that 0 < n < 25.
The number of blocks is followed by a sequence of block commands, one command per line. Your program should process all commands until the quit command is encountered.
You may assume that all commands will be of the form specified above. There will be no syntactically incorrect commands.

The Output 

The output should consist of the final state of the blocks world. Each original block position numbered i ( $0 \leq i < n$ where n is the number of blocks) should appear followed immediately by a colon. If there is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear stacked in that position with each block number separated from other block numbers by a space. Don't put any trailing spaces on a line.
There should be one line of output for each block position (i.e., n lines of output where n is the integer on the first line of input).

Sample Input 

10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit

Sample Output 

 0: 0
 1: 1 9 2 4
 2:
 3: 3
 4:
 5: 5 8 7 6
 6:
 7:
 8:
 9:

出處: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37



問題敘述

此問題首先會輸入積木的個數,接著會輸入一連串的指令來告訴機器手臂該如何搬動積木,一直到輸入quit才結束。假設積木的總數量為n,積木會從0開始編號到n-1,指令的形式為: move/pile 積木編號 onto/over 積木編號,舉例: move 9 onto 1 意思就是把9號積木移動到1號積木的上面,若9號積木或1號積木上面有其他積木,就把上面的積木都移回原位。move 8 over 1 意思就是把8號積木移動到有1號積木的積木堆上面,若8號積木上面有其他積木,就把上面的積木都移回原位。pile 8 onto 6 意思就是把8號以上的積木全部移動到6號積木的上面,若6號積木上面有其他積木,就把上面的積木都移回原位。pile 8 over 6 意思就是把8號以上積木全部移動到有6號積木的積木堆上面。

解題思路

沒有什麼特別的思路,照著做就可以求解。不過在寫程式上卻有一些技巧,因為很多指令的動作是有重複的,所以可以把它寫成function呼叫。比如說"將某個積木上面的所有積木搬回原位"這個動作是很常用到的,所以可以寫成function比較方便。還有要注意的一點是如果指令裡的2個積木編號是在同一個積木堆上,則此指令無效,必須忽略此指令否則會wrong answer。

c++ 程式碼

#include <iostream>
#include <vector>
using namespace std;

void moveBackToOri(vector< vector<int> >& blocks, vector<int>& blockPos, int pos, int theBlock) {
    while (blocks[pos].back() != theBlock) {
        int last = blocks[pos].back();

        blocks[pos].pop_back();
        blocks[last].push_back(last);
        blockPos[last] = last;
    }
}

void pileBlocks(vector< vector<int> >& blocks, vector<int>& blockPos, int pos, int theBlock, vector<int>& temp) {
    while (true) {
        int last = blocks[pos].back();

        blocks[pos].pop_back();
        temp.push_back(last);
        if (last == theBlock)
            break;
    }
}

int main() {
    int blockNum;

    cin >> blockNum;
    vector< vector<int> > blocks(blockNum, vector<int>());
    vector<int> blockPos(blockNum, -1);
    for (int i=0; i<blocks.size(); i++) {
        blocks[i].push_back(i);
        blockPos[i] = i;
    }
    while (true) {
        string action, where;
        int fromBlock, toBlock;
        int fromPos;
        int toPos;

        cin >> action;
        if (action == "quit")
            break;
        cin >> fromBlock >> where >> toBlock;
        fromPos = blockPos[fromBlock];
        toPos = blockPos[toBlock];
        if (fromPos != toPos) {
            if (action == "move") {
                if (where == "onto") {
                    moveBackToOri(blocks, blockPos, fromPos, fromBlock);
                    moveBackToOri(blocks, blockPos, toPos, toBlock);
                    blocks[fromPos].pop_back();
                    blocks[toPos].push_back(fromBlock);
                    blockPos[fromBlock] = toPos;
                }
                else {
                    moveBackToOri(blocks, blockPos, fromPos, fromBlock);
                    blocks[fromPos].pop_back();
                    blocks[toPos].push_back(fromBlock);
                    blockPos[fromBlock] = toPos;
                }
            }
            else {
                vector<int> temp;

                if (where == "onto") {
                    pileBlocks(blocks, blockPos, fromPos, fromBlock, temp);
                    moveBackToOri(blocks, blockPos, toPos, toBlock);
                    while (!temp.empty()) {
                        int last = temp.back();

                        temp.pop_back();
                        blocks[toPos].push_back(last);
                        blockPos[last] = toPos;
                    }
                }
                else {
                    pileBlocks(blocks, blockPos, fromPos, fromBlock, temp);
                    while (!temp.empty()) {
                        int last = temp.back();

                        temp.pop_back();
                        blocks[toPos].push_back(last);
                        blockPos[last] = toPos;
                    }
                }
            }
        }
    }
    for (int i=0; i<blocks.size(); i++) {
        cout << i << ":";
        for (int j=0; j<blocks[i].size(); j++)
            cout << " " << blocks[i][j];
        cout << endl;
    }

    return 0;
}

沒有留言:

張貼留言