Guest User

Untitled

a guest
Jul 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) throws Exception {
  7.         Scanner s = new Scanner (System.in);
  8.         int n = s.nextInt();
  9.         int m = s.nextInt();
  10.         ArrayList<String> paths = getMazePaths(1, 1, n , m);
  11.         System.out.println(paths);
  12.     }
  13.  
  14.     // sr - source row
  15.     // sc - source column
  16.     // dr - destination row
  17.     // dc - destination column
  18.     public static ArrayList<String> getMazePaths(int sr, int sc, int dr, int dc) {
  19.         if (sr == dr && sc == dc){
  20.             ArrayList<String> bres = new ArrayList<>();
  21.             bres.add("");
  22.             return bres;
  23.         }
  24.        
  25.         ArrayList<String> hpaths = new ArrayList<>();
  26.         ArrayList<String> vpaths = new ArrayList<>();
  27.        
  28.         if (sr < dr){
  29.             hpaths = getMazePaths(sr, sc + 1, dr, dc);
  30.         }
  31.         if(sc < dc){
  32.             vpaths = getMazePaths(sr + 1, sc, dr, dc);
  33.         }
  34.         ArrayList<String> paths = new ArrayList<>();
  35.        
  36.         for (String hpath: hpaths){
  37.             paths.add("h" + hpath);
  38.         }
  39.        
  40.         for (String vpath: vpaths){
  41.             paths.add("v" + vpath);
  42.         }
  43.         return paths;
  44.     }
  45.  
  46. }
Add Comment
Please, Sign In to add comment