sunfresh

Scrooge McDuck

Jan 23rd, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. Scrooge McDuck
  2. Description
  3. Scrooge McDuck likes his treasure very much. That is why he likes to play a funny game.
  4.  
  5. He builds a labyrinth of coins and tries to escape from it. You can think of the labyrinth as a rectangular field. Each cell of the field contains 0 or more coins.
  6.  
  7. When Scrooge McDuck steps on a cell, he can take only a single coin from this cell, and only if there are any coins. Scrooge McDuck can escape the field, only if he is surrounded by empty cells.
  8.  
  9. Scrooge McDuck always wants to go to the neighbouring cell with most coins. BUT if there are more than one cells with the same amount of coins (the largest), he chooses a cell (always the largest) from the order left, right, up, down
  10.  
  11. If Scrooge McDuck cannot go in any direction, he is out of the labyrinth
  12.  
  13. Examples
  14.  
  15. Scrooge McDuck is worried, not about his life, but if the coins he collect will be enough. Your task is to tell him how many coins he will collect, following the rules above.
  16.  
  17. Input
  18. Read from the standard input
  19. On the first line find N and M
  20. The size of the labyrinth
  21. On the next N lines find M integer values, separated by a space
  22. The input data will always be valid and there is no need to check it explicitly
  23. The starting location of Scrooge McDuck will be marked as the only 0
  24. Output
  25. Print to the standard output
  26. On the single line, print the number of coins Scrooge McDuck can collect, following the rules
  27. Contraints
  28. 2 <= N <= 10
  29. 2 <= M <= 10
  30. Each cell can contain up to 1024 coins
  31. Sample tests
  32. Input
  33. 4 3
  34. 3 2 4
  35. 2 0 3
  36. 1 1 5
  37. 2 2 5
  38. Output
  39. 22
  40. Input
  41. 3 3
  42. 10 10 0
  43. 10 10 10
  44. 10 10 10
  45. Output
  46.  
  47. package Recursion;
  48.  
  49. import java.util.Scanner;
  50.  
  51. public class McDuck {
  52. public static void main(String[] args) {
  53.  
  54. Scanner scanner = new Scanner(System.in);
  55. String[] input = scanner.nextLine().split(" ");
  56. int row = Integer.parseInt(input[0]);
  57. int col = Integer.parseInt(input[1]);
  58. int[][] matrix = new int[row][col];
  59. int startRow = 0;
  60. int startCow = 0;
  61.  
  62. for (int i = 0; i < row; i++) {
  63. for (int j = 0; j < col; j++) {
  64. matrix[i][j] = scanner.nextInt();
  65. if (matrix[i][j] == 0) {
  66. startRow = i;
  67. startCow = j;
  68. }
  69. }
  70. }
  71. System.out.println(solveMcDuck(matrix, startRow, startCow));
  72. }
  73. static int coins = 0;
  74. private static int solveMcDuck(int[][] matrix, int row, int col){
  75.  
  76. int left = 0;
  77. int right = 0;
  78. int up = 0;
  79. int down = 0;
  80.  
  81. if(outOfMatrix(matrix, row, col - 1) && matrix[row][col-1] > 0){
  82. left = matrix[row][col-1];
  83. }
  84. if(outOfMatrix(matrix, row, col + 1) && matrix[row][col +1] > 0){
  85. right = matrix[row][col+1];
  86. }
  87. if(outOfMatrix(matrix, row - 1, col) && matrix[row -1][col] > 0){
  88. up = matrix[row-1][col];
  89. }
  90. if(outOfMatrix(matrix, row + 1, col) && matrix[row+1][col] >0){
  91. down = matrix[row+1][col];
  92. }
  93. int[] maxCoins = new int[4];
  94. maxCoins[0] = left;
  95. maxCoins[1] = right;
  96. maxCoins[2] = up;
  97. maxCoins[3] = down;
  98.  
  99. int temp = 0;
  100. int index = -1;
  101.  
  102. for (int i = 0; i < maxCoins.length; i++) {
  103. if(maxCoins[i] > temp){
  104. temp = maxCoins[i];
  105. index = i;
  106. }
  107. }
  108. if(index == 0){
  109. coins++;
  110. matrix[row][col - 1]--;
  111. solveMcDuck(matrix, row, col-1);
  112. }
  113. if(index == 1){
  114. coins++;
  115. matrix[row][col + 1]--;
  116. solveMcDuck(matrix, row, col+1);
  117. }
  118. if(index == 2){
  119. coins++;
  120. matrix[row-1][col]--;
  121. solveMcDuck(matrix, row-1, col);
  122. }
  123. if(index == 3){
  124. coins++;
  125. matrix[row+1][col]--;
  126. solveMcDuck(matrix, row+1, col);
  127. }
  128. return coins;
  129. }
  130.  
  131. private static boolean outOfMatrix(int[][] matrix, int row, int col){
  132. return row < matrix.length && row >= 0 && col < matrix[0].length && col >= 0;
  133. }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment