The Blocks Problem
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 1896 |
|
Accepted: 681 |
Description
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 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.
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.
Output
The output should consist of the final state of the blocks world. Each original block position numbered i ( 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:
Source
Duke Internet Programming Contest 1990,uva 101
/* 好久没做模拟题了,这题移来移去的还是挺麻烦的 用链表实现可能更简单,但速度肯定不行,用数组 稍微麻烦点,但是速度还可以 */ #include #include #define MAX_N 25 struct block { int pos, heightPos; //当前block所在的位置以及在当前位置所处的高度 int upNum; //当前位置上的block数 int which[MAX_N + 1]; //记录当前位置都有哪些block block(int p = 0) { pos = p; heightPos = 0; upNum = 1; which[0] = p; } }blocks[MAX_N + 1]; int bNum; char com1[6], com2[6]; //存储指令 int src, dest; //源block与目标block //把block id所在位置id上面的block都放回原处,如果rm = true则id也去掉,否则id不动 void moveBackTop(int id, bool rm) { int posSrc = blocks[id].pos, p; int tempHeight = blocks[id].heightPos; for(p = blocks[id].heightPos + 1; p < blocks[posSrc].upNum; p++) { int tempId = blocks[posSrc].which[p]; blocks[tempId].pos = tempId; blocks[tempId].upNum++; blocks[tempId].heightPos = blocks[tempId].upNum - 1; blocks[tempId].which[blocks[tempId].heightPos] = tempId; } if(rm) blocks[posSrc].upNum = tempHeight; else blocks[posSrc].upNum = tempHeight + 1; } //单纯地把block src移动到block dest所在的位置上 void moveSingleTop(int src, int dest) { int pos = blocks[dest].pos; blocks[pos].upNum++; blocks[pos].which[blocks[pos].upNum - 1] = src; blocks[src].pos = pos; blocks[src].heightPos = blocks[pos].upNum - 1; } //把src及其上方的所有block移动到dest所在的block上 void movePileTop(int src, int dest) { int srcPos = blocks[src].pos; int srcH = blocks[src].heightPos; int srcUpNum = blocks[srcPos].upNum; int destH = blocks[dest].heightPos; int destPos = blocks[dest].pos; for(int i = srcH; i < srcUpNum; i++) { int curId = blocks[srcPos].which[i]; blocks[srcPos].upNum--; blocks[destPos].upNum++; blocks[destPos].which[blocks[destPos].upNum - 1] = curId; blocks[curId].pos = destPos; blocks[curId].heightPos = blocks[destPos].upNum - 1; } } int main() { int i, j; scanf("%d", &bNum); for(i = 0; i < bNum; i++) blocks[i] = block(i); while(scanf("%s", com1) && strcmp(com1, "quit") != 0) { scanf("%d%s%d", &src, com2, &dest); if(src == dest || blocks[src].pos == blocks[dest].pos) continue; //先把a上面的block和b上面的block都归位,然后在把block a移动到block b所在的位置 if(strcmp(com1, "move") == 0 && strcmp(com2, "onto") == 0) { moveBackTop(src, true); moveBackTop(dest, false); moveSingleTop(src, dest); } //先把a上面的block归位,然后在把block a移动到block b所在的位置 else if(strcmp(com1, "move") == 0 && strcmp(com2, "over") == 0) { moveBackTop(src, true); moveSingleTop(src, dest); } //先把b上面的block归位,然后在把block a及其上面的所有block移动到block b所在的位置 else if(strcmp(com1, "pile") == 0 && strcmp(com2, "onto") == 0) { moveBackTop(dest, false); movePileTop(src, dest); } //把block a及其上面的所有block移动到block b所在的位置 else movePileTop(src, dest); } for(i = 0; i < bNum; i++) { printf("%d:", i); for(j = 0; j < blocks[i].upNum; j++) printf(" %d", blocks[i].which[j]); printf("/n"); } return 0; }