UVA101 The Blocks Problem

 The Blocks Problem 

Background

Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AIstudy of planning and robotics (STRIPS) used a block world in which arobot arm performed tasks involving the manipulation of blocks.

In thisproblem you will model a simple block world under certain rules andconstraints. 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 armin how to manipulate blocks that lie on a flat table. Initially thereare
n blocks on the table (numbered from 0 to n-1)with block bi adjacent to block
bi+1for all as shown in the diagram below:

Figure:Initial Blocks World

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 afterreturning any blocks that are stacked on top of blocks a and
    b totheir initial positions.

  • move a over b

    where a and b are block numbers, puts block a onto the top of thestack containing block
    b, after returning any blocks that are stackedon top of block a to their initial positions.
  • pile a onto b

    where a and b are block numbers, moves the pile of blocks consistingof block
    a, and any blocks that are stacked above block a, ontoblock
    b
    . All blocks on top of block b are moved to their initialpositions prior to the pile taking place. The blocks stacked above blocka retain their order when moved.
  • pile a over b

    where a and b are block numbers, puts the pile of blocks consistingof block
    a, and any blocks that are stacked above block a, ontothe top of the stack containing block
    b. The blocks stacked above blocka retain their original order when moved.
  • quit

    terminates manipulations in the block world.

Any command in which a = b or in which a and bare in the same stack of blocks is an illegal command. All illegalcommands should be ignored and should have noaffect on the configuration of blocks.

The Input

The input begins with an integer n on a line by itself representingthe 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, onecommand per line. Yourprogram should process all commands until the
quit command isencountered.

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. Eachoriginal block position numbered
i (where
n is the number of blocks) should appearfollowed immediately by a colon.If there is at least a blockon 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:

Miguel Revilla

2000-04-06

题目的意思是: 有一排过去n个木块,按指令移动木块;

move a over b:

在木块a所在的那一堆木块,把a上面的的木块全部放回原地,然后把a放到b所在那堆木块上面。。

move a onto b:

把a所在的那堆木块,a上面的全部放回原处,b所在的那堆也是一样,把b上面的木块都放回原处,然后把a放到b上面;

pile a over b:

把a所在的那一堆木块,a上面的全部木块包括a ,都移到b所在的那堆木块上面。

pile  a onto b:

把b所在那堆木块,在b上面的木块全都放回原处,并把a所在的那堆木块a上面的全部移到b上面。。

总的来说就是碰到move就要把a上面的全部放回原处。如果碰到 onto 就要把b上面的全部放到原处。

因为move是只移动a一个,所以a上面的要归位,而pile是移一堆,所以不用。

onto是要和b贴在一起,所以b上面的要归位,而over是上方,不需要直接接触,所以不用。。

思路就是用栈来模拟,一开始就是n个栈。每个栈里都是一个元素,然后按照指令移,在这个栈里pop()掉它,在另一个栈里push()进去。。

分四种情况来做移动,每种情况处理方式不一样。要注意如果是一堆移过去,因为还是要按照这个顺序,多以要先把这一堆放到另一个数组,再按顺序pushj进去。

模拟完输出即可。。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string>
#include<stack>
using namespace std;

int main () {
	stack <int> sta[100];
	int t;
	int num[100];
	int res[100];
	string m1,m2;
	int p1,p2;
	cin >> t;
	getchar();
	for( int i = 0; i < t ;i++) {
		sta[i].push(i);
		num[i] = i;

	}
	while(1) {
		cin >> m1;
		if(m1 == "quit")
			break;
		cin >>p1 >>m2 >>p2;
		if (num[p1] == num[p2])
			continue;
		if (m1 == "move" && m2 == "over") {
			for (;sta[num[p1]].top() != p1; ) {
				sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());
				num[sta[num[p1]].top()] = sta[num[p1]].top();
				sta[num[p1]].pop();
			}
			sta[num[p2]].push(p1);
			sta[num[p1]].pop();
			num[p1] = num[p2];
		}
		if (m1 == "pile" && m2 == "over") {
			int k = 0;
			int temp[200];
			for (;sta[num[p1]].top() != p1; ) {
				temp[k++] = sta[num[p1]].top();
				num[sta[num[p1]].top()] = num[p2];
				sta[num[p1]].pop();
			}
			sta[num[p1]].pop();
			temp[k] = p1;
			num[p1] = num[p2];
			for(int w = k ;w >= 0; w--)
				sta[num[p2]].push(temp[w]);
		}
		if (m1 == "move" && m2 == "onto") {
			for (;sta[num[p1]].top() != p1; ) {
				sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());
				num[sta[num[p1]].top()] = sta[num[p1]].top();
				sta[num[p1]].pop();
			}
			for (;sta[num[p2]].top() != p2; ) {
				sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());
				num[sta[num[p2]].top()] = sta[num[p2]].top();
				sta[num[p2]].pop();
			}
			sta[num[p2]].push(sta[num[p1]].top());
			sta[num[p1]].pop();
			num[p1] = num[p2];
		}
		if (m1 == "pile" && m2 == "onto") {
			int k = 0;
			int temp[200];
			for (;sta[num[p1]].top() != p1; ) {
				temp[k++] = sta[num[p1]].top();
				num[sta[num[p1]].top()] = num[p2];
				sta[num[p1]].pop();
			}
			sta[num[p1]].pop();
			temp[k] = p1;
			num[p1] =num[p2];
			for (;sta[num[p2]].top() != p2; ) {
				sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());
				num[sta[num[p2]].top()] = sta[num[p2]].top();
				sta[num[p2]].pop();
			}
			for(int w = k ;w >= 0; w--)
				sta[num[p2]].push(temp[w]);

		}
	}
	int j;
	for(int i = 0;i < t;i++) {
		cout << i <<":";
		for( j = 0 ;!sta[i].empty();j++) {
			res[j] =  sta[i].top();
			sta[i].pop();
		}
		for (j = j -1; j >= 0;j--)
			cout<<" "<<res[j];
		cout << endl;
	}
	return 0;
}

时间: 2024-10-13 19:56:51

UVA101 The Blocks Problem的相关文章

UVA101 HDU1612 POJ1208 The Blocks Problem

问题链接:UVA101 HDU1612 POJ1208 The Blocks Problem. 这是一个模拟题,程序过程都是套路. 程序中用到了STL的容器类vector. 这个程序在UVA和POJ中都AC,可是在HDU中是"Presentation Error". AC通过的C++语言程序如下: /* UVA101 HDU1612 POJ1208 The Blocks Problem */ #include <iostream> #include <string&g

POJ 1208 The Blocks Problem

The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5397   Accepted: 2312 Description Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of

UVA - 101 The Blocks Problem(STL,模拟)

The Blocks Problem Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an e

101 - The Blocks Problem

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=37   The Blocks Problem  Background Many areas of Computer Science use simple, abstract domains for both analytical and empiric

uva 101 The Blocks Problem (模拟)

uva 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 per

uva 101 The Blocks Problem (基本算法-模拟)

 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

The Blocks Problem(vector)

题目链接:http://poj.org/problem?id=1208 The Blocks Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5004   Accepted: 2119 Description Many areas of Computer Science use simple, abstract domains for both analytical and empirical studie

uva101木块问题The Blocks Problem

背景:一看这道题,再看书前面的知识点,我只能说一点不会,于是我将上面的代码在电脑上面打了一遍,这才会一点点新知识的运用,然后我就按照自己的想法,说实话已经参照了书上的一些东西,然后自己去用新知识写代码,当我满心欢喜的去测试的时候,啊,wrong,仔细一读题,才发现自己题意都理解错了. 思路:模拟四条指令即可.由于每个木块堆的高度不确定,所以用vector来保存很合适. 学习:vector就是一个不定长数组.若a是一个vector,可以用a.size()读取他的大小,用a.resize()改变他的

Winter-2-STL-D The Blocks Problem 解题报告及测试数据

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 i