/* @JUDGE_ID: 8962YR 101 C "simple test" */ /* Problem 101 Robot Description: http://acm.uva.es/problemset/v1/101.html Programmer: Kejian */ #include #include #define ONLINE_JUDGE 1 // since it is finite # of n // stack implement is enough // max = # of n /** stack for storage of each array */ class Stack{ private: int* array; int top; public: Stack(){ top = 0; } Stack(int n); void initialize(int n); void push(int k); int pop() ; int length(); int isEmpty(); int peek(); void printStack(); int isExist(int x); void printStacktoFile(ofstream& out); ~Stack() { delete[] array; } }; Stack::Stack(int n){ top =0; array = new int[n]; } void Stack::initialize(int n){ top = 0; array = new int[n]; } void Stack::push(int k){ array[top] = k; top++; } int Stack::pop() { return array[--top]; } int Stack::isEmpty(){ if(top<=0) return 1; else return 0; } int Stack::length(){ return top; } int Stack::peek(){ if(!isEmpty()) return array[top-1]; return -1; } void Stack::printStack(){ int i=0; while(i0){ tmp = st[ito].peek(); if(tmp == to ) break; tmp = st[ito].pop(); st[tmp].push(tmp); } } /* move all a to temporary stack, then pop and push on to pop off all the value on top of from */ Stack ts(n); while(!st[ifrom].isEmpty()){ if(st[ifrom].peek()==from){ ts.push(st[ifrom].pop()); break; } ts.push(st[ifrom].pop()); } /* push it back to destination stack */ while(!ts.isEmpty()){ st[ito].push(ts.pop()); } } void Robot::pileOver(int from, int to){ int ifrom = findA(from); int ito = findA(to); if(ifrom==ito) return; int tmp; /*move all a to temporary stack, then pop and push on to pop off all the value on top of from */ Stack ts(n); while(!st[ifrom].isEmpty()){ if(st[ifrom].peek()==from){ ts.push(st[ifrom].pop()); break; } ts.push(st[ifrom].pop()); } /* push it back to destination stack */ while(!ts.isEmpty()){ st[ito].push(ts.pop()); } } void Robot::setN(int i){ n=i; } void Robot::moveOnto(int from, int to){ int ifrom = findA(from); int ito = findA(to); if(ifrom==ito) return; int tmp; // pop off all the value on top of from if(!st[ifrom].isEmpty() && st[ifrom].peek() != from){ // for stack a do{ tmp = st[ifrom].peek(); if(tmp == from) break; tmp = st[ifrom].pop(); st[tmp].push(tmp); }while(tmp!=from); } // pop off all the value on the top of to if(!st[ito].isEmpty() && st[ito].peek() != to){ do{ tmp = st[ito].peek(); if(tmp == to ) break; tmp = st[ito].pop(); st[tmp].push(tmp); }while(st[ito].length()>1); } // then move a onto b st[ito].push(st[ifrom].pop()); } void Robot::setAction(int m, int f, int t){ mode = m; if(mode == 1) moveOnto(f, t); else if(mode == 2) moveOver(f, t); else if(mode == 3) pileOnto(f, t); else if(mode == 4) pileOver(f, t); } #include #include #include #include using namespace std; main() { int number; Robot rob(10); string action1; string action2; int from; int to; #ifndef ONLINE_JUDGE ifstream fin("myprog.in"); ofstream fout("myprog.out"); fin>>number; /* command input */ while(!fin.fail()){ fin>>action1; if(action1=="quit") break; fin>>from; fin>>action2; fin>>to; if(action1=="move"&&action2=="onto") rob.setAction(1,from,to); else if(action1=="move"&&action2=="over") rob.setAction(2,from,to); else if(action1=="pile"&&action2=="onto") rob.setAction(3,from,to); else if(action1=="pile"&&action2=="over") rob.setAction(4,from,to); } rob.printFile(fout); fin.close(); fout.close(); return; #endif scanf("%d",&number); string action; string fromstr; string tostr; char s1[10]; char s2[10]; while(true){ // why next time can not get input? scanf("%s",s1); if(s1[0] == 'q') exit(0); int i = scanf("%d %s %d",&from,s2, &to); action1.assign(s1); action2.assign(s2); if(action1=="quit") break; if(action1=="move"&&action2=="onto") rob.setAction(1,from,to); else if(action1=="move"&&action2=="over") rob.setAction(2,from,to); else if(action1=="pile"&&action2=="onto") rob.setAction(3,from,to); else if(action1=="pile"&&action2=="over") rob.setAction(4,from,to); else cout<<"error in command"; rob.print(); action1=""; action2=""; } }