Advertisement
TsetsoP

DinamichnoOptimirane

Feb 26th, 2021
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public class DO {
  2.  
  3. private static int number = 3;
  4. private static int col = number;
  5. private static int row = number;
  6.  
  7. public static void main(String[] args) {
  8.  
  9. long[][] Stat = new long[row][col];
  10. MemSet(Stat);
  11.  
  12. Stat = new long[row][col];
  13. PrintArr(Stat);
  14. System.out.println("Number of solutions : " + recurse(row - 1, col - 1, Stat));
  15. System.out.printf("");
  16. PrintArr(Stat);
  17.  
  18. }
  19. private static long recurse(int rRows, int rCols, long [][] Stat){
  20. for (int i = 0; i < Stat.length; i++) {
  21. long[] step = Stat[i];
  22. while(rRows < row) {
  23. if (rRows == 0) {
  24. step = Stat[i++];
  25. if (rRows == 0) {
  26. return 1;
  27. }
  28. }
  29. }
  30. for (int j = 0; j < Stat[i].length; j++) {
  31. while(rCols < col) {
  32. if (rCols == 0) {
  33. return 1;
  34. if (rCols == 0) {
  35. step = Stat[j++];
  36. }
  37. }
  38. }
  39. }
  40. MemSet(Stat);
  41. }
  42.  
  43. if(Stat[rRows][rCols] != 0)
  44. return Stat[rRows][rCols];
  45.  
  46. long answer = 0;
  47.  
  48. answer += recurse( rRows - 1, rCols, Stat );
  49. System.out.println(answer + "d");
  50. answer += recurse (rRows, rCols - 1, Stat);
  51. System.out.println(answer + "r");
  52.  
  53. return Stat[rRows][rCols] = answer;
  54. }
  55. private static void PrintArr(long[][] Stat){
  56. System.out.println("Stat: ");
  57. for (int i = 0; i < Stat.length; i++) {
  58. for (int j = 0; j < Stat[i].length; j++) {
  59. System.out.print(Stat[i][j]);
  60. }
  61. System.out.println();
  62. }
  63. }
  64. private static void MemSet(long[][] Stat){
  65. for (int i = 0; i < row; i++) {
  66. for (int j = 0; j < col; j++) {
  67. Stat[i][j]= 0;
  68. }
  69. }
  70. }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement