Просмотр исходного кода

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

Guillaume Koenig лет назад: 7
Родитель
Сommit
edf8cba6a3
1 измененных файлов с 28 добавлено и 2 удалено
  1. 28 2
      main.go

+ 28 - 2
main.go

@@ -43,6 +43,18 @@ func (r Ride) Length() int {
43 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 58
 type ByEndtime []*Ride
47 59
 
48 60
 func (rs ByEndtime) Len() int      { return len(rs) }
@@ -97,17 +109,31 @@ func (c *Car) moveTo(x, y int) {
97 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 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 121
 		if r.used {
103 122
 			continue
104 123
 		}
105 124
 		if r.f < c.EarliestFinish(r) {
106 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 134
 		return r
109 135
 	}
110
-	return nil
136
+	return bestRide
111 137
 }
112 138
 
113 139
 func assign() bool {