YORDAN2347

FindWaysInMatrix

Mar 1st, 2021
523
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. public class Main {
  4.  
  5.     private static int size = 3;
  6.     private static int rows = size;
  7.     private static int cols = size;
  8.  
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         long[][] Stat = new long[rows][cols];
  13.  
  14.         memSet(Stat);
  15.  
  16.         System.out.println("Number of tracks " + recurse(rows - 1, cols - 1, Stat));
  17.         System.out.printf("");
  18.  
  19.         printMatrix(Stat);
  20.  
  21.     }//End of main
  22.  
  23.  
  24.     // Recurse method
  25.     private static long recurse(int remainingRows, int remainingCols, long[][] stat){
  26.  
  27.         if (remainingRows == 0 || remainingCols == 0){// if we are in end of matrix
  28.             return 1;
  29.         }
  30.  
  31.         if (stat[remainingRows][remainingCols] != 0) {// ако не сме правили изчислението
  32.             return stat[remainingRows][remainingCols];
  33.         }
  34.  
  35.  
  36.         // Counting answers
  37.         long answer = 0;
  38.         answer = answer + recurse(remainingRows - 1, remainingCols, stat);//намалявам с 1 реда
  39.         answer = answer + recurse(remainingRows, remainingCols - 1, stat);//намалявам с 1 колоната
  40.  
  41.  
  42.         return stat[remainingRows][remainingCols] = answer;// save the anwser in stat Array
  43.  
  44.     }// end of Recurse method
  45.  
  46.     private static void printMatrix(long[][] stat){
  47.         System.out.println("Stat:");
  48.  
  49.         for (int i = 0; i < rows; i++) {
  50.             for (int j = 0; j < cols; j++) {
  51.  
  52.                 System.out.printf("[%d]", stat[i][j]);
  53.             }
  54.             System.out.println();
  55.         }
  56.  
  57.     }// end of print Matrix
  58.  
  59.     private static void memSet(long[][] stat){ // set Array with zeros
  60.         for (int r = 0; r < rows; r++) {
  61.             for (int c = 0; c < cols; c++) {
  62.                 stat[r][c] = 0;
  63.             }
  64.         }
  65.     }// End of memSet
  66.  
  67. }
  68.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×