Problem Links:
poj1208, uva00101,Problem:
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 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
![]() |
The valid commands for the robot arm that manipulates blocks are:
- move a onto bwhere 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 bwhere 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 bwhere 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 bwhere 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.
- quitterminates manipulations in the block world.
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 (
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
Solution:
Stack simulation;
PS: 1st, check if a == b;
2nd, check if a and b are in the same stack.
Source Code:
//Fri Apr 15 01:53:13 CDT 2011#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
vector<vector<int> > block;
void init(int N) {
block.clear();
for (int i = 0; i < N; i++)
block.push_back(vector<int> (1, i));
// for (int i = 0; i < N; i++)
// for (int j = 0; j < block[i].size(); j++)
// cout << block[i][j] << endl;
}
pair<pair<int, int> , pair<int, int> > findPosition(int a, int b, int N) {
int x1, y1, x2, y2;
bool flag1 = true;
bool flag2 = true;
for (int i = 0; i < N; i++) {
for (int j = 0; j < block[i].size(); j++) {
if (block[i][j] == a && flag1) {
x1 = i;
y1 = j;
flag1 = false;
}
if (block[i][j] == b && flag2) {
x2 = i;
y2 = j;
flag2 = false;
}
if (!flag1 && !flag2)
break;
}
}
// cout << x1 << y1 << endl;
// cout << x2 << y2 << endl;
return make_pair(make_pair(x1, y1), make_pair(x2, y2));
}
void moveOnto(int a, int b, int N) {
if (a == b)
return;
pair<pair<int, int> , pair<int, int> > tmp = findPosition(a, b, N);
if (tmp.first.first == tmp.second.first)
return;
while (block[tmp.first.first].empty() == false
&& block[tmp.first.first].back() != a) {
block[block[tmp.first.first].back()].push_back(
block[tmp.first.first].back());
block[tmp.first.first].pop_back();
}
while (block[tmp.second.first].empty() == false
&& block[tmp.second.first].back() != b) {
block[block[tmp.second.first].back()].push_back(
block[tmp.second.first].back());
block[tmp.second.first].pop_back();
}
block[tmp.first.first].pop_back();
block[tmp.second.first].push_back(a);
}
void moveOver(int a, int b, int N) {
if (a == b)
return;
pair<pair<int, int> , pair<int, int> > tmp = findPosition(a, b, N);
if (tmp.first.first == tmp.second.first)
return;
while (block[tmp.first.first].empty() == false
&& block[tmp.first.first].back() != a) {
block[block[tmp.first.first].back()].push_back(
block[tmp.first.first].back());
block[tmp.first.first].pop_back();
}
block[tmp.first.first].pop_back();
block[tmp.second.first].push_back(a);
}
void pileOnto(int a, int b, int N) {
if (a == b)
return;
pair<pair<int, int> , pair<int, int> > tmp = findPosition(a, b, N);
if (tmp.first.first == tmp.second.first)
return;
while (block[tmp.second.first].empty() == false
&& block[tmp.second.first].back() != b) {
block[block[tmp.second.first].back()].push_back(
block[tmp.second.first].back());
block[tmp.second.first].pop_back();
}
block[tmp.second.first].push_back(a);
vector<int> temp;
while (block[tmp.first.first].empty() == false
&& block[tmp.first.first].back() != a) {
temp.push_back(block[tmp.first.first].back());
block[tmp.first.first].pop_back();
}
while (temp.empty() == false) {
block[tmp.second.first].push_back(temp.back());
temp.pop_back();
}
block[tmp.first.first].pop_back();
}
void pileOver(int a, int b, int N) {
if (a == b)
return;
pair<pair<int, int> , pair<int, int> > tmp = findPosition(a, b, N);
if (tmp.first.first == tmp.second.first)
return;
block[tmp.second.first].push_back(a);
vector<int> temp;
while (block[tmp.first.first].empty() == false
&& block[tmp.first.first].back() != a) {
temp.push_back(block[tmp.first.first].back());
block[tmp.first.first].pop_back();
}
while (temp.empty() == false) {
block[tmp.second.first].push_back(temp.back());
temp.pop_back();
}
block[tmp.first.first].pop_back();
//Version 2;
// for (int i = tmp.first.second; i < block[tmp.first.first].size(); i++)
// block[tmp.second.first].push_back(block[tmp.first.first][i]);
// block[tmp.first.first].erase(block[tmp.first.first].begin()
// + tmp.first.second, block[tmp.first.first].end());
}
void print(int N) {
for (int i = 0; i < N; i++) {
cout << i << ":";
for (int j = 0; j < block[i].size(); j++)
cout << " " << block[i][j];
cout << endl;
}
}
void solve(int N) {
string verb;
int a, b;
string s;
while (cin >> verb && verb != "quit") {
cin >> a >> s >> b;
// cout << verb << a << s << b << endl;
if (verb == "move") {
if (s == "onto")
moveOnto(a, b, N);
else
moveOver(a, b, N);
} else {
if (s == "onto")
pileOnto(a, b, N);
else
pileOver(a, b, N);
}
// print(N);
}
}
int main(int argc, char* argv[]) {
// freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int N;
cin >> N;
init(N);
solve(N);
print(N);
//fclose(stdin);
//fclose(stdout);
return 0;
}
No comments :
Post a Comment