Advertisement
Guest User

Never Ending Snake

a guest
Apr 1st, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.awt.Point;
  6. public class NeverEndingSnake {
  7.     public enum Result {COMPLETE, INCOMPLETE, DEATH};
  8.     public enum Cell {START, EMPTY, FOOD, HOLE, VISITED, VISITED_FOOD};
  9.     private Result evaluate(Cell[][] map, Path path) {
  10.         int x = 0;
  11.         int y = 0;
  12.         for (int i = 0; i < map.length; i++) {
  13.             for (int j = 0; j < map[0].length; j++) {
  14.                 if (map[i][j] == Cell.START) {
  15.                     x = i;
  16.                     y = j;
  17.                 }
  18.             }
  19.         }
  20.         for (char dir : path.getPath().toCharArray()) {
  21.             x += dir == 'l' ? -1 : dir == 'r' ? 1 : 0;
  22.             y += dir == 'u' ? -1 : dir == 'd' ? 1 : 0;
  23.             if (x < 0 || x >= map.length || y < 0 || y >= map[0].length) {
  24.                 return Result.DEATH;
  25.             } else {
  26.                 if (map[x][y] == Cell.HOLE || map[x][y] == Cell.START || map[x][y] == Cell.VISITED || map[x][y] == Cell.VISITED_FOOD) {
  27.                     return Result.DEATH;
  28.                 }
  29.             }
  30.             map[x][y] = map[x][y] == Cell.FOOD ? Cell.VISITED_FOOD : Cell.VISITED;
  31.         }
  32.         boolean complete = true;
  33.         for (int i = 0; i < map.length; i++) {
  34.             for (int j = 0; j < map[0].length; j++) {
  35.                 complete = complete && map[i][j] != Cell.FOOD;
  36.             }
  37.         }
  38.         return complete ? Result.COMPLETE : Result.INCOMPLETE;
  39.     }
  40.     private void solve(Cell[][] map) {
  41.         ArrayList<Path> paths = new ArrayList<>();
  42.         paths.add(new Path("", map));
  43.         while (!paths.isEmpty()) {
  44.             paths.sort((Path path1, Path path2) -> path1.getPriority() - path2.getPriority());
  45.             Path path = paths.get(0);
  46.             switch (evaluate(map, path)) {
  47.                 case COMPLETE:
  48.                     char last = ' ';
  49.                     int count = 1;
  50.                     for (char dir : path.getPath().toCharArray()) {
  51.                         if (dir == last) {
  52.                             count++;
  53.                         } else {
  54.                             if (count != 1) {
  55.                                 System.out.print(count);
  56.                             }
  57.                             count = 1;
  58.                             System.out.print(dir);
  59.                         }
  60.                         last = dir;
  61.                     }
  62.                     if (count != 1) {
  63.                         System.out.print(count);
  64.                     }
  65.                     System.out.println();
  66.                     return;
  67.                 case INCOMPLETE:
  68.                     paths.remove(0);
  69.                     paths.add(new Path(path.getPath() + 'l', map));
  70.                     paths.add(new Path(path.getPath() + 'u', map));
  71.                     paths.add(new Path(path.getPath() + 'r', map));
  72.                     paths.add(new Path(path.getPath() + 'd', map));
  73.                     break;
  74.                 case DEATH:
  75.                     paths.remove(0);
  76.                     break;
  77.             }
  78.             for (int x = 0; x < map.length; x++) {
  79.                 for (int y = 0; y < map[0].length; y++) {
  80.                     switch (map[x][y]) {
  81.                         case VISITED:
  82.                             map[x][y] = Cell.EMPTY;
  83.                             break;
  84.                         case VISITED_FOOD:
  85.                             map[x][y] = Cell.FOOD;
  86.                             break;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         System.out.println("No solution found.");
  92.     }
  93.     public Cell[][] loadMap(String filename) throws FileNotFoundException {
  94.         File file = new File(filename);
  95.         Scanner scanner = new Scanner(file);
  96.         ArrayList<String> input = new ArrayList<>();
  97.         while (scanner.hasNextLine()) {
  98.             input.add(scanner.nextLine());
  99.         }
  100.         int w = input.get(0).length() - 2;
  101.         int h = input.size() - 2;
  102.         Cell[][] map = new Cell[w][h];
  103.         for (int y = 0; y < h; y++) {
  104.             for (int x = 0; x < w; x++) {
  105.                 switch (input.get(y + 1).charAt(x + 1)) {
  106.                     case 's':
  107.                         map[x][y] = Cell.START;
  108.                         break;
  109.                     case '*':
  110.                         map[x][y] = Cell.FOOD;
  111.                         break;
  112.                     case 'O':
  113.                         map[x][y] = Cell.HOLE;
  114.                         break;
  115.                     default:
  116.                         map[x][y] = Cell.EMPTY;
  117.                         break;
  118.                 }
  119.             }
  120.         }
  121.         return map;
  122.     }
  123.     public void neverEndingSnake() throws FileNotFoundException {
  124.         for (int i = 1; i <= 16; i++) {
  125.             String filename = "map" + i + ".txt";
  126.             System.out.print(filename + " ");
  127.             solve(loadMap(filename));
  128.         }
  129.     }
  130.     public static void main(String[] args) throws FileNotFoundException {
  131.         (new NeverEndingSnake()).neverEndingSnake();
  132.     }
  133.     private class Path {
  134.         final private String path;
  135.         final private int priority;
  136.         public Path(String path, Cell[][] map) {
  137.             this.path = path;
  138.             ArrayList<Point> foods = new ArrayList<>();
  139.             Point start = new Point(0, 0);
  140.             for (int x = 0; x < map.length; x++) {
  141.                 for (int y = 0; y < map[0].length; y++) {
  142.                     switch (map[x][y]) {
  143.                         case START:
  144.                             start.x = x;
  145.                             start.y = y;
  146.                             break;
  147.                         case FOOD:
  148.                             foods.add(new Point(x, y));
  149.                             break;
  150.                     }
  151.                 }
  152.             }
  153.             for (char dir : path.toCharArray()) {
  154.                 start.x += dir == 'l' ? -1 : dir == 'r' ? 1 : 0;
  155.                 start.y += dir == 'u' ? -1 : dir == 'd' ? 1 : 0;
  156.             }
  157.             int pr = path.length();
  158.             while (!foods.isEmpty()) {
  159.                 Point closest = foods.get(0);
  160.                 for (Point food : foods) {
  161.                     if (Math.abs(food.x - start.x) + Math.abs(food.y - start.y) < Math.abs(closest.x - start.x) + Math.abs(closest.y - start.y)) {
  162.                         closest = food;
  163.                     }
  164.                 }
  165.                 pr += Math.abs(closest.x - start.x) + Math.abs(closest.y - start.y);
  166.                 start.x = closest.x;
  167.                 start.y = closest.y;
  168.                 foods.remove(closest);
  169.             }
  170.             priority = pr;
  171.         }
  172.         public String getPath() {
  173.             return path;
  174.         }
  175.         public int getPriority() {
  176.             return priority;
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement