-
Notifications
You must be signed in to change notification settings - Fork 2
/
MiningSolver.cs
162 lines (129 loc) · 4.87 KB
/
MiningSolver.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using lib.Models;
using lib.Models.Actions;
using lib.Solvers.RandomWalk;
namespace lib.Solvers
{
public class MiningSolver : ISolver
{
private List<List<ActionBase>> result;
private Random random = new Random();
private bool palka;
private readonly int limit;
public MiningSolver(bool palka = true, int limit = 10)
{
this.palka = palka;
this.limit = limit;
}
public string GetName()
{
return $"mining-{palka}-{limit}";
}
public int GetVersion()
{
return 1;
}
public Solved Solve(State state)
{
result = new List<List<ActionBase>> { new List<ActionBase>() };
if (palka)
BoosterMaster.CreatePalka2(state, result[0]);
if (limit != -1)
BoosterMaster.CloneAttack(state, result);
var pathBuilder = new PathBuilder(state.Map, limit);
while (state.UnwrappedLeft > 0)
{
var workerActions = new List<(Worker worker, ActionBase action)>();
for (int i = 0; i < state.Workers.Count; i++)
{
var map = state.Map;
var me = state.Workers[i];
pathBuilder.Build(map, me.Position, state.Workers.Take(i).Select(w => w.Position).ToList());
V best = null;
var bestDist = int.MaxValue;
for (int y = 0; y < map.SizeY; y++)
for (int x = 0; x < map.SizeX; x++)
{
if (map[new V(x, y)] != CellState.Void)
continue;
var dist = pathBuilder.Distance(new V(x, y));
if (dist == int.MaxValue)
continue;
if (dist < bestDist)
{
bestDist = dist;
best = new V(x, y);
}
}
var action = best == null ? new Wait() : pathBuilder.GetActions(best).First();
workerActions.Add((me, action));
result[i].Add(action);
}
state.Apply(workerActions);
}
return new Solved {Actions = result};
}
private class PathBuilder
{
private V start;
private List<V> other;
private Queue<V> queue;
private Map<int> distance;
private Map<(V, int)> parent;
private int timer;
private int limit;
public PathBuilder(Map map, int limit)
{
distance = new Map<int>(map.SizeX, map.SizeY);
parent = new Map<(V, int)>(map.SizeX, map.SizeY);
this.limit = limit;
}
private V Parent(V v)
{
return parent[v].Item2 == timer ? parent[v].Item1 : null;
}
public void Build(Map map, V start, List<V> other)
{
this.start = start;
this.other = other;
queue = new Queue<V>();
queue.Enqueue(start);
timer++;
while (queue.Any())
{
var v = queue.Dequeue();
if (map[v] == CellState.Void && !TooClose(v))
break;
for (var direction = 0; direction < 4; direction++)
{
var u = v.Shift(direction);
if (!u.Inside(map) || Parent(u) != null || map[u] == CellState.Obstacle)
continue;
parent[u] = (v, timer);
distance[u] = distance[v] + 1;
queue.Enqueue(u);
}
}
}
public int Distance(V v) => Parent(v) == null || TooClose(v) ? int.MaxValue : distance[v];
public List<ActionBase> GetActions(V to)
{
var result = new List<ActionBase>();
while (to != start)
{
var from = Parent(to);
result.Add(new Move(to - from));
to = from;
}
result.Reverse();
return result;
}
private bool TooClose(V v)
{
return other.Any(o => (o - v).MLen() < limit);
}
}
}
}