Browse Source

choose: lowest ratio lenOfRide/(goToStart+lenOfRide) HIGH MEM (???)

Guillaume Koenig 7 years ago
parent
commit
edf8cba6a3
1 changed files with 28 additions and 2 deletions
  1. 28 2
      main.go

+ 28 - 2
main.go

@@ -43,6 +43,18 @@ func (r Ride) Length() int {
43
 	return xdist + ydist
43
 	return xdist + ydist
44
 }
44
 }
45
 
45
 
46
+func abs(x int) int {
47
+	if x < 0 {
48
+		return -x
49
+	} else {
50
+		return x
51
+	}
52
+}
53
+
54
+func (r *Ride) length() int {
55
+	return abs(r.a-r.x) + abs(r.b-r.y)
56
+}
57
+
46
 type ByEndtime []*Ride
58
 type ByEndtime []*Ride
47
 
59
 
48
 func (rs ByEndtime) Len() int      { return len(rs) }
60
 func (rs ByEndtime) Len() int      { return len(rs) }
@@ -97,17 +109,31 @@ func (c *Car) moveTo(x, y int) {
97
 	c.Y = y
109
 	c.Y = y
98
 }
110
 }
99
 
111
 
112
+func (c *Car) distanceTo(x, y int) int {
113
+	return abs(c.X-x) + abs(c.Y-y)
114
+}
115
+
100
 func Choose(c *Car) *Ride {
116
 func Choose(c *Car) *Ride {
101
-	for _, r := range Rides {
117
+	bestRide := Rides[0]
118
+	bestLenOfRide := Rides[0].length()
119
+	bestTotal := c.distanceTo(Rides[0].a, Rides[0].b) + bestLenOfRide
120
+	for _, r := range Rides[1:] {
102
 		if r.used {
121
 		if r.used {
103
 			continue
122
 			continue
104
 		}
123
 		}
105
 		if r.f < c.EarliestFinish(r) {
124
 		if r.f < c.EarliestFinish(r) {
106
 			continue
125
 			continue
107
 		}
126
 		}
127
+		lenOfRide := r.length()
128
+		total := c.distanceTo(r.a, r.b) + lenOfRide
129
+		if lenOfRide*bestTotal < total*bestLenOfRide {
130
+			bestLenOfRide = lenOfRide
131
+			bestTotal = total
132
+			bestRide = r
133
+		}
108
 		return r
134
 		return r
109
 	}
135
 	}
110
-	return nil
136
+	return bestRide
111
 }
137
 }
112
 
138
 
113
 func assign() bool {
139
 func assign() bool {