Time Limit:3000MS Memory Limit:0KB
Description
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 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 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:
题解:
1、这道题总共有四种堆积木的方法,(1)是move onto 就是将a,b上面的积木恢复到原来的位置。将a堆在b上。(2)是move over 就是将a上面的积木恢复到原来的位置,将a堆在b所在的那一摞积木的最上面。(3)是pile onto 就是将b上面的积木恢复到原来的位置,将a及其上面的积木堆在b上。(4)pile over 就是将a及其上面的积木堆在b所在那一摞积木的最上面。
2、其实如果a,b上面都没有积木,那么pile onto、pile over 其实是和move onto、move over没有区别的,a,b上有积木,区别就在于是否需要将a或b上的积木恢复。所以可以拆分为小函数。(1)是函数restore(int c),将c上的积木恢复。(2)是函数pile(int a,int b)将a及其以上积木,堆在含有b那一摞积木的最上面。
3、那么四种方法可以简化为三步 ,(1)如果是move,那么就restore(a);(2)如果是onto 就restore(b);(3)最后pile(a,b)即可。
4、执行操作过程中,如果a==b 或者a和b在同一摞,则为无效指令,跳过即可,判断在islegal()中。
5、特别需要注意的是输出格式,最后一行的回车不可少,每一行每一个数字前面有一个空格,最后一个数字之后没有空格,不然算法是对了,但是PE就太忧伤了。
以下是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int A[30][30],x,y,z,w;
char s1[10],s2[10];
int a,b,op,n;
void Find(int c,int &u,int &v){
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(A[i][j]==c){ u=i;v=j;}
}
bool islegal(){
if(a==b)return 0;
Find(a,x,y);
Find(b,z,w);
if(x==z)return 0;
return 1;
}
void restore(int p){
Find(p,x,y);
while(A[x][++y]!=-1){
int t= A[x][y];
A[t][0]=t;
A[x][y]=-1;
}
}
void pile(int a,int b){
Find(a,x,y);
Find(b,z,w);
while(A[z][++w]!=-1);w--;
while(A[x][y]!=-1){
A[z][++w]=A[x][y];
A[x][y++]=-1;
}
}
int main(){
//freopen("1.in","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)A[i][j]=-1;
A[i][0]=i;
}
while(scanf("%s",s1)!=EOF && s1[0]!='q'){
scanf("%d%s%d",&a,s2,&b);
if(islegal()==0)continue;
if(s1[0]=='m')restore(a);
if(s2[1]=='n')restore(b);
pile(a,b);
}
for(int i=0;i<n;i++){
printf("%d:",i);
int k=0;
while(A[i][k]!=-1)
printf(" %d",A[i][k++]);
printf("\n");
}
}
以下是测试数据:
sample input
10
move 6 on 5
move 4 over 8
pile 9 on 0
move 3 on 9
pile 1 on 9
move 1 over 0
pile 7 over 6
move 1 on 8
pile 5 on 3
move 2 on 5
quit
7
move 6 over 5
pile 3 on 0
pile 5 on 1
move 2 on 0
pile 0 on 1
move 0 over 2
move 1 over 1
move 5 over 3
pile 6 over 5
pile 3 over 1
quit
sample output
0: 0 9
1:
2:
3: 3 5 2
4: 4
5:
6: 6
7: 7
8: 8 1
9:
0:
1: 1 0 2 3 5 6
2:
3:
4: 4
5:
6: