BetinaUKTC

Grid

Feb 26th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. package DynamicProgramming;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DynamicProgramming {
  6. //динамична таблица
  7. static long table[][] = new long[30][30];
  8.  
  9. public static long RightDown(int row, int col) {
  10.  
  11. if (row == 0 || col == 0) { // невалиден вход
  12. return 0;
  13. }
  14. if (row == 1 && col == 1){ // възможен е само един ход
  15. return 1;
  16. }
  17. if (table[row][col] != Integer.MIN_VALUE) { // проверка
  18. return table[row][col];
  19. }
  20.  
  21.  
  22. long result = 0;
  23. result += RightDown(row - 1, col); // стъпка надолу
  24. result += RightDown(row, col - 1); // стъпка надясно
  25.  
  26. table[row][col] = result;
  27. return table[row][col];
  28. }
  29.  
  30. public static void main(String[] args) {
  31. Scanner scan = new Scanner(System.in);
  32. int rows = scan.nextInt();
  33. int cols = scan.nextInt();
  34. // пълним таблицата със стойности, които не могат да бъдат решение
  35. for (int i = 0; i < 30; i++) {
  36. for(int j = 0; j < 30; j++) {
  37. table[i][j] = Integer.MIN_VALUE;
  38. }
  39. }
  40. System.out.printf("Съществуват %d начина",RightDown(rows,cols));
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment