diff -r 660ac71d13b8 -r 70cfa8a066df icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 15:46:08 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 18:59:13 2012 +0400 @@ -14,7 +14,6 @@ import ru.bosony.model.game.GameState; import ru.bosony.model.mine.Cell; import ru.bosony.model.mine.Mine; -import ru.bosony.model.moving.Coordinate; import ru.bosony.model.moving.Movement; import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; import edu.uci.ics.jung.graph.DirectedSparseGraph; @@ -27,7 +26,8 @@ */ public class DijkstraSolver extends AbstractSolver { - protected Map> testedTargetCells = new HashMap>(); + protected Map> loseMovs = new HashMap>(); + protected Set loseLinks = new HashSet(); public DijkstraSolver(Mine mine, SolverListener listener) { super(mine, listener); @@ -45,26 +45,14 @@ Game game = new Game(testMine); while (game.getState() == GameState.Game) { - if (!testedTargetCells.containsKey(testMine)) { - testedTargetCells.put(testMine, new HashSet()); + String map = testMine.toText(); + if (!loseMovs.containsKey(map)) { + loseMovs.put(map, new HashSet()); } Set targets = testMine.findCells(CellContent.Lambda); targets.addAll(testMine.findCells(CellContent.HighOrderRock)); if (targets.size() == 0) targets = testMine.findCells(CellContent.OpenLambdaLift); - if (targets.size() == 0 || testedTargetCells.get(testMine).containsAll(targets)) { - // No targets. Walking around - Cell robotCell = testMine.getRobotCell(); - for (Coordinate nextCoordinate : robotCell.getCoordinate().getAdjacentCoordinates()) { - Cell nextCell = testMine.getCell(nextCoordinate); - targets.add(nextCell); - } - if (testedTargetCells.get(testMine).containsAll(targets)) { - // Fail. All cases are tried. - game.move(Movement.ABORT); - break; - } - } Graph graph = getDirectedGraph(testMine); DijkstraShortestPath alg = new DijkstraShortestPath(graph, new CellsLinkTransformer()); @@ -81,24 +69,44 @@ dist = alg.getDistance(testMine.getRobotCell(), target); } catch (IllegalArgumentException e) { // hmmm... - testedTargetCells.get(testMine).add(target); continue; } - if (dist != null && route != null && dist.doubleValue() < shortestDist - && !testedTargetCells.get(testMine).contains(target)) { - shortestDist = dist.doubleValue(); - nextTarget = target; - routeToNextTarget = route; - testedTargetCells.get(testMine).add(target); + if (dist != null && route != null && route.size() > 0 && dist.doubleValue() < shortestDist) { + CellsLink firstLink = route.get(0); + Movement mov = firstLink.getSource().getCoordinate() + .getNecessaryMovement(firstLink.getTarget().getCoordinate()); + if (!loseMovs.get(map).contains(mov)) { + shortestDist = dist.doubleValue(); + nextTarget = target; + routeToNextTarget = route; + break; + } } } if (nextTarget != null) { for (CellsLink link : routeToNextTarget) { - Movement mov = link.getSource().getCoordinate() - .getNecessaryMovement(link.getTarget().getCoordinate()); - game.move(mov); + Cell source = link.getSource(); + Cell target = link.getTarget(); + if (isSafeMovement(testMine, source, target)) { + Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); + GameState state = game.move(mov); + if (state == GameState.Lose || state == GameState.Abort || game.hasViciousCircle()) { + loseMovs.get(map).add(mov); + loseLinks.add(link); + } + } break; } + } else { + for (Movement mov : Movement.values()) { + if (!loseMovs.get(map).contains(mov)) { + GameState state = game.move(mov); + if (state == GameState.Lose || state == GameState.Abort || map.equals(testMine.toText()) + || game.hasViciousCircle()) + loseMovs.get(map).add(mov); + break; + } + } } } if (game.getState() == GameState.Win) { @@ -108,6 +116,28 @@ } } + protected boolean isSafeMovement(Mine mine, Cell source, Cell target) { + if (source == null || target == null) + return false; + if (source.getContent().getTrampolineTarget() == target.getContent()) + return true; + Cell upCell = mine.getCell(target.getCoordinate().up()); + if (upCell == null) + return true; + Cell upUpCell = mine.getCell(target.getCoordinate().up().up()); + Cell upUpLeftCell = mine.getCell(target.getCoordinate().up().up().left()); + Cell upUpRightCell = mine.getCell(target.getCoordinate().up().up().right()); + if (upCell.getContent() == CellContent.Empty + && (upUpCell != null + && (upUpCell.getContent() == CellContent.Rock || upUpCell.getContent() == CellContent.HighOrderRock) + || upUpLeftCell != null + && (upUpLeftCell.getContent() == CellContent.Rock || upUpLeftCell.getContent() == CellContent.HighOrderRock) || upUpRightCell != null + && (upUpRightCell.getContent() == CellContent.Rock || upUpRightCell.getContent() == CellContent.HighOrderRock))) { + return false; + } + return true; + } + protected Graph getDirectedGraph(Mine mine) { Graph graph = new DirectedSparseGraph(); for (CellsLink link : getCellLinks(mine)) { @@ -135,9 +165,13 @@ if (neighboringContent == CellContent.Earth || neighboringContent == CellContent.Empty || neighboringContent == CellContent.HuttonsRazor || neighboringContent == CellContent.Lambda || neighboringContent == CellContent.OpenLambdaLift) { - CellsLink link = new CellsLink(mine, cell, neighboringCell); - links.add(link); - findAllCellLinks(mine, neighboringCell, links, processedCells); + if (isSafeMovement(mine, cell, neighboringCell)) { + CellsLink link = new CellsLink(mine, cell, neighboringCell); + if (!loseLinks.contains(link)) { + links.add(link); + findAllCellLinks(mine, neighboringCell, links, processedCells); + } + } } else if (!CellContent.getTargets().contains(cell.getContent()) && CellContent.getTargets().contains(neighboringContent)) { findAllCellLinks(mine, neighboringCell, links, processedCells); @@ -147,9 +181,13 @@ for (Cell target : mine.findCells(cell.getContent().getTrampolineTarget())) { if (target == null) continue; - CellsLink link = new CellsLink(mine, cell, target); - links.add(link); - findAllCellLinks(mine, target, links, processedCells); + if (isSafeMovement(mine, cell, target)) { + CellsLink link = new CellsLink(mine, cell, target); + if (!loseLinks.contains(link)) { + links.add(link); + findAllCellLinks(mine, target, links, processedCells); + } + } } } }