# HG changeset patch # User indvdum (gotoindvdum[at]gmail[dot]com) # Date 1342560343 -14400 # Node ID b431cec961d714166873320b41135d0765f77f66 # Parent 9530f55e9639ddb1106382f844063f654d790a8c Moar diff -r 9530f55e9639 -r b431cec961d7 icfp-model/src/main/java/ru/bosony/model/mine/Mine.java --- a/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Tue Jul 17 21:38:39 2012 +0400 +++ b/icfp-model/src/main/java/ru/bosony/model/mine/Mine.java Wed Jul 18 01:25:43 2012 +0400 @@ -8,6 +8,7 @@ import static ru.bosony.model.cellscontents.CellContent.OpenLambdaLift; import static ru.bosony.model.cellscontents.CellContent.Rock; +import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; @@ -78,6 +79,10 @@ return text; } + public String toTextWithoutRobot() { + return toText().replaceAll(CellContent.MiningRobot.toText(), CellContent.Empty.toText()); + } + protected void fromText(String str) { Matcher matcher = minePattern.matcher(str); if (!matcher.matches()) @@ -178,6 +183,18 @@ return cells[x - 1][sizeY - y]; } + public Set getCells(Collection coordinates) { + Set cells = new HashSet(); + if (coordinates == null) + return cells; + for (Coordinate coordinate : coordinates) { + Cell cell = getCell(coordinate); + if (cell != null) + cells.add(cell); + } + return cells; + } + @Override public String toString() { return toText(); @@ -219,15 +236,21 @@ public Set getNeighboringCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getNeighboringCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } public Set getAdjacentCells(Cell cell) { Set cells = new HashSet(); - for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) - cells.add(getCell(coordinate)); + for (Coordinate coordinate : cell.getCoordinate().getAdjacentCoordinates()) { + Cell nextCell = getCell(coordinate); + if (nextCell != null) + cells.add(nextCell); + } return cells; } diff -r 9530f55e9639 -r b431cec961d7 icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Tue Jul 17 21:38:39 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/AbstractSolver.java Wed Jul 18 01:25:43 2012 +0400 @@ -1,7 +1,6 @@ package ru.bosony.solvers; import java.text.DecimalFormat; -import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -15,14 +14,14 @@ */ public abstract class AbstractSolver { - protected Mine mine = null; + protected Mine initialMine = null; protected SolverListener listener = null; protected Set games = new HashSet(); protected long attemptsCount = 0; protected long startTime = System.currentTimeMillis(); public AbstractSolver(Mine mine, SolverListener listener) { - this.mine = mine; + this.initialMine = mine; this.listener = listener; } @@ -34,8 +33,8 @@ listener.foundNextRoute(game.getStringRoute()); // TODO delete System.out.println(new DecimalFormat("0.00").format((((System.currentTimeMillis() - startTime) / 1000d))) - + " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route = " - + game.getStringRoute()); + + " seconds, State = " + game.getState() + ", Score = " + game.getScore() + ", Route(" + + game.getRoute().size() + ") = " + game.getStringRoute()); } games.add(game); } diff -r 9530f55e9639 -r b431cec961d7 icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java --- a/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Tue Jul 17 21:38:39 2012 +0400 +++ b/icfp-solver/src/main/java/ru/bosony/solvers/DijkstraSolver.java Wed Jul 18 01:25:43 2012 +0400 @@ -14,6 +14,7 @@ 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; @@ -26,8 +27,10 @@ */ public class DijkstraSolver extends AbstractSolver { - protected Map> loseMovs = new HashMap>(); - protected Set loseLinks = new HashSet(); + protected Map> loseMovs = new HashMap>(); + protected Map> loseLinks = new HashMap>(); + protected Map> attendedWalkingAroundCoordinates = new HashMap>(); + protected Map> noWay = new HashMap>(); public DijkstraSolver(Mine mine, SolverListener listener) { super(mine, listener); @@ -36,46 +39,80 @@ @Override public void solve() { while (true) { - Mine testMine = null; - testMine = mine.clone(); - Game game = new Game(testMine); + Mine mine = null; + mine = initialMine.clone(); + Game game = new Game(mine); while (game.getState() == GameState.Game) { - String map = testMine.toText(); + Cell robotCell = mine.getRobotCell(); + String map = mine.toText(); + String mapWithoutRobot = mine.toTextWithoutRobot(); 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); - Graph graph = getDirectedGraph(testMine); + if (!loseLinks.containsKey(map)) { + loseLinks.put(map, new HashSet()); + } + if (!noWay.containsKey(map)) { + noWay.put(map, new HashSet()); + } + if (!attendedWalkingAroundCoordinates.containsKey(mapWithoutRobot)) { + attendedWalkingAroundCoordinates.put(mapWithoutRobot, new HashSet()); + } + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(robotCell.getCoordinate()); + Set targets = new HashSet(); + boolean isFreeWay = false; + for (Cell cell : mine.getAdjacentCells(robotCell)) { + Movement mov = robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()); + isFreeWay |= !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains(cell.getCoordinate()) + && !loseMovs.get(map).contains(mov); + } + if (isFreeWay) { + targets.addAll(mine.findCells(CellContent.Lambda)); + targets.addAll(mine.findCells(CellContent.HighOrderRock)); + if (targets.size() == 0) + targets.addAll(mine.findCells(CellContent.OpenLambdaLift)); + targets.removeAll(mine.getCells(noWay.get(map))); + } else { + targets.addAll(mine.findCells(CellContent.Empty)); + targets.addAll(mine.findCells(CellContent.Earth)); + targets.addAll(mine.findCells(CellContent.HuttonsRazor)); + for (CellContent content : CellContent.getTrampolines()) { + targets.addAll(mine.findCells(content)); + } + targets.removeAll(mine.getCells(attendedWalkingAroundCoordinates.get(mapWithoutRobot))); + targets.removeAll(mine.getCells(noWay.get(map))); + if (targets.size() == 0) { + game.move(Movement.ABORT); + continue; + } + } + Graph graph = getDirectedGraph(mine); DijkstraShortestPath alg = new DijkstraShortestPath(graph, new CellsLinkTransformer()); Cell nextTarget = null; List routeToNextTarget = null; double shortestDist = Double.MAX_VALUE; for (Cell target : targets) { - if (target == null) - continue; List route = null; Number dist = null; try { - route = alg.getPath(testMine.getRobotCell(), target); - dist = alg.getDistance(testMine.getRobotCell(), target); + route = alg.getPath(mine.getRobotCell(), target); + dist = alg.getDistance(mine.getRobotCell(), target); } catch (IllegalArgumentException e) { - // hmmm... + noWay.get(map).add(target.getCoordinate()); continue; } 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)) { + if (!loseMovs.get(map).contains(mov) + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + target.getCoordinate())) { shortestDist = dist.doubleValue(); nextTarget = target; routeToNextTarget = route; - break; } } } @@ -83,26 +120,52 @@ for (CellsLink link : routeToNextTarget) { Cell source = link.getSource(); Cell target = link.getTarget(); - if (isSafeMovement(testMine, source, target)) { + if (isSafeMovement(mine, source, target)) { Movement mov = source.getCoordinate().getNecessaryMovement(target.getCoordinate()); GameState state = game.move(mov); if (state == GameState.Lose || state == GameState.Abort) { loseMovs.get(map).add(mov); - loseLinks.add(link); + loseLinks.get(map).add(link); } } break; } } else { + // Walking around for (Movement mov : Movement.values()) { if (!loseMovs.get(map).contains(mov)) { - GameState state = game.move(mov); - if (state == GameState.Game && map.equals(testMine.toText())) { - if (mov != Movement.WAIT) + if (mov == Movement.RAZOR && mine.getRazorsCount() > 0) { + boolean isRazorApplied = false; + for (Cell cell : mine.getNeighboringCells(robotCell)) { + isRazorApplied |= cell.getContent() == CellContent.WadlersBeard; + } + if (!isRazorApplied) + continue; + } + Cell movCell = null; + for (Cell cell : mine.getAdjacentCells(robotCell)) { + if (robotCell.getCoordinate().getNecessaryMovement(cell.getCoordinate()) == mov) { + if (!attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + cell.getCoordinate())) { + movCell = cell; + break; + } + } + + } + if (mov == Movement.WAIT + || mov == Movement.RAZOR + || movCell != null + && !attendedWalkingAroundCoordinates.get(mapWithoutRobot).contains( + movCell.getCoordinate())) { + GameState state = game.move(mov); + if (movCell != null && mapWithoutRobot.equals(mine.toTextWithoutRobot())) + attendedWalkingAroundCoordinates.get(mapWithoutRobot).add(movCell.getCoordinate()); + if (state == GameState.Game && map.equals(mine.toText())) { loseMovs.get(map).add(mov); - game.move(Movement.ABORT); + } + break; } - break; } } } @@ -154,6 +217,7 @@ return; else processedCells.add(cell); + String map = mine.toText(); for (Cell neighboringCell : mine.getAdjacentCells(cell)) { if (neighboringCell == null) continue; @@ -163,7 +227,7 @@ || neighboringContent == CellContent.OpenLambdaLift) { if (isSafeMovement(mine, cell, neighboringCell)) { CellsLink link = new CellsLink(mine, cell, neighboringCell); - if (!loseLinks.contains(link)) { + if (!loseLinks.get(map).contains(link)) { links.add(link); findAllCellLinks(mine, neighboringCell, links, processedCells); } @@ -179,7 +243,7 @@ continue; if (isSafeMovement(mine, cell, target)) { CellsLink link = new CellsLink(mine, cell, target); - if (!loseLinks.contains(link)) { + if (!loseLinks.get(map).contains(link)) { links.add(link); findAllCellLinks(mine, target, links, processedCells); }