uvaoj-101:小木块
来源:互联网 发布:深圳淘宝学院 编辑:程序博客网 时间:2024/06/03 22:05
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 aren blocks on the table (numbered from 0 ton-1) with blockbi adjacent to blockbi+1 for all as shown in the diagram below:
The valid commands for the robot arm that manipulates blocks are:
- move a ontob
where a andb are block numbers, puts blocka onto blockb after returning any blocks that are stacked on top of blocksa andb to their initial positions.
- move a overb
where a andb are block numbers, puts blocka onto the top of the stack containing blockb, after returning any blocks that are stacked on top of blocka to their initial positions.
- pile a ontob
where a andb are block numbers, moves the pile of blocks consisting of blocka, and any blocks that are stacked above blocka, onto blockb. All blocks on top of blockb are moved to their initial positions prior to the pile taking place. The blocks stacked above blocka retain their order when moved.
- pile a overb
where a andb are block numbers, puts the pile of blocks consisting of blocka, and any blocks that are stacked above blocka, onto the top of the stack containing blockb. The blocks stacked above blocka retain their original order when moved.
- quit
terminates manipulations in the block world.
Any command in whicha =b or in whicha andb 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 integern 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 thequit 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 numberedi ( wheren 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 wheren is the integer on the first line of input).
Sample Input
10move 9 onto 1move 8 over 1move 7 over 1move 6 over 1pile 8 over 6pile 8 over 5move 2 over 1move 4 over 9quit
Sample Output
0: 0 1: 1 9 2 4 2: 3: 3 4: 5: 5 8 7 6 6: 7: 8: 9:
题解:模拟题,刘汝佳书上110页的例题。没错我就是照着刘汝佳书上的代码敲的,因为这道题锻炼的重点是vector的运用。虽然题不难,但是我还是花了很长一段时间才做完,因为一些小细节,代码上我做了一定的注释。大神请右转,和我一样的小菜可以看一下代码贴完我会对今天新掌握的只是做一个小记录;
code:
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
vector <int> pile[maxn];
void find_block(int a,int &p,int &h)//传引用,返回的时候将已经改变的p和h值返回;
{
for(p=0; p<n; p++)//注意不要在循环的时候将p和h重新定义;
{
int len=pile[p].size();//也可以放在循环里,但放在外面能稍微快一点点吧;
for(h=0; h<len; h++)
{
//printf("p == %d\nh == %d\n",p,h);
if(pile[p][h]==a)
return ;//void函数可以这样返回,姿势+1;
}
}
}
{
for(int i=h+1; i<pile[p].size(); i++)
{
int temp=pile[p][i];
pile[temp].push_back(temp);//放回原位置;
}
pile[p].resize(h+1);//重新定义不定长数组大小;
{
for(int i=h; i<pile[p].size(); i++)
{
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);
{
for(int i=0; i<n; i++)
{
printf("%d:",i);
for(int j=0; j<pile[i].size(); j++)
{
printf(" %d",pile[i][j]);
}
printf("\n");
}
}
{
int a,b;
cin>>n;
string s1,s2;
for(int i=0; i<n; i++)
{
pile[i].push_back(i);
//printf("pile[%d][0] == %d\n",i,pile[i][0]);
}
{
int pa,pb,ha,hb;
//cout<<"s1 == "<<s1<<"s2 == "<<s2<<endl;//string不可以用%s打印;
find_block(a,pa,ha);//printf("pa == %d\nha == %d\n",pa,ha);
find_block(b,pb,hb);//printf("pb == %d\nhb == %d\n",pb,hb);
if(pa==pb)
continue;
if(s2=="onto") clear_above(pb,hb);
if(s1=="move") clear_above(pa,ha);
pile_onto(pa,ha,pb);
}
print();
return 0;
}
笔记:
vector's function:
vector <int> vec;
vec.push_back();尾部插入元素;
vec.pop_back();尾部删除元素;
//插入和删除元素的同时数组的长度会随之变化;
vec[];使用下标访问元素;
vec.insert(vec.begin()+i,a);在i+1个元素前边插入a;
vec.erase(vec.begin()+2);删除第三个元素;
vec.size();获取不定长数组大小;
vec.clear();清空不定长数组;
reverse(vec.begin(),vec.end());翻转不定长数组;
sort(vec.begin(),vec.end(),cmp);sort对补丁长数组排序;
vec.resize(n,0);将不定长数组重新初始化为长度为n的每个元素都为0的不定长数组;
vec.resize(n);重新将不定长数组的长度划为n;
- uvaoj-101:小木块
- UVAOJ 101 移动木块
- 数数小木块
- 数数小木块
- 数数小木块
- 数数小木块
- 数数小木块
- 260 数数小木块
- nyoj-数数小木块
- nyoj260数数小木块
- 数数小木块
- NYOJ数数小木块
- 数数小木块
- ACM-数数小木块
- 数数小木块0
- 260 数数小木块
- nyoj260 数数小木块
- 41.数数小木块
- RadioButton和CheckBox的区别,RadioButton&RadioGroup
- Quartz与Spring的整合使用
- iOS之UI学习-UIButton
- Python快速教程
- 迪克斯特拉算法-- Dijkstra's Algorithm
- uvaoj-101:小木块
- Swift与OC混编的方法
- Linux下处理JSON的命令行工具:jq---安装
- C语言中内存分布及程序运行中(BSS段、数据段、代码段、堆栈)
- 华为AnyOffice 2015 HTML5 大赛,可以免费申请ECS机器3个月
- Android 中添加menu菜单
- caffe学习笔记8-- Python solving with LeNet
- c++ 模版函数的定义和实现放在一个文件中
- LeetCode Single Number