Przeglądaj źródła

Revert "Recursion to try a few different routes - score: 47.9M"

This reverts commit 8187c0f1f24c5ee90fee56c8c42a88e404ba6059.
Guillaume Koenig 7 lat temu
rodzic
commit
654a9971c1
1 zmienionych plików z 30 dodań i 49 usunięć
  1. 30 49
      main.go

+ 30 - 49
main.go

@@ -2,7 +2,6 @@ package main
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"math/rand"
6 5
 	"os"
7 6
 	"sort"
8 7
 )
@@ -135,23 +134,7 @@ func max(a, b int) int {
135 134
 	}
136 135
 }
137 136
 
138
-var bestTotalScore int
139
-
140
-// invariant : when entering and leaving function,
141
-// Sched has the "same" heap. Same means successive
142
-// popped values will be the same (but the internal
143
-// ordering of nodes may be different)
144
-func Choose(cumulativeScore int, depth int) {
145
-	if cumulativeScore > bestTotalScore {
146
-		bestTotalScore = cumulativeScore
147
-	}
148
-	c := Sched.Pop()
149
-	if c == nil {
150
-		// Stop recursion, empty car list,
151
-		// could not match cars to rides
152
-		return
153
-	}
154
-
137
+func Choose(c *Car) *Ride {
155 138
 	var bestRides []struct {
156 139
 		r         *Ride
157 140
 		lenOfRide int
@@ -162,9 +145,9 @@ func Choose(cumulativeScore int, depth int) {
162 145
 		if r.used {
163 146
 			continue
164 147
 		}
165
-		if r.Length() > 6000 {
166
-			continue
167
-		}
148
+		// if r.Length() > 6000 {
149
+		// 	continue
150
+		// }
168 151
 		if r.f < c.EarliestFinish(r) {
169 152
 			continue
170 153
 		}
@@ -180,10 +163,7 @@ func Choose(cumulativeScore int, depth int) {
180 163
 			}{r, lenOfRide, total})
181 164
 			// shitty sort-of-correct-but-quite-incorrect way
182 165
 			// of picking next best n rides
183
-			n := 1
184
-			if rand.Intn(max(1, int(N/5))) == 0 {
185
-				n = 3
186
-			}
166
+			n := 3
187 167
 			if len(bestRides) > n {
188 168
 				bestRides = bestRides[len(bestRides)-n:]
189 169
 			}
@@ -193,31 +173,31 @@ func Choose(cumulativeScore int, depth int) {
193 173
 	// 	fmt.Printf("Picking %d %d -> %d %d\n", bestRide.a, bestRide.b, bestRide.x, bestRide.y)
194 174
 	// }
195 175
 	if len(bestRides) != 0 {
196
-		for _, br := range bestRides {
197
-			r := br.r
198
-			r.used = true
199
-			oldC := *c
200
-			c.Update(r)
201
-			Sched.Add(c)
202
-			// recursion 101
203
-			Choose(cumulativeScore+c.score-oldC.score, depth+1)
204
-			Sched.RemoveAtIndex(c.pqindex)
205
-			// pqindex is meaningless now but it's ok
206
-			// it will be fixed when c is added again
207
-			*c = oldC
208
-			r.used = false
209
-		}
210
-	} else {
211
-		// another car may still have other rides
212
-		Choose(cumulativeScore, depth)
176
+		// pick the not quite best ride...hey some
177
+		// of the inputs already  give a better score !
178
+		return bestRides[0].r
179
+		//return bestRides[len(bestRides)-1].r
180
+	}
181
+	return nil
182
+}
183
+
184
+func assign() bool {
185
+	c := Sched.Pop()
186
+	if c == nil {
187
+		return false
188
+	}
189
+	r := Choose(c)
190
+	if r == nil {
191
+		return true
213 192
 	}
214
-	// add back the one we popped at beginning of function
193
+	r.used = true
194
+	c.Update(r)
215 195
 	Sched.Add(c)
196
+
197
+	return true
216 198
 }
217 199
 
218 200
 func solve() {
219
-	rand.Seed(1)
220
-
221 201
 	sort.Sort(ByEndtime(Rides))
222 202
 
223 203
 	Sched = &prioq{}
@@ -234,17 +214,19 @@ func solve() {
234 214
 		Sched.Add(c)
235 215
 	}
236 216
 
237
-	// start recursion
238
-	Choose(0, 0)
217
+	for assign() {
218
+	}
239 219
 
220
+	totalScore := 0
240 221
 	for _, c := range Cars {
241 222
 		fmt.Fprintf(output, "%d", len(c.Rides))
242 223
 		for _, ri := range c.Rides {
243 224
 			fmt.Fprintf(output, " %d", ri)
244 225
 		}
245 226
 		fmt.Fprintf(output, "\n")
227
+		totalScore += c.score
246 228
 	}
247
-	fmt.Printf("%d\n", bestTotalScore)
229
+	fmt.Printf("%d\n", totalScore)
248 230
 }
249 231
 
250 232
 func main() {
@@ -340,7 +322,6 @@ func (pq *prioq) RemoveAtIndex(k int) *Car {
340 322
 		return elem
341 323
 	}
342 324
 
343
-	pq.bintree[k].pqindex = -1
344 325
 	elem := pq.bintree[k]
345 326
 	// Put last element at hole
346 327
 	pq.bintree[k] = pq.bintree[len(pq.bintree)-1]