#include <iostream>
#include <vector>
using namespace std;
vector <vector <int> > moveToRecovery(int woodNumber, vector <vector <int> > locationArray, bool isRemoveSelf) {
for (int woodLocation = 0; woodLocation < locationArray.size(); woodLocation++) {
bool isRecovery = false;
for (int compareWoodNumber = 0; compareWoodNumber < locationArray[woodLocation].size(); compareWoodNumber++) {
if (locationArray[woodLocation][compareWoodNumber] == woodNumber) {
isRecovery = true;
if (isRemoveSelf) {
locationArray[woodLocation].erase(locationArray[woodLocation].begin() + compareWoodNumber);
}
}
if (isRecovery && locationArray[woodLocation][compareWoodNumber] != woodNumber) {
locationArray[locationArray[woodLocation][compareWoodNumber]].push_back(locationArray[woodLocation][compareWoodNumber]);
locationArray[woodLocation].erase(locationArray[woodLocation].begin() + compareWoodNumber);
}
}
}
return locationArray;
}
vector <vector <int> > moveToALL(int moveWoodNumber, int toWoodNumber, vector <vector <int> > locationArray, bool isMoveToRecovery) {
if (isMoveToRecovery) {
locationArray = moveToRecovery(toWoodNumber, locationArray, false);
}
vector <int> cache;
int findtToWoodNumberI = 0;
bool isStartMove = false;
for (int woodLocation = 0; woodLocation < locationArray.size(); woodLocation++) {
if (isStartMove) {
isStartMove = false;
}
for (int compareWoodNumber = 0; compareWoodNumber < locationArray[woodLocation].size(); compareWoodNumber++) {
if (locationArray[woodLocation][compareWoodNumber] == toWoodNumber) {
findtToWoodNumberI = woodLocation;
}
if (locationArray[woodLocation][compareWoodNumber] == moveWoodNumber) {
isStartMove = true;
}
if (isStartMove) {
cache.push_back(locationArray[woodLocation][compareWoodNumber]);
locationArray[woodLocation].erase(locationArray[woodLocation].begin() + compareWoodNumber);
compareWoodNumber--;
}
}
}
for (int woodLocation = 0; woodLocation < cache.size(); woodLocation++) {
locationArray[findtToWoodNumberI].push_back(cache[woodLocation]);
}
return locationArray;
}
vector <vector <int> > moveToWoodNumber(int moveWoodNumber, int toWoodNumber, vector <vector <int> > locationArray) {
for (int woodLocation = 0; woodLocation < locationArray.size(); woodLocation++) {
for (int compareWoodNumber = 0; compareWoodNumber < locationArray[woodLocation].size(); compareWoodNumber++) {
if (locationArray[woodLocation][compareWoodNumber] == toWoodNumber) {
locationArray[woodLocation].push_back(moveWoodNumber);
}
}
}
return locationArray;
}
int main() {
int cinNumber;
while (cin >> cinNumber) {
//[積木的位置] 0 ~ cinNumber-1
vector <vector <int> > locationArray(cinNumber);
//[積木的位置] 對應初始積木編號 0 ~ cinNumber-1,每個積木編號都是一個 array。
for (int runCinNumber = 0; runCinNumber < cinNumber; runCinNumber++) {
vector <int> woodNumber(1, runCinNumber);
locationArray[runCinNumber] = woodNumber;
}
string woodAction, woodNarrative;
int woodNumberAction, woodNumberNarrative;
while (cin >> woodAction) {
if (woodAction == "quit")
break;
cin >> woodNumberAction >> woodNarrative >> woodNumberNarrative;
if (woodAction == "move" && woodNarrative == "onto") {
locationArray = moveToRecovery(woodNumberNarrative, locationArray, false);
locationArray = moveToRecovery(woodNumberAction, locationArray, true);
locationArray = moveToWoodNumber(woodNumberAction, woodNumberNarrative, locationArray);
}
else if (woodAction == "move" && woodNarrative == "over") {
locationArray = moveToRecovery(woodNumberAction, locationArray, true);
locationArray = moveToWoodNumber(woodNumberAction, woodNumberNarrative, locationArray);
}
else if (woodAction == "pile" && woodNarrative == "onto") {
locationArray = moveToALL(woodNumberAction, woodNumberNarrative, locationArray, true);
}
else if (woodAction == "pile" && woodNarrative == "over") {
locationArray = moveToALL(woodNumberAction, woodNumberNarrative, locationArray, false);
}
}
//輸出
for (int woodLocation = 0; woodLocation < locationArray.size(); woodLocation++) {
cout << woodLocation << ":";
for (int woodNumber = 0; woodNumber < locationArray[woodLocation].size(); woodNumber++) {
cout << " " << locationArray[woodLocation][woodNumber];
}
cout << endl;
}
}
return 0;
}
參考看看,應該還可以再化簡。 :D