题目链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=37
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
#include <cstddef>
using namespace std;
int n;
vector<int> block[30];
void move_onto(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
tmp = block[ib].back();
while(tmp != b)
{
block[ib].pop_back();
block[tmp].push_back(tmp);
tmp = block[ib].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
void move_over(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
void pile_onto(int ia, int a, int ib, int b)
{
int tmp;
tmp = block[ib].back();
while(tmp != b)
{
block[ib].pop_back();
block[tmp].push_back(tmp);
tmp = block[ib].back();
}
vector<int>::iterator it;
it = find(block[ia].begin(), block[ia].end(), a);
block[ib].insert(block[ib].end(), it, block[ia].end());
block[ia].erase(it, block[ia].end());
}
void pile_over(int ia, int a, int ib, int b)
{
vector<int>::iterator it;
it = find(block[ia].begin(), block[ia].end(), a);
block[ib].insert(block[ib].end(), it, block[ia].end());
block[ia].erase(it, block[ia].end());
}
void manipulate(const string &s)
{
string s1, s2;
int a, b, ia, ib;
stringstream str(s);
str >> s1 >> a >> s2 >> b;
if(a == b) return;
for(int i = 0; i < n; ++i)
{
for(size_t j = 0; j != block[i].size(); ++j)
{
if(block[i][j] == a) ia = i;
if(block[i][j] == b) ib = i;
}
}
if(ia == ib) return;
if(s1 == "move" && s2 == "onto") move_onto(ia, a, ib, b);
else if(s1 == "move" && s2 == "over") move_over(ia, a, ib, b);
else if(s1 == "pile" && s2 == "onto") pile_onto(ia, a, ib, b);
else pile_over(ia, a, ib, b);
}
int main()
{
string s;
cin >> n;
for(int i = 0; i < n; ++i)
block[i].push_back(i);
cin.ignore();
while(getline(cin, s) && s != "quit")
manipulate(s);
for(int i = 0; i < n; ++i)
{
cout << i << ':';
for(size_t j = 0; j != block[i].size(); ++j)
cout << ' ' << block[i][j];
cout << endl;
}
return 0;
}
posted on 2011-11-19 00:04
wuxu 阅读(327)
评论(0) 编辑 收藏 引用 所属分类:
模拟