题目就是根据题意进行四个操作 。。复制一下别人的翻译:
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.
- a和b都是积木的编号,先将a和b上面所有的积木都放回原处,再将a放在b上。
- 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.
- a和b都是积木的编号,先将a上面所有的积木放回原处,再将a放在b上。(b上原有积木不动)
- 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.
- a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b上。在移动前要先将b上面所有的积极都放回原处。移动的一摞积木要保持原来的顺序不变。
- 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.
- a和b都是积木的编号,将a和其上面所有的积极组成的一摞整体移动到b所在一摞积木的最上面一个积木上。移动的一摞积木要保持原来的顺序不变。
- 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.
当a = b或a和b处在同一摞时,任何企图操作a和b的命令都是非法的。所有非法的命令都要忽略,且不能对当前积木的状态产生作用。
代码比较恶心。。。就是纯粹的模拟
题目:
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 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 i ( 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 #include <iostream> 2 #include <vector> 3 #include <deque> 4 #include <string> 5 #include <string.h> 6 #include <cstdio> 7 using namespace std; 8 9 int blocks[30][30]; 10 int height[30]; 11 int bheight[30]; 12 int pos[30]; 13 int num; 14 15 int comprehend(char* cmd) 16 { 17 if(strstr(cmd,"move")) 18 { 19 if(strstr(cmd,"onto"))return 1; 20 else if(strstr(cmd,"over"))return 2; 21 } 22 else 23 { 24 if(strstr(cmd,"onto")) return 3; 25 else return 4; 26 } 27 28 } 29 30 void run(char* cmd) 31 { 32 int cmdkind = comprehend(cmd); 33 int from,to; 34 char rub[100]; 35 if(cmdkind==1) 36 { 37 sscanf(cmd,"%s %d %s %d",rub,&from,rub,&to); 38 39 40 41 int px = pos[from]; 42 int py = height[from]; 43 int ptx = pos[to]; 44 int pty = height[to]; 45 if(from==to||px==ptx)return; 46 for(int i= py+1;i<=bheight[px];i++) 47 { 48 int now = blocks[px][i]; 49 blocks[now][0]=now; 50 bheight[now]=0; 51 pos[now] = now; 52 height[now]=0; 53 } 54 bheight[px]=py; 55 56 57 58 for(int i=pty+1;i<=bheight[ptx];i++) 59 { 60 int now = blocks[ptx][i]; 61 blocks[now][0]=now; 62 bheight[now]=0; 63 64 pos[now] = now; 65 height[now]=0; 66 } 67 68 // cout<<"px"<<px<<"py"<<py<<"ptx"<<ptx<<"pty"<<pty<<endl; 69 bheight[ptx]=pty+1; 70 blocks[ptx][pty+1] = from; 71 72 bheight[px]--; 73 pos[from]=ptx; 74 height[from]=pty+1; 75 76 } 77 if(cmdkind==2) 78 { 79 sscanf(cmd,"%s %d %s %d",rub,&from,rub,&to); 80 81 82 int px = pos[from]; 83 int py = height[from]; 84 int ptx = pos[to]; 85 int pty = bheight[ptx]; 86 if(from==to||px==ptx)return ; 87 for(int i= py+1;i<=bheight[px];i++) 88 { 89 int now = blocks[px][i]; 90 blocks[now][0]=now; 91 bheight[now]=0; 92 pos[now] = now; 93 height[now]=0; 94 } 95 bheight[px]=py; 96 97 98 99 //cout<<"px"<<px<<"py"<<py<<"ptx"<<ptx<<"pty"<<pty<<endl; 100 bheight[ptx]=pty+1; 101 blocks[ptx][pty+1] = from; 102 103 bheight[px]--; 104 pos[from]=ptx; 105 height[from]=pty+1; 106 107 108 109 } 110 111 if(cmdkind==3) 112 { 113 sscanf(cmd,"%s %d %s %d",rub,&from,rub,&to); 114 115 int px = pos[from]; 116 int py = height[from]; 117 118 119 int ptx = pos[to]; 120 int pty = height[to]; 121 122 if(from==to||px==ptx)return; 123 124 for(int i=pty+1;i<=bheight[ptx];i++) 125 { 126 int now = blocks[ptx][i]; 127 blocks[now][0]=now; 128 bheight[now]=0; 129 130 pos[now] = now; 131 height[now]=0; 132 } 133 bheight[ptx] = pty; 134 135 for(int i=py;i<=bheight[px];i++) 136 { 137 int now = blocks[px][i]; 138 blocks[ptx][++pty] = now; 139 pos[now] = ptx; 140 height[now] = pty; 141 } 142 bheight[px] = py-1; 143 144 // cout<<"px"<<px<<"py"<<py<<"ptx"<<ptx<<"pty"<<pty<<endl; 145 bheight[ptx]=pty; 146 } 147 148 149 150 if(cmdkind==4) 151 { 152 sscanf(cmd,"%s %d %s %d",rub,&from,rub,&to); 153 154 155 int px = pos[from]; 156 int py = height[from]; 157 int ptx = pos[to]; 158 int pty = bheight[ptx]; 159 if(from==to||px==ptx)return ; 160 for(int i= py;i<=bheight[px];i++) 161 { 162 int now = blocks[px][i]; 163 blocks[ptx][++pty]=now; 164 pos[now] = ptx; 165 height[now]=pty; 166 } 167 bheight[px]=py-1; 168 169 // cout<<"px"<<px<<"py"<<py<<"ptx"<<ptx<<"pty"<<pty<<endl; 170 bheight[ptx]=pty; 171 } 172 173 } 174 void print() 175 { 176 for(int i=0;i<num;i++) 177 { 178 printf("%d:",i); 179 for(int j=0;j<=bheight[i];j++) 180 { 181 printf(" %d",blocks[i][j]); 182 } 183 printf(" "); 184 } 185 186 } 187 188 189 int main() 190 { 191 cin>>num; 192 193 char cmd[100]; 194 195 memset(height,0,sizeof(height)); 196 for(int i=0;i<num;i++) 197 { 198 blocks[i][0]=i; 199 bheight[i]=0; 200 height[i]=0; 201 pos[i]=i; 202 } 203 204 205 getchar(); 206 // print(); 207 while(gets(cmd) && !strstr(cmd,"quit")) 208 { 209 run(cmd); 210 211 } 212 print(); 213 214 return 0; 215 }