Advertisement
vladimirVenkov

Labirint

Jun 10th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class MazeNew {
  6.  
  7.     static boolean findPath(char[][] matrix, int x, int y, char goal) {
  8.         if ( x < 0 || y < 0 || x >= matrix.length || y >= matrix[0].length ) return  false;
  9.  
  10.         if(matrix[x][y] == goal) return true;
  11.  
  12.         if(matrix [x][y] == 'v' || matrix[x][y] == '#') return false;
  13.         matrix[x][y] = 'v';
  14.         if ( findPath(matrix, x , y + 1, goal)) return true;
  15.         if ( findPath(matrix, x + 1, y , goal)) return true;
  16.         if ( findPath(matrix, x, y - 1 , goal)) return true;
  17.         if ( findPath( matrix, x - 1, y, goal)) return true;
  18.         matrix[x][y] = '.';
  19.         return false;
  20.     }
  21.     public static void main(String[] args) throws IOException {
  22.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.         String[] matrixRows = {
  24.                 "S...###",
  25.                 "#.#...#",
  26.                 "#.##.##",
  27.                 "..#.###",
  28.                 "#...#G#",
  29.                 "#.#...#"
  30.         };
  31.         char[][] matrix = new char[matrixRows.length][matrixRows[0].length()];
  32.         for (int i = 0; i < matrixRows.length; i++) {
  33.             matrix[i] = matrixRows[i].toCharArray();
  34.         }
  35.         System.out.println(matrix[0].length + " " + matrix.length);
  36.         System.out.println(matrix[0][6]);
  37.         System.out.println(findPath(matrix, 0, 0, 'G'));
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement