Guest User

Untitled

a guest
Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CountPaths {
  4. private int[] myFieldRow, myFieldCol;
  5. private int myR, myC;
  6.  
  7. public int inField(int r, int c) {
  8. // System.out.printf("(%d,%d) -> (%d,%d)\n", r, c, myR-r, c+1);
  9. for(int j = 0; j < myFieldRow.length; j++) {
  10. if(myFieldRow[j] == myR-r && myFieldCol[j] == c+1) {
  11. return j;
  12. }
  13. }
  14. return -1;
  15. }
  16.  
  17. // Calculate the score for (r, c) by using the sum of (r-1, c) and (r, c-1).
  18. public int[][] neighborSum(int[][][][] grid, int r, int c) {
  19. int[] dr = {1, 0};
  20. int[] dc = {0, -1};
  21. int[][] score = new int[grid[0][0].length][grid[0][0].length];
  22. int specialField = inField(r, c);
  23.  
  24. for(int i = 0; i < 2; i++) {
  25. int _r = r+dr[i];
  26. int _c = c+dc[i];
  27. if(_r >= 0 && _c >=0 && _r < grid.length &&_c < grid[_r].length) {
  28. for(int j = 0; j < (specialField >= 0 ? score.length-1 : score.length); j++) {
  29. if(specialField >= 0) {
  30. for(int k = 0; k <= specialField; k++)
  31. score[j+1][specialField] +=
  32. grid[_r][_c][j][k];
  33. } else {
  34. for(int k = 0; k < score.length; k++) {
  35. score[j][k] +=
  36. grid[_r][_c][j][k];
  37. }
  38. }
  39. }
  40. }
  41. }
  42. /* if(specialField >= 0) {
  43. System.out.printf("(%d,%d) field=%d:\n", r, c, specialField);
  44. for(int i = 0; i < score.length; i++) {
  45. for(int j = 0; j < score[i].length; j++) {
  46. System.out.print(score[i][j] + " ");
  47. }
  48. System.out.println("");
  49. }
  50. }
  51. */ return score;
  52. }
  53.  
  54. boolean isEmpty(int[][][][] grid) {
  55. for(int r = 0; r < grid.length; r++) {
  56. for(int c = 0; c < grid[r].length; c++) {
  57. for(int n = 0; n < grid[r][c].length; n++) {
  58. for(int i = 0; i < grid[r][c][n].length; i++)
  59. if(grid[r][c][n][i] != 0)
  60. return false;
  61. }
  62. }
  63. }
  64. return true;
  65. }
  66.  
  67. public void printGrid(int[][][][] grid, int n) {
  68. System.out.println("---");
  69. for(int r = 0; r < grid.length; r++) {
  70. for(int c = 0; c < grid[r].length; c++) {
  71. int sum = 0;
  72. for(int i = 0; i < grid[r][c][n].length; i++)
  73. sum += grid[r][c][n][i];
  74. System.out.printf("%2d ", sum);
  75. }
  76. System.out.println("");
  77. }
  78. }
  79.  
  80. public int[] difPaths(int r, int c, int[] fieldrow, int[] fieldcol) {
  81. myFieldRow = fieldrow;
  82. myFieldCol = fieldcol;
  83. myR = r;
  84. myC = c;
  85. int n = fieldrow.length+1;
  86.  
  87. // current[row][column][n-value][index corresponds to largest fieldrow encountered].
  88. int[][][][] current = new int[r][c][n][n], previous = null;
  89. current[r-1][0][0][0] = 1;
  90.  
  91. // printGrid(current, 0);
  92.  
  93. while(true) {
  94. previous = current;
  95. current = new int[r][c][n][n];
  96. for(int _r = 0; _r < r; _r++) {
  97. for(int _c = 0; _c < c; _c++) {
  98. current[_r][_c] = neighborSum(previous, _r, _c);
  99. }
  100. }
  101. if(isEmpty(current))
  102. break;
  103. // printGrid(current, 0);
  104. }
  105.  
  106. int endPos = -1;
  107. // See if we missed the end case.
  108. for(int _n = 0; _n < n-1; _n++) {
  109. if(fieldrow[_n] == r && fieldcol[_n] == c) {
  110. endPos = _n;
  111. break;
  112. }
  113. }
  114.  
  115. int[] nValues = new int[n];
  116. for(int _n = 0; _n < n; _n++) {
  117. for(int i = 0; i < n; i++) {
  118. if(endPos < 0 || i > endPos)
  119. nValues[_n] += previous[0][c-1][_n][i];
  120. }
  121. }
  122. return nValues;
  123. }
  124.  
  125. public static void main(String[] args) {
  126. CountPaths countPaths = new CountPaths();
  127. int r = 50;
  128. int c = 50;
  129. int[] fieldrow = {50, 1};
  130. int[] fieldcol = {50, 1};
  131. System.out.println(Arrays.toString(countPaths.difPaths(r, c, fieldrow, fieldcol)));
  132. }
  133. }
Add Comment
Please, Sign In to add comment