1.1 --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Tue Jul 17 21:38:39 2012 +0400
1.2 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Wed Jul 18 01:25:43 2012 +0400
1.3 @@ -14,6 +14,7 @@
1.4 import ru.bosony.model.game.GameState;
1.5 import ru.bosony.model.mine.Cell;
1.6 import ru.bosony.model.mine.Mine;
1.7 +import ru.bosony.model.moving.Coordinate;
1.8 import ru.bosony.model.moving.Movement;
1.9 import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
1.10 import edu.uci.ics.jung.graph.DirectedSparseGraph;
1.11 @@ -26,8 +27,10 @@
1.12 */
1.13 public class DijkstraSolver extends AbstractSolver {
1.14
1.15 - protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>();
1.16 - protected Set<CellsLink> loseLinks = new HashSet<CellsLink>();
1.17 + protected Map<String, Set<Movement>> loseMovs = new HashMap<String, Set<Movement>>();
1.18 + protected Map<String, Set<CellsLink>> loseLinks = new HashMap<String, Set<CellsLink>>();
1.19 + protected Map<String, Set<Coordinate>> attendedWalkingAroundCoordinates = new HashMap<String, Set<Coordinate>>();
1.20 + protected Map<String, Set<Coordinate>> noWay = new HashMap<String, Set<Coordinate>>();
1.21
1.22 public DijkstraSolver(Mine mine, SolverListener listener) {
1.23 super(mine, listener);
1.24 @@ -36,46 +39,80 @@
1.25 @Override
1.26 public void solve() {
1.27 while (true) {
1.28 - Mine testMine = null;
1.29 - testMine = mine.clone();
1.30 - Game game = new Game(testMine);
1.31 + Mine mine = null;
1.32 + mine = initialMine.clone();
1.33 + Game game = new Game(mine);
1.34
1.35 while (game.getState() == GameState.Game) {
1.36 - String map = testMine.toText();
1.37 + Cell robotCell = mine.getRobotCell();
1.38 + String map = mine.toText();
1.39 + String mapWithoutRobot = mine.toTextWithoutRobot();
1.40 if (!loseMovs.containsKey(map)) {
1.41 loseMovs.put(map, new HashSet<Movement>());
1.42 }
1.43 - Set<Cell> targets = testMine.findCells(CellContent.Lambda);
1.44 - targets.addAll(testMine.findCells(CellContent.HighOrderRock));
1.45 - if (targets.size() == 0)
1.46 - targets = testMine.findCells(CellContent.OpenLambdaLift);
1.47 - Graph<Cell, CellsLink> graph = getDirectedGraph(testMine);
1.48 + if (!loseLinks.containsKey(map)) {
1.49 + loseLinks.put(map, new HashSet<CellsLink>());
1.50 + }
1.51 + if (!noWay.containsKey(map)) {
1.52 + noWay.put(map, new HashSet<Coordinate>());
1.53 + }
1.54 + if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) {
1.55 + attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet<Coordinate>());
1.56 + }
1.57 + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate());
1.58 + Set<Cell> targets = new HashSet<Cell>();
1.59 + boolean isFreeWay = false;
1.60 + for (Cell cell : mine.getAdjacentCells(robotCell)) {
1.61 + Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate());
1.62 + isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate())
1.63 + && !loseMovs.get(map).contains(mov);
1.64 + }
1.65 + if (isFreeWay) {
1.66 + targets.addAll(mine.findCells(CellContent.Lambda));
1.67 + targets.addAll(mine.findCells(CellContent.HighOrderRock));
1.68 + if (targets.size() == 0)
1.69 + targets.addAll(mine.findCells(CellContent.OpenLambdaLift));
1.70 + targets.removeAll(mine.getCells(noWay.get(map)));
1.71 + } else {
1.72 + targets.addAll(mine.findCells(CellContent.Empty));
1.73 + targets.addAll(mine.findCells(CellContent.Earth));
1.74 + targets.addAll(mine.findCells(CellContent.HuttonsRazor));
1.75 + for (CellContent content : CellContent.getTrampolines()) {
1.76 + targets.addAll(mine.findCells(content));
1.77 + }
1.78 + targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot)));
1.79 + targets.removeAll(mine.getCells(noWay.get(map)));
1.80 + if (targets.size() == 0) {
1.81 + game.move(Movement.ABORT);
1.82 + continue;
1.83 + }
1.84 + }
1.85 + Graph<Cell, CellsLink> graph = getDirectedGraph(mine);
1.86 DijkstraShortestPath<Cell, CellsLink> alg = new DijkstraShortestPath<Cell, CellsLink>(graph,
1.87 new CellsLinkTransformer<CellsLink, Double>());
1.88 Cell nextTarget = null;
1.89 List<CellsLink> routeToNextTarget = null;
1.90 double shortestDist = Double.MAX_VALUE;
1.91 for (Cell target : targets) {
1.92 - if (target == null)
1.93 - continue;
1.94 List<CellsLink> route = null;
1.95 Number dist = null;
1.96 try {
1.97 - route = alg.getPath(testMine.getRobotCell(), target);
1.98 - dist = alg.getDistance(testMine.getRobotCell(), target);
1.99 + route = alg.getPath(mine.getRobotCell(), target);
1.100 + dist = alg.getDistance(mine.getRobotCell(), target);
1.101 } catch (IllegalArgumentException e) {
1.102 - // hmmm...
1.103 + noWay.get(map).add(target.getCoordinate());
1.104 continue;
1.105 }
1.106 if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) {
1.107 CellsLink firstLink = route.get(0);
1.108 Movement mov = firstLink.getSource().getCoordinate()
1.109 .getNecessaryMovement(firstLink.getTarget().getCoordinate());
1.110 - if (!loseMovs.get(map).contains(mov)) {
1.111 + if (!loseMovs.get(map).contains(mov)
1.112 + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.113 + target.getCoordinate())) {
1.114 shortestDist = dist.doubleValue();
1.115 nextTarget = target;
1.116 routeToNextTarget = route;
1.117 - break;
1.118 }
1.119 }
1.120 }
1.121 @@ -83,26 +120,52 @@
1.122 for (CellsLink link : routeToNextTarget) {
1.123 Cell source = link.getSource();
1.124 Cell target = link.getTarget();
1.125 - if (isSafeMovement(testMine, source, target)) {
1.126 + if (isSafeMovement(mine, source, target)) {
1.127 Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate());
1.128 GameState state = game.move(mov);
1.129 if (state == GameState.Lose || state == GameState.Abort) {
1.130 loseMovs.get(map).add(mov);
1.131 - loseLinks.add(link);
1.132 + loseLinks.get(map).add(link);
1.133 }
1.134 }
1.135 break;
1.136 }
1.137 } else {
1.138 + // Walking around
1.139 for (Movement mov : Movement.values()) {
1.140 if (!loseMovs.get(map).contains(mov)) {
1.141 - GameState state = game.move(mov);
1.142 - if (state == GameState.Game && map.equals(testMine.toText())) {
1.143 - if (mov != Movement.WAIT)
1.144 + if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) {
1.145 + boolean isRazorApplied = false;
1.146 + for (Cell cell : mine.getNeighboringCells(robotCell)) {
1.147 + isRazorApplied |= cell.getContent() == CellContent.WadlersBeard;
1.148 + }
1.149 + if (!isRazorApplied)
1.150 + continue;
1.151 + }
1.152 + Cell movCell = null;
1.153 + for (Cell cell : mine.getAdjacentCells(robotCell)) {
1.154 + if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) {
1.155 + if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.156 + cell.getCoordinate())) {
1.157 + movCell = cell;
1.158 + break;
1.159 + }
1.160 + }
1.161 +
1.162 + }
1.163 + if (mov == Movement.WAIT
1.164 + || mov == Movement.RAZOR
1.165 + || movCell != null
1.166 + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(
1.167 + movCell.getCoordinate())) {
1.168 + GameState state = game.move(mov);
1.169 + if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot()))
1.170 + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate());
1.171 + if (state == GameState.Game && map.equals(mine.toText())) {
1.172 loseMovs.get(map).add(mov);
1.173 - game.move(Movement.ABORT);
1.174 + }
1.175 + break;
1.176 }
1.177 - break;
1.178 }
1.179 }
1.180 }
1.181 @@ -154,6 +217,7 @@
1.182 return;
1.183 else
1.184 processedCells.add(cell);
1.185 + String map = mine.toText();
1.186 for (Cell neighboringCell : mine.getAdjacentCells(cell)) {
1.187 if (neighboringCell == null)
1.188 continue;
1.189 @@ -163,7 +227,7 @@
1.190 || neighboringContent == CellContent.OpenLambdaLift) {
1.191 if (isSafeMovement(mine, cell, neighboringCell)) {
1.192 CellsLink link = new CellsLink(mine, cell, neighboringCell);
1.193 - if (!loseLinks.contains(link)) {
1.194 + if (!loseLinks.get(map).contains(link)) {
1.195 links.add(link);
1.196 findAllCellLinks(mine, neighboringCell, links, processedCells);
1.197 }
1.198 @@ -179,7 +243,7 @@
1.199 continue;
1.200 if (isSafeMovement(mine, cell, target)) {
1.201 CellsLink link = new CellsLink(mine, cell, target);
1.202 - if (!loseLinks.contains(link)) {
1.203 + if (!loseLinks.get(map).contains(link)) {
1.204 links.add(link);
1.205 findAllCellLinks(mine, target, links, processedCells);
1.206 }