4月 26, 2008

【解題】The Blocks Problem

@
ACM Volume I 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 n blocks on the table (numbered from 0 to n-1) with block bi adjacent to block bi+1 for all 0 ≦ i < n-1 as shown in the diagram below:



The valid commands for the robot arm that manipulates blocks are:

  * move a onto b

   where 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 b

   where 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 b

   where 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 a retain their order when moved.

  * pile a over b

   where 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.

  * quit

   terminates 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 0 ≦ 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:


解題思考

  這一題不會太難,只要掌握題目即可順利完成。

  首先,我先使用一個n*n的陣列去存整個資料內容,再使用兩個大小為n的陣列儲存編號 i (0 ≦ i < n)積木在第幾行、第幾列。另外還有一個陣列表示每一堆的頂端。

  接著分析指令時,只要有 "move" 與 "onto" 這兩種指令,就需要把其上的積木歸位。所以我獨立出一個"歸位(returned)"的函式,供這兩種指令呼叫。

  之後的工作,就只是把 a 上的積木移動到 b 之上,要實現也不會太難。只要注意到題目所說,若 a = b 或是 a 與 b 在同一堆中,就不能執行任何動作。一開始我就是疏忽掉這一點了,難怪錯誤頻頻,害我百思不得其解。

  除了這種解法之外,我發現有人是使用串列(List)來解決這題。也就是說,一個積木的資料中,有一個指標指向其後的積木。這樣搬移積木時,只需要更改少部分的指標即可,可以省下許多搬移陣列元素的時間(我的陣列解法所需執行時間,約是此種方法的兩倍)。

  由於是別人寫的,這裡我就不貼出來了。


參考解答(C++)

#include <iostream>

using namespace std;

void returned(int);
void moved(int, int);

int **block, *col, *row, *top;

int main(void)
{
    int n;

    while (cin >> n)
    {
        // 動態配置記憶體, 並賦予初值
        col = new int[n];
        row = new int[n];
        top = new int[n];

        block = new int *[n];
        for (int i = 0; i < n; i++)
        {
            row[i] = 0;
            col[i] = i;

            top[i] = 1;

            block[i] = new int[n];
            block[i][0] = i;
        }

        while (1)
        {
            int a, b;
            char command[2][5];

            // 判斷輸入是否結束
            cin >> command[0];
            if (!strcmp(command[0], "quit")) { break; }

            cin >> a >> command[1] >> b;

            if (a != b && col[a] != col[b])
            {
                // 分析指令
                if (!strcmp(command[0], "move")) { returned(a); }

                if (!strcmp(command[1], "onto")) { returned(b); }

                moved(a, b);
            }
        }

        // 印出執行結果
        for (int i = 0; i < n; i++)
        {
            cout << i << ":";

            for (int j = 0; j < top[i]; j++)
            {
                cout << " " << block[i][j];
            }

            cout << endl;
        }

        // 釋放記憶體空間
        delete [] col;
        delete [] row;
        delete [] top;

        for (int i = 0; i < n; i++) { delete [] block[i]; }
        delete [] block;
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

void returned(int a)
{
    int x = col[a];

    // 將 block 之上全部歸位
    for (int i = row[a] + 1; i < top[x]; i++)
    {
        int n = block[x][i];

        block[n][top[n]] = n;

        row[n] = top[n]++;
        col[n] = n;
    }

    // 更新此行頂端
    top[x] = row[a] + 1;
}

void moved(int a, int b)
{
    int x = col[a], y = col[b], m = row[a];

    // 依序移動 a 到 b 上
    for (int i = 0; i < top[x] - m; i++)
    {
        int n = block[y][top[y] + i] = block[x][m + i];

        col[n] = y;
        row[n] = top[y] + i;
    }

    // 更新頂端
    top[y] += top[x] - m;
    top[x] = m;
}

3 回覆:

智涵 提到...

這要用Linked List的話
要用雙向的Linked List
由於是雙向的
移動一個地方要改2個指標
比電子鍵還麻煩XD

我的答案
http://kgame.ihost.tw/NETCF/read.php?tid=9&fpage=2
不知道有沒有比較快
不過真的是比你的長
我把一些BOX當成桌子用
所以Stat是用來分辯桌子和BOX的

小參 提到...

我沒記錯的話
當初看到的Linked List
好像是單向的!?
程式碼沒有留下來Orz

C#程式我都要看好久才看懂...
你的方法跟我看到的那個解法有點像

以效率來說的話
你的解法相較於我的
省去了許多搬移的動作
假如使用同一支語言來寫
應該比較快吧XD

kgame 智涵 提到...

怪了
我拿你的Code去Judge, 0.012秒
我的Double Linked List,也是0.012秒= =
//原本有用string還到0.016秒

張貼留言