22 * 16.07.2012 11:05:26 |
25 * 16.07.2012 11:05:26 |
23 * |
26 * |
24 */ |
27 */ |
25 public class DijkstraSolver extends AbstractSolver { |
28 public class DijkstraSolver extends AbstractSolver { |
26 |
29 |
|
30 protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>(); |
|
31 protected Map<String, Set<CellsLink>> loseLinks = new HashMap<String, Set<CellsLink>>(); |
|
32 protected Map<String, Set<Coordinate>> attendedWalkingAroundCoordinates = new HashMap<String, Set<Coordinate>>(); |
|
33 protected Map<String, Set<Coordinate>> noWay = new HashMap<String, Set<Coordinate>>(); |
|
34 |
27 public DijkstraSolver(Mine mine, SolverListener listener) { |
35 public DijkstraSolver(Mine mine, SolverListener listener) { |
28 super(mine, listener); |
36 super(mine, listener); |
29 } |
37 } |
30 |
38 |
31 @Override |
39 @Override |
32 protected void solve() { |
40 public void solve() { |
33 while (true) { |
41 while (true) { |
|
42 Mine mine = null; |
|
43 mine = initialMine.clone(); |
34 Game game = new Game(mine); |
44 Game game = new Game(mine); |
35 |
45 |
36 while (game.getState() == GameState.Game) { |
46 while (game.getState() == GameState.Game) { |
37 try { |
47 Cell robotCell = mine.getRobotCell(); |
38 Set<Cell> targets = mine.findCells(CellContent.Lambda); |
48 String map = mine.toText(); |
|
49 String mapWithoutRobot = mine.toTextWithoutRobot(); |
|
50 if (!loseMovs.containsKey(map)) { |
|
51 loseMovs.put(map, new HashSet<Movement>()); |
|
52 } |
|
53 if (!loseLinks.containsKey(map)) { |
|
54 loseLinks.put(map, new HashSet<CellsLink>()); |
|
55 } |
|
56 if (!noWay.containsKey(map)) { |
|
57 noWay.put(map, new HashSet<Coordinate>()); |
|
58 } |
|
59 if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) { |
|
60 attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet<Coordinate>()); |
|
61 } |
|
62 attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate()); |
|
63 Set<Cell> targets = new HashSet<Cell>(); |
|
64 boolean isFreeWay = false; |
|
65 for (Cell cell : mine.getAdjacentCells(robotCell)) { |
|
66 Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()); |
|
67 isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate()) |
|
68 && !loseMovs.get(map).contains(mov); |
|
69 } |
|
70 if (isFreeWay) { |
|
71 targets.addAll(mine.findCells(CellContent.Lambda)); |
39 targets.addAll(mine.findCells(CellContent.HighOrderRock)); |
72 targets.addAll(mine.findCells(CellContent.HighOrderRock)); |
40 if (targets.size() == 0) |
73 if (targets.size() == 0) |
41 targets = mine.findCells(CellContent.OpenLambdaLift); |
74 targets.addAll(mine.findCells(CellContent.OpenLambdaLift)); |
42 if (targets.size() == 0) |
75 targets.removeAll(mine.getCells(noWay.get(map))); |
43 break; |
76 } else { |
44 Graph<Cell, CellsLink> graph = getDirectedGraph(); |
77 targets.addAll(mine.findCells(CellContent.Empty)); |
45 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
78 targets.addAll(mine.findCells(CellContent.Earth)); |
46 new CellsLinkTransformer<CellsLink, Double>()); |
79 targets.addAll(mine.findCells(CellContent.HuttonsRazor)); |
47 Cell nextTarget = null; |
80 for (CellContent content : CellContent.getTrampolines()) { |
48 List<CellsLink> routeToNextTarget = null; |
81 targets.addAll(mine.findCells(content)); |
49 double shortestDist = Double.MAX_VALUE; |
82 } |
50 for (Cell target : targets) { |
83 targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot))); |
51 List<CellsLink> route = alg.getPath(mine.getRobotCell(), target); |
84 targets.removeAll(mine.getCells(noWay.get(map))); |
52 Number dist = alg.getDistance(mine.getRobotCell(), target); |
85 if (targets.size() == 0) { |
53 if (dist != null && dist.doubleValue() < shortestDist) { |
86 game.move(Movement.ABORT); |
|
87 continue; |
|
88 } |
|
89 } |
|
90 Graph<Cell, CellsLink> graph = getDirectedGraph(mine); |
|
91 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph, |
|
92 new CellsLinkTransformer<CellsLink, Double>()); |
|
93 Cell nextTarget = null; |
|
94 List<CellsLink> routeToNextTarget = null; |
|
95 double shortestDist = Double.MAX_VALUE; |
|
96 for (Cell target : targets) { |
|
97 List<CellsLink> route = null; |
|
98 Number dist = null; |
|
99 try { |
|
100 route = alg.getPath(mine.getRobotCell(), target); |
|
101 dist = alg.getDistance(mine.getRobotCell(), target); |
|
102 } catch (IllegalArgumentException e) { |
|
103 noWay.get(map).add(target.getCoordinate()); |
|
104 continue; |
|
105 } |
|
106 if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) { |
|
107 CellsLink firstLink = route.get(0); |
|
108 Movement mov = firstLink.getSource().getCoordinate() |
|
109 .getNecessaryMovement(firstLink.getTarget().getCoordinate()); |
|
110 if (!loseMovs.get(map).contains(mov) |
|
111 && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( |
|
112 target.getCoordinate())) { |
54 shortestDist = dist.doubleValue(); |
113 shortestDist = dist.doubleValue(); |
55 nextTarget = target; |
114 nextTarget = target; |
56 routeToNextTarget = route; |
115 routeToNextTarget = route; |
57 } |
116 } |
58 } |
117 } |
59 if (nextTarget != null) { |
118 } |
60 for (CellsLink link : routeToNextTarget) { |
119 if (nextTarget != null) { |
61 Movement mov = link.getSource().getCoordinate() |
120 for (CellsLink link : routeToNextTarget) { |
62 .getNecessaryMovement(link.getTarget().getCoordinate()); |
121 Cell source = link.getSource(); |
63 game.move(mov); |
122 Cell target = link.getTarget(); |
64 break; |
123 if (isSafeMovement(mine, source, target)) { |
|
124 Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); |
|
125 GameState state = game.move(mov); |
|
126 if (state == GameState.Lose || state == GameState.Abort) { |
|
127 loseMovs.get(map).add(mov); |
|
128 loseLinks.get(map).add(link); |
|
129 } |
65 } |
130 } |
66 } |
131 break; |
67 } catch (IllegalArgumentException e) { |
132 } |
68 // hmmm... |
133 } else { |
|
134 // Walking around |
|
135 for (Movement mov : Movement.values()) { |
|
136 if (!loseMovs.get(map).contains(mov)) { |
|
137 if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) { |
|
138 boolean isRazorApplied = false; |
|
139 for (Cell cell : mine.getNeighboringCells(robotCell)) { |
|
140 isRazorApplied |= cell.getContent() == CellContent.WadlersBeard; |
|
141 } |
|
142 if (!isRazorApplied) |
|
143 continue; |
|
144 } |
|
145 Cell movCell = null; |
|
146 for (Cell cell : mine.getAdjacentCells(robotCell)) { |
|
147 if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) { |
|
148 if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( |
|
149 cell.getCoordinate())) { |
|
150 movCell = cell; |
|
151 break; |
|
152 } |
|
153 } |
|
154 |
|
155 } |
|
156 if (mov == Movement.WAIT |
|
157 || mov == Movement.RAZOR |
|
158 || movCell != null |
|
159 && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( |
|
160 movCell.getCoordinate())) { |
|
161 GameState state = game.move(mov); |
|
162 if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot())) |
|
163 attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate()); |
|
164 if (state == GameState.Game && map.equals(mine.toText())) { |
|
165 loseMovs.get(map).add(mov); |
|
166 } |
|
167 break; |
|
168 } |
|
169 } |
|
170 } |
69 } |
171 } |
70 } |
172 } |
71 if (game.getState() == GameState.Win) { |
173 addNewRoute(game); |
72 addNewRoute(game.getStringRoute(), game.getScore()); |
|
73 } |
|
74 attemptsCount++; |
174 attemptsCount++; |
75 } |
175 } |
76 } |
176 } |
77 |
177 |
78 protected Graph<Cell, CellsLink> getDirectedGraph() { |
178 protected boolean isSafeMovement(Mine mine, Cell source, Cell target) { |
|
179 if (source == null || target == null) |
|
180 return false; |
|
181 if (source.getContent().getTrampolineTarget() == target.getContent()) |
|
182 return true; |
|
183 Cell upCell = mine.getCell(target.getCoordinate().up()); |
|
184 if (upCell == null) |
|
185 return true; |
|
186 Cell upUpCell = mine.getCell(target.getCoordinate().up().up()); |
|
187 Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left()); |
|
188 Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right()); |
|
189 if (upCell.getContent() == CellContent.Empty |
|
190 && (upUpCell != null |
|
191 && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock) |
|
192 || upUpLeftCell != null |
|
193 && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null |
|
194 && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) { |
|
195 return false; |
|
196 } |
|
197 return true; |
|
198 } |
|
199 |
|
200 protected Graph<Cell, CellsLink> getDirectedGraph(Mine mine) { |
79 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
201 Graph<Cell, CellsLink> graph = new DirectedSparseGraph<Cell, CellsLink>(); |
80 for (CellsLink link : getCellLinks()) { |
202 for (CellsLink link : getCellLinks(mine)) { |
81 graph.addEdge(link, link.getSource(), link.getTarget()); |
203 graph.addEdge(link, link.getSource(), link.getTarget()); |
82 } |
204 } |
83 return graph; |
205 return graph; |
84 } |
206 } |
85 |
207 |
86 protected List<CellsLink> getCellLinks() { |
208 protected List<CellsLink> getCellLinks(Mine mine) { |
87 List<CellsLink> links = new ArrayList<CellsLink>(); |
209 List<CellsLink> links = new ArrayList<CellsLink>(); |
88 Cell robotCell = mine.getRobotCell(); |
210 Cell robotCell = mine.getRobotCell(); |
89 findAllCellLinks(robotCell, links, new HashSet<Cell>()); |
211 findAllCellLinks(mine, robotCell, links, new HashSet<Cell>()); |
90 return links; |
212 return links; |
91 } |
213 } |
92 |
214 |
93 protected void findAllCellLinks(Cell cell, List<CellsLink> links, Set<Cell> processedCells) { |
215 protected void findAllCellLinks(Mine mine, Cell cell, List<CellsLink> links, Set<Cell> processedCells) { |
94 if (processedCells.contains(cell)) |
216 if (processedCells.contains(cell)) |
95 return; |
217 return; |
96 else |
218 else |
97 processedCells.add(cell); |
219 processedCells.add(cell); |
|
220 String map = mine.toText(); |
98 for (Cell neighboringCell : mine.getAdjacentCells(cell)) { |
221 for (Cell neighboringCell : mine.getAdjacentCells(cell)) { |
99 if (neighboringCell == null) |
222 if (neighboringCell == null) |
100 continue; |
223 continue; |
101 CellContent neighboringContent = neighboringCell.getContent(); |
224 CellContent neighboringContent = neighboringCell.getContent(); |
102 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
225 if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty |
103 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
226 || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda |
104 || neighboringContent == CellContent.OpenLambdaLift) { |
227 || neighboringContent == CellContent.OpenLambdaLift) { |
105 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
228 if (isSafeMovement(mine, cell, neighboringCell)) { |
106 links.add(link); |
229 CellsLink link = new CellsLink(mine, cell, neighboringCell); |
107 findAllCellLinks(neighboringCell, links, processedCells); |
230 if (!loseLinks.get(map).contains(link)) { |
|
231 links.add(link); |
|
232 findAllCellLinks(mine, neighboringCell, links, processedCells); |
|
233 } |
|
234 } |
108 } else if (!CellContent.getTargets().contains(cell.getContent()) |
235 } else if (!CellContent.getTargets().contains(cell.getContent()) |
109 && CellContent.getTargets().contains(neighboringContent)) { |
236 && CellContent.getTargets().contains(neighboringContent)) { |
110 findAllCellLinks(neighboringCell, links, processedCells); |
237 findAllCellLinks(mine, neighboringCell, links, processedCells); |
111 } |
238 } |
112 } |
239 } |
113 if (CellContent.getTrampolines().contains(cell.getContent())) { |
240 if (CellContent.getTrampolines().contains(cell.getContent())) { |
114 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
241 for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { |
115 if (target == null) |
242 if (target == null) |
116 continue; |
243 continue; |
117 CellsLink link = new CellsLink(mine, cell, target); |
244 if (isSafeMovement(mine, cell, target)) { |
118 links.add(link); |
245 CellsLink link = new CellsLink(mine, cell, target); |
119 findAllCellLinks(target, links, processedCells); |
246 if (!loseLinks.get(map).contains(link)) { |
|
247 links.add(link); |
|
248 findAllCellLinks(mine, target, links, processedCells); |
|
249 } |
|
250 } |
120 } |
251 } |
121 } |
252 } |
122 } |
253 } |
123 } |
254 } |