# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342450688 -14400 # Node ID b3d48e364f9e56248e78736f7a77bf1fa16770ab # Parent 4197f736166a179665a8fd4a307fc1138610f4ca Moar diff -r 4197f736166a -r b3d48e364f9e icfp-model/src/main/java/ru/bosony/model/game/Game.java --- a/icfp-model/src/main/java/ru/bosony/model/game/Game.java Mon Jul 16 15:20:48 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/game/Game.java Mon Jul 16 18:58:08 2012 +0400 @@ -12,7 +12,9 @@ import static ru.bosony.model.cellscontents.CellContent.WadlersBeard; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import ru.bosony.model.cellscontents.CellContent; import ru.bosony.model.mine.Cell; @@ -32,6 +34,7 @@ protected int lastScoreChange = 0; protected GameState state = GameState.Game; protected int lambdaCollectedCount = 0; + protected List history = new ArrayList(); protected List route = new ArrayList(); protected int underwater = 0; @@ -69,6 +72,11 @@ // Moving and scoring route.add(mov); + try { + history.add(mine.clone()); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } Cell robotCell = mine.getRobotCell(); Coordinate robotCoord = robotCell.getCoordinate(); Coordinate nextRobotCoord = null; @@ -349,4 +357,18 @@ public int getUnderwater() { return underwater; } + + public boolean hasViciousCircle() { + if (route.size() != history.size()) + throw new RuntimeException("Desynchronized history"); + Map state = new HashMap(); + for (int i = route.size() - 1; i >= 0; i--) { + Mine mine = history.get(i); + Movement mov = route.get(i); + if (state.get(mine) == mov) + return true; + state.put(mine, mov); + } + return false; + } } diff -r 4197f736166a -r b3d48e364f9e icfp-model/src/main/java/ru/bosony/model/moving/Movement.java --- a/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Mon Jul 16 15:20:48 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/moving/Movement.java Mon Jul 16 18:58:08 2012 +0400 @@ -10,12 +10,12 @@ public enum Movement implements TextRepresentable { LEFT("L"), + UP("U"), RIGHT("R"), - UP("U"), DOWN("D"), WAIT("W"), - ABORT("A"), - RAZOR("S"); + RAZOR("S"), + ABORT("A"); private String text; diff -r 4197f736166a -r b3d48e364f9e 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:20:48 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Mon Jul 16 18:58:08 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); + } + } } } }