Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Plain Text by LLL ( 11 years ago )
#include <cassert>
#include <cstdlib>

#include <exception>
#include <iostream>
#include <vector>

using namespace std;

struct State {
 int move = 0;
 vector<int> start;
 vector<int> tmp;
 vector<int> finish;

 explicit State(int n) {
  while (n) {
   start.push_back(n);
   --n;
  }
 }

 static void PrintVector(const vector<int>& v) {
  cout << "[";
  for (int i : v)
   cout << i << ",";
  cout << "]";
 }

 void Output() const {
  cout << move << ":" << " ";
  PrintVector(start);
  cout << " ";
  PrintVector(tmp);
  cout << " ";
  PrintVector(finish);
  cout << endl;
 }
};

void MoveTop(int sz, State& st, vector<int>& from, vector<int>& tmp, vector<int>& to) {
 if (sz == 1) {
  st.Output();

  assert(!(from.empty() || (!to.empty() && to.back() < from.back())));

  ++st.move;
  to.push_back(from.back());
  from.pop_back();
 } else {
  MoveTop(sz - 1, st, from, to, tmp);
  MoveTop(1, st, from, tmp, to);
  MoveTop(sz - 1, st, tmp, from, to);
 }
}

void Solve(State& st) {
 int move = 0;
 size_t tgt = st.start.size();

 while (st.finish.size() < tgt) {
  MoveTop(tgt, st, st.start, st.tmp, st.finish);
 }

 st.Output();
}

int main() {
 int n = 0;
 cin >> n;
 State state(n);

 Solve(state);

 return 0;
}

 

Revise this Paste

Parent: 76845
Your Name: Code Language: