Advertisement
Guest User

ScroogeMcDuck

a guest
Dec 9th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. import java.util.Locale;
  6. import java.util.Scanner;
  7.  
  8. public class ScroogeMcDuck {
  9. public static void main(String[] args) throws IOException {
  10. Scanner input = new Scanner(System.in).useLocale(Locale.US);
  11.  
  12. int[] coordinates = Arrays.stream(input.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  13. int row = coordinates[0];
  14. int column = coordinates[1];
  15. int[][] field = new int[row][column];
  16.  
  17. for (int i = 0; i < row; i++) {
  18. for (int j = 0; j < column; j++) {
  19. field[i][j] = input.nextInt();
  20. if (field[i][j] == 0) {
  21. coordinates[0] = i;
  22. coordinates[1] = j;
  23. }
  24. }
  25. }
  26.  
  27. boolean exit = false;
  28. int sum = 0;
  29. while (!exit) {
  30. int x = coordinates[0];
  31. int y = coordinates[1];
  32.  
  33. int left = 0;
  34. int right = 0;
  35. int up = 0;
  36. int down = 0;
  37.  
  38. if (x - 1 >= 0) {
  39. up = field[x - 1][y];
  40. } else {
  41. up = -1;
  42. }
  43. if (x + 1 <=row-1) {
  44. down = field[x + 1][y];
  45. } else {
  46. down = -1;
  47. }
  48. if (y - 1 >= 0) {
  49. left = field[x][y-1];
  50. } else {
  51. left = -1;
  52. }
  53. if (y + 1 <= column-1) {
  54. right = field[x][y+1];
  55. } else {
  56. right = -1;
  57. }
  58.  
  59.  
  60.  
  61. String bestMove = calculateMax(up, down, left, right);
  62. if (bestMove == "wrong") {
  63. int secondBest = calculateEqual(up, down, left, right);
  64. if (left == secondBest) {
  65. bestMove = "left";
  66.  
  67. } else if (right == secondBest) {
  68. bestMove = "right";
  69.  
  70. } else if (up == secondBest) {
  71. bestMove = "up";
  72.  
  73. } else if (down == secondBest) {
  74. bestMove = "down";
  75.  
  76. }
  77. }
  78.  
  79. switch (bestMove) {
  80. case "up":
  81. x--;
  82. break;
  83. case "down":
  84. x++;
  85. break;
  86. case "left":
  87. y--;
  88. break;
  89. case "right":
  90. y++;
  91. break;
  92. }
  93. if (field[x][y] == 0) {
  94. exit = true;
  95. } else {
  96. coordinates[0] = x;
  97. coordinates[1] = y;
  98. field[x][y]--;
  99. sum++;
  100. }
  101.  
  102. }
  103. System.out.println(sum);
  104.  
  105. }
  106.  
  107. public static String calculateMax(int a, int b, int c, int d) {
  108. if (a > b && a > c && a > d) {
  109. return "up";
  110. } else if (b > a && b > c && b > d) {
  111. return "down";
  112. } else if (c > b && c > a && c > d) {
  113. return "left";
  114. } else if (d > b && d > c && d > a) {
  115. return "right";
  116. } else {
  117. return "wrong";
  118. }
  119. }
  120.  
  121. public static int calculateEqual(int a, int b, int c, int d) {
  122. if (a >= b && a >= c && a >= d) {
  123. return a;
  124. } else if (b >= a && b >= c && b >= d) {
  125. return b;
  126. } else if (c >= b && c >= a && c >= d) {
  127. return c;
  128. } else {
  129. return d;
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement