Advertisement
Guest User

Untitled

a guest
Mar 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package com.company;
  2.  
  3. import sun.security.util.BigInt;
  4.  
  5. import java.math.BigInteger;
  6. import java.util.*;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         boolean[][] grid =
  12.                 {
  13.                         {true, true, true, true},
  14.                         {false, true, true, true},
  15.                         {true, false, true, true},
  16.                         {true, true, true, true},
  17.                 };
  18.  
  19.         Boolean[][] memo = new Boolean[4][4];
  20.         boolean path = findPath(grid, 3, 3, memo);// 0,0; 0,1; 0,2; 1,2;
  21.         System.out.println(path);
  22.     }
  23.  
  24.     public static boolean findPath(boolean[][] grid, int endRow, int endColumn, Boolean[][] memo) {
  25.         if (endRow == 0 && endColumn == 0) {
  26.             return true;
  27.         } else if (grid[endRow][endColumn]) {
  28.             boolean pathUp = false;
  29.             boolean pathLeft = false;
  30.             if (endColumn - 1 >= 0) {
  31.                 if (memo[endRow][endColumn - 1] != null) {
  32.                     pathUp = memo[endRow][endColumn - 1];
  33.                 } else {
  34.                     pathUp = findPath(grid, endRow, endColumn - 1, memo);
  35.                 }
  36.             }
  37.             if (endRow - 1 >= 0) {
  38.                 if (memo[endRow - 1][endColumn] != null) {
  39.                     pathLeft = memo[endRow - 1][endColumn];
  40.                 } else {
  41.                     pathLeft = findPath(grid, endRow - 1, endColumn, memo);
  42.                 }
  43.             }
  44.  
  45.             memo[endRow][endColumn] = pathLeft || pathUp;
  46.  
  47.             return pathLeft || pathUp;
  48.         }
  49.         return false;
  50.     }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement