Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4.  
  5. public class Map {
  6.     private char[][] map;
  7.     private int[][] mapValue;
  8.     private int m, n, shortlyWayCount;
  9.     private boolean isLoaded;
  10.  
  11.     public Map(int m, int n) {
  12.         this.m = m;
  13.         this.n = n;
  14.         map = new char[n][m];
  15.         mapValue = new int[n][m];
  16.         isLoaded = false;
  17.     }
  18.  
  19.     public int getShortlyWayCount() {
  20.         return shortlyWayCount;
  21.     }
  22.  
  23.     public void setShortlyWayCount(int shortlyWayCount) {
  24.         this.shortlyWayCount = shortlyWayCount;
  25.     }
  26.  
  27.     public void printMap() throws IOException {
  28.         if (!isLoaded)
  29.             loadMapFromFile();
  30.         print();
  31.     }
  32.  
  33.     public void printMapValue() throws IOException{
  34.         if (!isLoaded)
  35.             loadMapFromFile();
  36.         System.out.println("\nPrint mapValue:");
  37.         print();
  38.     }
  39.  
  40.     private void print() {
  41.         boolean printMap = false;
  42.         if (Thread.currentThread().getStackTrace()[Thread.currentThread().getStackTrace().length - 2].getMethodName().equals("printMap"))
  43.             printMap = true;
  44.         for (int i = 0; i < n; i++) {
  45.             for (int j = 0; j < m; j++) {
  46.                 if (printMap)
  47.                     System.out.print(map[i][j] == ';' ? "\u001B[30;42m"  + map[i][j] + "\u001B[0m": map[i][j]);
  48.                 else
  49.                     System.out.print(mapValue[i][j] < 9999 ? (mapValue[i][j] < 10 ? " " + mapValue[i][j] : mapValue[i][j] + "" ) : "-1");
  50.             }
  51.             System.out.println();
  52.         }
  53.     }
  54.  
  55.     public void loadMapFromFile() throws IOException {
  56.         BufferedReader fileReader = new BufferedReader(new FileReader("map.txt"));
  57.         readFromFile(fileReader);
  58.         fileReader.close();
  59.  
  60.         isLoaded = true;
  61.     }
  62.  
  63.     private void readFromFile(BufferedReader fileReader) throws IOException {
  64.         int numberOfReadedLine = 0;
  65.         while (fileReader.ready()) {
  66.             map[numberOfReadedLine] = fileReader.readLine().toCharArray();
  67.             numberOfReadedLine++;
  68.         }
  69.     }
  70.  
  71.     public int searchShortlyWay(int i, int j) {
  72.         if (isFirst(i, j))
  73.             return 1;
  74.         if (isDeadline(i, j))
  75.           return 9999;
  76.         if (mapValue[i][j] != 0)
  77.             return mapValue[i][j] + 1;
  78.  
  79.         mapValue[i][j] = 9999;
  80.         int upValue = i > 0 ? searchShortlyWay(i - 1, j) : 9999;
  81.         int downValue = i < n - 1 ? searchShortlyWay(i + 1, j) : 9999;
  82.         int leftValue = j > 0 ? searchShortlyWay(i, j - 1) : 9999;
  83.         int rightValue = j < n - 1 ? searchShortlyWay(i, j + 1) : 9999;
  84.         int result = 1 + Integer.min(Integer.min(upValue, downValue), Integer.min(rightValue, leftValue));
  85.         mapValue[i][j] = (char)result;
  86.         return result;
  87.     }
  88.  
  89.     private boolean isFirst(int i, int j) {
  90.         return (i == 0 && j == 0);
  91.     }
  92.  
  93.     private boolean isDeadline(int i, int j) {
  94.         return (i < 0 || i >= n || j < 0 || j >= m || map[i][j] == 'x');
  95.     }
  96.  
  97.     public void printShortTrack() throws IOException {
  98.         mapValue[0][0] = 1;
  99.         int index = shortlyWayCount + 1;
  100.         while (index > 0) {
  101.             for (int i = 0; i < n; i++) {
  102.                 for (int j = 0; j < m; j++) {
  103.                     if (mapValue[i][j] == index && map[i][j] != 'x') {
  104.                         map[i][j] = ';';
  105.                         index--;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement