例题 5-2 木块问题 (The Blocks Problem) UVa 101
2017-08-24 22:09
435 查看
题目:给你n个方块,有四种操作:
1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;
2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;
3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面;
4.pile a over b,把a和a上面的方块整体放到b所在堆的上面。
仔细分析一下就会发现 其实只有两种操作 一个是 清空 a或b或(a和b) 上面的木块,二是 pile[a] to pile[b](单独的a或者b也可以看做pile);
实现:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn];
void find_block(int a, int &p, int &h)//找到当前的木块所在的位置和高度
{
for(p = 0; p < n; p++ ){
for(h = 0; h < pile[p].size(); h++ ){
if(pile[p][h] == a)
return;
}
}
}
void clear_above(int p, int h)//把a/b上方的木块全部归位 注意:这里的h从0开始 所以 当前位置的元素个数为最大h+1
{
for(int i = h+1; i < pile[p].size(); i++ ){
int b = pile[p][i];
pile[b].push_back(b);
}
pile[p].resize(h+1);//修改此时a/b的大小
}
void pile_onto(int p, int h, int p2)//把 a及a以上 整体移到b上(b上面是否归位取决于指令)
{
for(int i = h; i < pile[p].size(); i++ ){
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);//修改此时的a的大小
}
void print()
{
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 main()
{
// freopen("C:\\Users\\zhangwei\\Desktop\\input.txt","r",stdin);
cin >> n;
string s1, s2;
int a, b;
for(int i = 0; i < n; i++ ) pile[i].push_back(i);
while(cin >> s1 >> a >> s2 >> b){
if(s1 == "quit")
break;
int pa, pb, ha, hb;
find_block(a, pa, ha);//传递引用
find_block(b, pb, hb);
if(pa == pb) continue;//错误指令
if(s1 == "move") clear_above(pa, ha);
if(s2 == "onto") clear_above(pb, hb);
pile_onto(pa, ha, pb);
}
print();
return 0;
}
1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;
2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;
3.pile a onto b,把b上面的放回原来位置,然后把a和a上面的方块整体放到b上面;
4.pile a over b,把a和a上面的方块整体放到b所在堆的上面。
仔细分析一下就会发现 其实只有两种操作 一个是 清空 a或b或(a和b) 上面的木块,二是 pile[a] to pile[b](单独的a或者b也可以看做pile);
实现:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn];
void find_block(int a, int &p, int &h)//找到当前的木块所在的位置和高度
{
for(p = 0; p < n; p++ ){
for(h = 0; h < pile[p].size(); h++ ){
if(pile[p][h] == a)
return;
}
}
}
void clear_above(int p, int h)//把a/b上方的木块全部归位 注意:这里的h从0开始 所以 当前位置的元素个数为最大h+1
{
for(int i = h+1; i < pile[p].size(); i++ ){
int b = pile[p][i];
pile[b].push_back(b);
}
pile[p].resize(h+1);//修改此时a/b的大小
}
void pile_onto(int p, int h, int p2)//把 a及a以上 整体移到b上(b上面是否归位取决于指令)
{
for(int i = h; i < pile[p].size(); i++ ){
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);//修改此时的a的大小
}
void print()
{
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 main()
{
// freopen("C:\\Users\\zhangwei\\Desktop\\input.txt","r",stdin);
cin >> n;
string s1, s2;
int a, b;
for(int i = 0; i < n; i++ ) pile[i].push_back(i);
while(cin >> s1 >> a >> s2 >> b){
if(s1 == "quit")
break;
int pa, pb, ha, hb;
find_block(a, pa, ha);//传递引用
find_block(b, pb, hb);
if(pa == pb) continue;//错误指令
if(s1 == "move") clear_above(pa, ha);
if(s2 == "onto") clear_above(pb, hb);
pile_onto(pa, ha, pb);
}
print();
return 0;
}
相关文章推荐
- 《算法竞赛入门经典2ndEdition 》例题5-2 木块问题(The Blocks Problem, Uva101)
- uva 101 木块问题 The Blocks Problem
- UVa OJ The Blocks Problem 木块问题 101
- 木块问题(The Blocks Problem, UVa 101)
- 例题:木块问题(UVa 101)
- 算法竞赛入门经典第五章例题5-2 The Blocks Problem UVA - 101
- Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector
- 例题5-2 木块问题 UVa101
- Uva 101:The Blocks Problem(木块问题)
- uva101木块问题The Blocks Problem
- 例题5-2 UVA 101 The Blocks Problems 木块问题
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
- C++ 不定长数组vector 例5-2木块问题(The Blocks Problem,UVa 101)
- UVA 101 (暑期-线性表-D-The Blocks Problem)
- The Blocks Problem UVA 101 模拟
- 木块问题(vector)-UVa 101
- 紫书章五例二 木块问题 UVA 101(vector)
- UVA101木块问题
- UVA 101 The blocks problem 例题整理