您的位置:首页 > 其它

The Blocks Problem-UVA-101

2018-02-09 10:08 423 查看
题意:状态模拟题,vector的用法
AC代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn];

//找到木块a所在的pile和height,以引用的形式返回调用者
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;
}

//把第p堆高度为h的木块上方的所有木块移回原位
void clear_above(int p, int h) {
for (int i = h + 1; i < pile[p].size(); i++) {
int m = pile[p][i];
pile[m].push_back(m);
}
pile[p].resize(h + 1);
}

//把第p堆高度为h及其上方的木块整体移动到p2堆的顶部
void pile_onto(int p, int h, int p2) {
for (int i = h; i < pile[p].size(); i++)
pile[p2].push_back(pile[p][i]);
pile[p].resize(h);
return;
}

void print() {
for (int i = 0; i < n; i++) {
cout << i << ":";
for (auto &j: pile[i])
cout << " " << j;
cout << endl;
}
return;
}

int main() {
int a, b;
cin >> n;
string s1, s2;
for (int i = 0; i < n; i++)
pile[i].push_back(i);
while (cin >> s1) {
if (s1[0] == 'q')
break;
cin >> a >> s2 >> b;
int pa, pb, ha, hb;
find_block(a, pa, ha);//函数引用返回
find_block(b, 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM STL