Advertisement
valchak

Find_All_Paths_in_a_Labyrinth

Sep 11th, 2019
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package backtracking;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class P02_Find_All_Paths_in_a_Labyrinth {
  9.     private static char[][] labyrinth;
  10.     private static List<Character> path = new ArrayList<>();
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.         labyrinth = readLab();
  14.         findPaths(0, 0, 'S');
  15.     }
  16.  
  17.     private static void findPaths(int row, int col, char direction) {
  18.         if(!isInBounds(row, col)){
  19.             return;
  20.         }
  21.  
  22.         path.add(direction);
  23.  
  24.         if(isExit(row, col)){
  25.             printPath();
  26.         }else if(!isVisited(row, col) && isFree(row, col)){
  27.             Mark(row, col);
  28.             findPaths(row, col +1, 'R');
  29.             findPaths(row+1, col, 'D');
  30.             findPaths(row, col -1, 'L');
  31.             findPaths(row-1, col, 'U');
  32.             UnMark(row, col);
  33.         }
  34.  
  35.         path.remove(path.size()-1);
  36.     }
  37.  
  38.     private static boolean isVisited(int row, int col) {
  39.         return labyrinth[row][col] == 'v';
  40.     }
  41.  
  42.     private static boolean isFree(int row, int col) {
  43.         return labyrinth[row][col] == '-';
  44.     }
  45.  
  46.     private static void Mark(int row, int col) {
  47.         labyrinth[row][col] = 'v';
  48.     }
  49.  
  50.     private static void UnMark(int row, int col) {
  51.         labyrinth[row][col] = '-';
  52.     }
  53.  
  54.     private static void printPath() {
  55.         List<Character> characters = path.subList(1, path.size());
  56.         System.out.println(Arrays.toString(characters.toArray())
  57.                     .replaceAll("[\\[\\],\\s]", "")
  58.             );
  59.     }
  60.  
  61.     private static boolean isExit(int row, int col) {
  62.         return labyrinth[row][col] == 'e';
  63.     }
  64.  
  65.     private static boolean isInBounds(int row, int col) {
  66.         return row >= 0 && row <= labyrinth.length - 1 &&
  67.                 col >= 0 && col <= labyrinth[0].length - 1;
  68.     }
  69.  
  70.     private static char[][] readLab() throws IOException {
  71.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  72.  
  73.         int rows = Integer.parseInt(reader.readLine());
  74.         int cols = Integer.parseInt(reader.readLine());
  75.         char[][] labyrinth = new char[rows][cols];
  76.  
  77.         for (int i = 0; i < rows ; i++) {
  78.             char[] row = reader.readLine().toCharArray();
  79.             labyrinth[i] = row;
  80.         }
  81.  
  82.         return labyrinth;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement