-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrailroad-stack.cpp
More file actions
161 lines (159 loc) · 2.67 KB
/
railroad-stack.cpp
File metadata and controls
161 lines (159 loc) · 2.67 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
152
153
154
155
156
157
158
159
160
161
#include<iostream>
using namespace std;
int length=0;
const int size=9;
const int z=3;
int a[size]={5,8,1,2,4,7,9,6,3};
int b[size]={0,0,0,0,0,0,0,0,0};
class Stack{
public:
int s[z];
int start;
Stack()
{
start=-1;
}
void push(int x);
int pop();
int peak();
bool is_full();
bool is_empty();
};
int Stack::peak()
{
return s[start];
}
bool Stack::is_empty()
{
if(start==-1)
{
return true;
}
return false;
}
bool Stack::is_full()
{
if(start==z)
{
return true;
}
return false;
}
void Stack::push(int x)
{
if(!is_full())
{
start++;
s[start]=x;
}
}
int Stack::pop()
{
if(is_empty()==0)
{
return s[start--];
}
else
{
return -1;
}
}
void print()
{
for(int i=0;i<size;i++)
{
cout<<b[i]<<" ";
}
}
Stack H1,H2,H3;
void shuffle()
{
int turn=1;
int i=8;
int p=0;
length=0;
while(length!=9)
{
if( i>=0 and turn==a[i])
{
b[p]=a[i];
p++;
length++;
turn++;
i--;
}
else if(turn==H1.peak())
{
int g=H1.pop();
b[p]=g;
p++;
length++;
turn++;
}
else if(turn==H2.peak())
{
int g=H2.pop();
b[p]=g;
p++;
length++;
turn++;
}
else if(turn==H3.peak())
{
int g=H3.pop();
b[p]=g;
p++;
length++;
turn++;
}
else if(H1.is_empty()==1)
{
H1.push(a[i]);
i--;
}
else if(H2.is_empty()==1)
{
H2.push(a[i]);
i--;
}
else if(H3.is_empty()==1)
{
H3.push(a[i]);
i--;
}
else if(H1.is_empty()==0 and H2.is_empty()==0 and H3.is_empty()==0)
{
int x=H1.peak()-a[i];
int y=H2.peak()-a[i];
int z=H3.peak()-a[i];
if(x>0 and x<y and x<z)
{
H1.push(a[i]);
i--;
}
else if(y>0 and y<z)
{
H2.push(a[i]);
i--;
}
else if(z>0)
{
H3.push(a[i]);
i--;
}
else
{
cout<<"Unable to solve this hierachy,change the values";
break;
}
}
}
if (b[0]==1 and b[size-1]==9)
{
print();
}
}
int main()
{
shuffle();
}