Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package Mandatory3;
  2. /*
  3. @author Lucas Gramme
  4. */
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class LetterLabyrinth2 {
  9.     //Variables to save maze
  10.     private int R, C;
  11.     char[][] m;
  12.     String[] input;
  13.     //Starting point
  14.     int sr = 0, sc = 0;
  15.     //Track number of steps taken
  16.     int move_count = 0;
  17.     int node_left_in_layer = 1;
  18.     int nodes_in_next_layer = 0;
  19.     //Check if we have reached the end
  20.     boolean reached_end = false;
  21.     //Check if cell has been visited
  22.     boolean visited[][] = new boolean[R][C];
  23.     //North, south, east, west directions
  24.     int[] dr = {-1, 1, 0, 0};
  25.     int[] dc = {0, 0, 1, -1};
  26.     //Queue
  27.     ArrayList<Integer> rq = new ArrayList<>();
  28.     ArrayList<Integer> cq = new ArrayList<>();
  29.  
  30.     public int solve(){
  31.         rq.add(sr);
  32.         cq.add(sc);
  33.         visited[sr][sc] = true;
  34.         while(rq.size() > 0){
  35.             int r = rq.remove(0);
  36.             int c = cq.remove(0);
  37.             if(r == R-1 && c == C-1){
  38.                 move_count++;
  39.                 reached_end = true;
  40.                 break;
  41.             }else{
  42.                 exploreNeighbours(r,c);
  43.                 node_left_in_layer--;
  44.                 if(node_left_in_layer == 0){
  45.                     node_left_in_layer = nodes_in_next_layer;
  46.                     nodes_in_next_layer = 0;
  47.                     move_count++;
  48.                 }
  49.             }
  50.         }
  51.         if(reached_end){
  52.             return move_count;
  53.         }else {
  54.             return -1;
  55.         }
  56.     }
  57.  
  58.     private void exploreNeighbours(int r, int c) {
  59.         for(int i = 0; i < 4; i++){
  60.             int rr = r + dr[i];
  61.             int cc = c + dc[i];
  62.             //Skip out of bounds locations
  63.             if(rr < 0 || cc < 0)continue;
  64.             if(rr >= R || cc >= C)continue;
  65.             //Skip visited cells
  66.             if(visited[rr][cc])continue;
  67.             //Skip invalid cells
  68.             if(m[rr][cc] == m[r][c])continue;
  69.             //Add neighbour to queue
  70.             rq.add(rr);
  71.             cq.add(cc);
  72.             //Set neighbour to visited = true and add to next layer
  73.             visited[rr][cc] = true;
  74.             nodes_in_next_layer++;
  75.         }
  76.     }
  77.  
  78.     public void run(){
  79.         Scanner scan = new Scanner(System.in);
  80.         //Maze will be RxC
  81.         R = C = scan.nextInt();
  82.         input = new String[R];
  83.         m = new char[R][C];
  84.         visited = new boolean[R][C];
  85.         for(int i = 0; i < R; i++){
  86.             input[i] = scan.next();
  87.             for(int j = 0; j < R; j++){
  88.                 m[i][j] = input[i].charAt(j);
  89.             }
  90.         }
  91.         System.out.println(solve());
  92.     }
  93.  
  94.     public static void main(String[] args) {
  95.        new LetterLabyrinth2().run();
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement