Skip to content

Instantly share code, notes, and snippets.

@jk13o3lll
Created November 13, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jk13o3lll/833d4376c8b0c4acd98c85c10a58a0fb to your computer and use it in GitHub Desktop.
Save jk13o3lll/833d4376c8b0c4acd98c85c10a58a0fb to your computer and use it in GitHub Desktop.
[UVa] 101 - The Blocks Problem
#include <stdio.h>
#include <string.h>
#include <stack>
using std::stack;
#define N 25
int pos[N];
stack<int> pile[N];
void reset_until(int a){
for(int top = pile[pos[a]].top(); top != a; top = pile[pos[a]].top()){
pile[pos[a]].pop();
pile[top].push(top);
pos[top] = top;
}
}
void pile_over(int a, int b){
int top;
stack<int> buffer;
do{
top = pile[pos[a]].top();
buffer.push(top);
pile[pos[a]].pop();
}while(top != a);
while(!buffer.empty()){
pile[pos[b]].push(buffer.top());
pos[buffer.top()] = pos[b];
buffer.pop();
}
}
int main(){
int n, i, a, b;
char cmd1[10], cmd2[10];
stack<int> tmp;
while(scanf("%d", &n) != EOF){
for(i = 0; i < n; ++i){
pos[i] = i;
while(!pile[i].empty())
pile[i].pop();
pile[i].push(i);
}
while(scanf("%s", cmd1) != EOF){
if(strcmp(cmd1, "quit") == 0)
break;
scanf("%d%s%d", &a, cmd2, &b);
if(pos[a] == pos[b])
continue;
if(strcmp(cmd1, "move") == 0)
reset_until(a);
if(strcmp(cmd2, "onto") == 0)
reset_until(b);
pile_over(a, b);
}
while(!tmp.empty())
tmp.pop();
for(i = 0; i < n; ++i){
while(!pile[i].empty()){
tmp.push(pile[i].top());
pile[i].pop();
}
printf("%d:", i);
while(!tmp.empty()){
printf(" %d", tmp.top());
tmp.pop();
}
putchar('\n');
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment