当前位置: 代码迷 >> 综合 >> Uva 101 The Blocks Problem
  详细解决方案

Uva 101 The Blocks Problem

热度:79   发布时间:2023-11-20 06:07:49.0
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 30;
vector<int> pile[maxn];
int n;
//查找a所在的pile和height;
void Find_pile(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_pile(int p, int h)
{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);
}
void pile_onto(int a, int h, int b)
{for (int i = h; i < pile[a].size(); i++){pile[b].push_back(pile[a][i]);}pile[a].resize(h);
}
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]);cout << endl;}}
int main()
{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){int pa, ha, pb, hb;Find_pile(a, pa, ha);Find_pile(b, pb, hb);if(pa == pb) continue;if(s2 == "onto") clear_pile(pb, hb);if(s1 == "move") clear_pile(pa, ha);pile_onto(pa, ha, pb);}print();return 0;
}

  相关解决方案