-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_short_bus_route.go
205 lines (170 loc) · 3.65 KB
/
find_short_bus_route.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import "fmt"
type Route struct {
routes []int
routesM map[int]bool
}
type track struct {
bus []int
cbusNo int
haveReachedTarget bool
}
var (
// k: bus v:stops
BusRoutes map[int]*Route
// k:busStop v:availableBus
BusOpt map[int][]int
vistedBus, vistedBusStop map[int]bool
)
func init() {
BusRoutes = make(map[int]*Route)
BusOpt = make(map[int][]int)
vistedBus = make(map[int]bool)
vistedBusStop = make(map[int]bool)
}
func main() {
var (
source, target int
)
// INPUT: [1,2,7],[3,6,7]], source = 1, target = 6
source = 1
target = 6
// [7,12],[4,5,15],[6],[15,19],[9,12,13]] source = 15, target = 12
// source = 15
// target = 12
inputRoutes := [][]int{
0: {1, 2, 7},
1: {3, 6, 7},
// 0: {7, 12},
// 1: {4, 5, 15},
// 2: {6},
// 3: {15, 19},
// 4: {9, 12, 13},
// 0: {1, 2, 7}, // source = 1
// 1: {3, 6, 7},
// 2: {9, 3, 4}, // target = 4
}
// source = 1
// target = 4
transalateInput(inputRoutes)
//get available bus
busOpts, ok := BusOpt[source]
if !ok {
fmt.Println(-1)
return
}
routeTrack := &track{
bus: make([]int, 0),
haveReachedTarget: false,
}
for _, busNo := range busOpts {
vistedBus[busNo] = true
routeTrack.bus = append(routeTrack.bus, busNo)
routeTrack.cbusNo = busNo
// take stopes
busStopes := BusRoutes[busNo]
// if the target within the same bus route
// ie. check if this bus goes to the target
if ok := busStopes.routesM[target]; ok {
routeTrack.haveReachedTarget = true
break
}
findRoute(target, routeTrack, busStopes.routes)
if routeTrack.haveReachedTarget {
break
}
}
if !routeTrack.haveReachedTarget {
fmt.Println(-1)
return
}
fmt.Println("No of Bus i took> ", len(routeTrack.bus))
}
// ok, check if i can move to other bus
// so, far we know, the same "bus" can't go to "target"
func findRoute(target int, routeTrack *track, busStopes []int) {
for _, nstop := range busStopes {
vistedBusStop[nstop] = true
busOpts := BusOpt[nstop]
fmt.Printf("Stop: %d, buses: %+v\n", nstop, busOpts)
if len(busOpts) == 1 {
continue
}
// from where i can take next bus
for _, nbus := range busOpts {
if nbus != routeTrack.cbusNo && !vistedBus[nbus] {
fmt.Printf("INSIDE> Stop: %d, bus: %+v\n", nstop, nbus)
busStopes := BusRoutes[nbus]
if ok := busStopes.routesM[target]; ok {
fmt.Printf("REACHED> Stop: %d, bus: %+v\n", nstop, nbus)
routeTrack.bus = append(routeTrack.bus, nbus)
routeTrack.cbusNo = nbus
routeTrack.haveReachedTarget = true
return
}
// from the next stop
fmt.Println("Checking all the busstop from this bus> ", nbus)
routeTrack.bus = append(routeTrack.bus, nbus)
routeTrack.cbusNo = nbus
findRoute(target, routeTrack, busStopes.routes)
}
}
if routeTrack.haveReachedTarget {
return
}
}
return
}
func transalateInput(inputRoutes [][]int) {
for bus, routes := range inputRoutes {
routeM := make(map[int]bool)
for _, busStop := range routes {
routeM[busStop] = true
bo, ok := BusOpt[busStop]
if !ok {
BusOpt[busStop] = append(BusOpt[busStop], bus)
continue
}
bo = append(bo, bus)
BusOpt[busStop] = bo
}
BusRoutes[bus] = &Route{
routes: routes,
routesM: routeM,
}
}
}
// [[1,2,7],[3,6,7]], source = 1, target = 6
// routesInput := []int
// map[busStop] = []Routes
/**
//
{
0 : [1,2,7]
1: [3,6,7]
}
//
{
1: [0] -> start
2: [0]
3: [1]
6: [1]
7: [0, 1] {
//
for bus (
for busRoutes {
}
)
->
}
}
// 1 > 2
// -----
// 1 > 2 > N
*/
// map[routes] -> [bus] -> findclosest?
// linked list of routes! ||
// source -> stop := []multiple
// find[]
// recursiveSearch() {
// }