-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101 - The Blocks Problem.cpp
141 lines (131 loc) · 4.53 KB
/
101 - The Blocks Problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/******************************************************************************
* ▄██████████▄ ▀█████████▄ ▀██████████▄ ▄████████████▄ ▄██████████▄ *
* ▀▀███ ███▀ ███ ███ ███ ▀██▄ ▀███▀ ▀███▀ █▀ ▄███▀ *
* ███ ███ ███ ██ ███ ███ ▄███▀ *
* ▄███▄▄▄██▀ ▄███▄▄▄██▀ ███ ██▀ ███ ███ ▄███▀ *
* ▀▀███▀▀▀██▄ ▀▀███▀▀▀██▄ ▄███▄▄▄▄▄██▀ ███ ███ ▄███▀ *
* ███ ███ ▀▀███▀▀▀▀▀██▄ ███ ███ ▄███▀ *
* ███ ███ ███ ███ ███ ▄███▄ ▄███▄ ▄███▀ ▄█ *
* ▄▄████▀ ▄█████████▀ ▄███ ███▄ ▀████████████▀ ▀██████████▀ *
* *
* *Don't limit your challenges, challenge your limits. *
******************************************************************************/
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int pos[N];
stack <int> s[N], tmp;
void moveOnto(int src, int dest){
while(s[pos[src]].top() != src){
int t = s[pos[src]].top();
s[t].push(t);
pos[t] = t;
s[pos[src]].pop();
}
while(s[pos[dest]].top() != dest){
int t = s[pos[dest]].top();
s[t].push(t);
pos[t] = t;
s[pos[dest]].pop();
}
s[pos[dest]].pop();
pos[dest] = dest;
s[dest].push(dest);
s[dest].push(s[pos[src]].top());
s[pos[src]].pop();
pos[src] = dest;
}
void moveOver(int src, int dest){
while(s[pos[src]].top() != src){
int t = s[pos[src]].top();
s[t].push(t);
pos[t] = t;
s[pos[src]].pop();
}
s[pos[dest]].push(s[pos[src]].top());
s[pos[src]].pop();
pos[src] = pos[dest];
}
void pileOnto(int src, int dest){
while(s[pos[dest]].top() != dest){
int t = s[pos[dest]].top();
s[t].push(t);
pos[t] = t;
s[pos[dest]].pop();
}
s[pos[dest]].pop();
pos[dest] = dest;
s[dest].push(dest);
while(s[pos[src]].top() != src){
tmp.push(s[pos[src]].top());
s[pos[src]].pop();
}
tmp.push(s[pos[src]].top());
s[pos[src]].pop();
while(!tmp.empty()){
s[pos[dest]].push(tmp.top());
pos[tmp.top()] = pos[dest];
tmp.pop();
}
}
void pileOver(int src, int dest){
while(s[pos[src]].top() != src){
tmp.push(s[pos[src]].top());
s[pos[src]].pop();
}
tmp.push(s[pos[src]].top());
s[pos[src]].pop();
while(!tmp.empty()){
s[pos[dest]].push(tmp.top());
pos[tmp.top()] = pos[dest];
tmp.pop();
}
}
int n, src, dest;
char cmd1[5], cmd2[5];
void init(){
while(scanf("%d",&n)==1){
for(int i = 0; i < n; i++){
s[i].push(i);
pos[i] = i;
}
while(scanf("%s",cmd1)==1){
if(cmd1[0] == 'q') break;
scanf("%d%s%d",&src,cmd2,&dest);
if(src != dest && pos[src] != pos[dest]){
if(cmd1[0] == 'm'){
if(cmd2[1] == 'n') moveOnto(src,dest);
else moveOver(src,dest);
}
else{
if(cmd2[1] == 'n') pileOnto(src,dest);
else pileOver(src,dest);
}
}
}
for(int i = 0; i < n; i++){
printf("%d:",i);
if(s[i].empty()) cout << endl;
else{
while(!s[i].empty()){
tmp.push(s[i].top());
s[i].pop();
}
while(!tmp.empty()){
printf(" %d",tmp.top());
tmp.pop();
}
printf("\n");
}
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
init();
return 0;
}