Guest User

Pascal Walk(DFS Java)

a guest
Apr 6th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. public class Solution {
  4.     static ArrayList<Location> res;
  5.     static class Location {
  6.         public int i;
  7.         public int j;
  8.         public Location(int i1, int j1) {
  9.             i = i1;
  10.             j = j1;
  11.         }
  12.         public String toString() {
  13.             return this.i + " " + this.j;
  14.         }
  15.     }
  16.     public static void main(String[] args) {
  17.         int[][] table = new int[32][1000000];
  18.  
  19.         table[1][1] = 1;
  20.         for(int i = 2; i <= 31; i++) {
  21.             for(int j = 1; j < 1000000; j++) {
  22.                 table[i][j] = table[i-1][j-1] + table[i-1][j];
  23.             }
  24.         }
  25.  
  26.  
  27.         Scanner sc = new Scanner(System.in);
  28.         int t = sc.nextInt();
  29.  
  30.         for(int i = 0; i < t; i++) {
  31.             int n = sc.nextInt();
  32.             res = new ArrayList<>();
  33.             boolean[][] visited = new boolean[32][1000000];
  34.             ArrayList<Location> currentPath = new ArrayList<>();
  35.             currentPath.add(new Location(1, 1));
  36.             dfs(table, visited, 1, 1, currentPath, 1, n);
  37.             System.out.println("Case " + "#" + (i + 1) + ": ");
  38.             for(Location x: res) {
  39.                 System.out.println(x);
  40.             }
  41.         }
  42.  
  43.     }
  44.     static void dfs(int[][] table, boolean[][] visited, int i, int j, ArrayList<Location> currentPath, int currentSum, int totalSum) {
  45.  
  46.  
  47.         if(visited[i][j] || currentPath.size() > 500)
  48.             return;
  49.            
  50.         visited[i][j] = true;
  51.         if(currentSum == totalSum) {
  52.             res = new ArrayList<Location>(currentPath);
  53.             return;
  54.         }
  55.        
  56.         int p1 = table[i][j - 1];
  57.         int p2 = table[i][j + 1];
  58.         int p3 = table[i - 1][j];
  59.         int p4 = table[i - 1][j - 1];
  60.          int p5 = 0, p6 = 0;
  61.         if(i + 1 <= 31) {
  62.             p5 = table[i + 1][j];
  63.             p6 = table[i + 1][j + 1];
  64.         }
  65.  
  66.         if(p1 != 0 && currentSum + p1 <= totalSum) {
  67.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  68.             modifiedPath.add(new Location(i, j - 1));
  69.             dfs(table, visited, i, j - 1, modifiedPath, currentSum + p1, totalSum);
  70.         }
  71.  
  72.         if(p2 != 0 && currentSum + p2 <= totalSum) {
  73.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  74.             modifiedPath.add(new Location(i, j + 1));
  75.             dfs(table, visited, i, j + 1, modifiedPath, currentSum + p2, totalSum);
  76.         }
  77.  
  78.         if(p3 != 0 && currentSum + p3 <= totalSum) {
  79.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  80.             modifiedPath.add(new Location(i - 1, j));
  81.             dfs(table, visited, i - 1, j, modifiedPath, currentSum + p3, totalSum);
  82.         }
  83.  
  84.         if(p4 != 0 && currentSum + p4 <= totalSum) {
  85.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  86.             modifiedPath.add(new Location(i - 1, j - 1));
  87.             dfs(table, visited, i - 1, j - 1, modifiedPath, currentSum + p4, totalSum);
  88.         }
  89.  
  90.         if(i + 1 <= 31 && p5 != 0 && currentSum + p5 <= totalSum) {
  91.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  92.             modifiedPath.add(new Location(i + 1, j));
  93.             dfs(table, visited, i + 1, j, modifiedPath, currentSum + p5, totalSum);
  94.         }
  95.  
  96.         if(i + 1 <= 31 && p6 != 0 && currentSum + p6 <= totalSum) {
  97.             ArrayList<Location> modifiedPath = new ArrayList<Location>(currentPath);
  98.             modifiedPath.add(new Location(i + 1, j + 1));
  99.             dfs(table, visited, i + 1, j + 1, modifiedPath, currentSum + p6, totalSum);
  100.         }
  101.  
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment