题目链接:
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;
![](/Images/OutliningIndicators/None.gif)
int n;
vector<int> block[30];
![](/Images/OutliningIndicators/None.gif)
void move_onto(int ia, int a, int ib, int b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
tmp = block[ib].back();
while(tmp != b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
block[ib].pop_back();
block[tmp].push_back(tmp);
tmp = block[ib].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
![](/Images/OutliningIndicators/None.gif)
void move_over(int ia, int a, int ib, int b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int tmp;
tmp = block[ia].back();
while(tmp != a)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
block[ia].pop_back();
block[tmp].push_back(tmp);
tmp = block[ia].back();
}
block[ia].pop_back();
block[ib].push_back(a);
}
![](/Images/OutliningIndicators/None.gif)
void pile_onto(int ia, int a, int ib, int b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int tmp;
tmp = block[ib].back();
while(tmp != b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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());
}
![](/Images/OutliningIndicators/None.gif)
void pile_over(int ia, int a, int ib, int b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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());
}
![](/Images/OutliningIndicators/None.gif)
void manipulate(const string &s)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(size_t j = 0; j != block[i].size(); ++j)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cout << i << ':';
for(size_t j = 0; j != block[i].size(); ++j)
cout << ' ' << block[i][j];
cout << endl;
}
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted on 2011-11-19 00:04
wuxu 阅读(320)
评论(0) 编辑 收藏 引用 所属分类:
模拟