-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA 101 - The blocks Problem.cpp
More file actions
161 lines (123 loc) · 3.05 KB
/
UVA 101 - The blocks Problem.cpp
File metadata and controls
161 lines (123 loc) · 3.05 KB
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
142
143
144
145
146
147
148
149
150
151
// std::setvbuf(stdout, nullptr, _IOFBF, BUFSIZ); //(will neglect the c's stdio.h property of doing inline flushing with every line of code)
//std::ios_base::sync_with_stdio(false); // (removes the interoperability that happens between C's stdio.h and iostream )
#include <iostream>
//#include<numeric>
//#include<vector>
#include<cmath>
//#include<stack>
//#include <iomanip>
//#include <vector>
#include<bits/stdc++.h>
//#include <bitset>
////#include<iomanip>
//#include <algorithm>
//#include<queue>
////#include<deque>
////#include<numeric>
//#include<set>
//#include<unordered_set>
//#include<map>
////#include<fstream>
//#include<sstream>
////#include <thread>
////#include <chrono>
//#include<cstring>
//#include<string>
//#include <unordered_map>
////#include <thread>
////#include<limits>+++
using namespace std;
int n;
vector<int> ground;
vector<int> linkOnTop;
//check invalidity
// 1 removestack a,b -> ground[a] = 0 -> linkontop[b] =a
//2 removestack a -> linkontop[getlast(b)]=a
// 3 removestack b -> linkontop[b] =a
//4 linkontop[getlast(b)] = a
void getBeForeme(int searchfor){
if(searchfor == -1) return;
for(int i =0;i< n;++i){
if(linkOnTop[i] == searchfor){
linkOnTop[i] =-1;
break;
}
}
}
bool aretheyLinked(int node,int searchfor){
if(linkOnTop[node] == searchfor) return true;
if(linkOnTop[node] ==-1) return false;
return aretheyLinked(linkOnTop[node],searchfor);
}
int getlast(int node){
if (linkOnTop[node] == -1) return node;
return getlast(linkOnTop[node]);
}
void removeStack(int node){
if(node ==-1) return;
ground[node] =node;
int temp = linkOnTop[node];
linkOnTop[node] =-1;
if(temp!=-1)removeStack(temp);
}
bool invalid(int a,int b){
return ((a==b)||aretheyLinked(a,b)||aretheyLinked(b,a));
}
void fun1(int a ,int b){
removeStack(a);
removeStack(linkOnTop[b]);
getBeForeme(a);
ground[a] = -1;
linkOnTop[b] =a;
return;
}
void fun2(int a,int b){
removeStack(a);
getBeForeme(a);
linkOnTop[getlast(b)] = a;
ground[a] =-1;
return;
}
void fun3(int a,int b){
removeStack(linkOnTop[b]);
getBeForeme(a);
linkOnTop[b] =a;
ground[a]=-1;
return;
}
void func4(int a,int b){
getBeForeme(a);
linkOnTop[getlast(b)] = a;
ground[a] =-1;
}
void printer(int node){
if (node == -1) return;
cout<<" "<<node;
printer(linkOnTop[node]);
}
int main(){
cin>>n;
ground.clear();
ground.resize(n);
for(int i=0;i<n;++i)ground[i] =i;
linkOnTop.clear();
linkOnTop.assign(n,-1);
while(true){
string s1,s2;
int a,b;
cin>>s1;
if(s1 == "quit") break;
cin>>a>>s2>>b;
if(invalid(a,b)) continue;
if(s1=="move"&& s2=="onto") fun1(a,b);
if(s1=="move"&& s2=="over") fun2(a,b);
if(s1=="pile"&& s2=="onto") fun3(a,b);
if(s1=="pile"&& s2=="over") func4(a,b);
}
for(int i =0;i<n;++i){
cout<<i<<":";
if(ground[i] !=-1) printer(i);
cout<<'\n';
}
return 0;
}