Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Navigation {
  4. public static void main(String[] args) {
  5. Scanner userInput = new Scanner(System.in);
  6.  
  7. int r = Integer.parseInt(userInput.nextLine());
  8. int c = Integer.parseInt(userInput.nextLine());
  9. int n = Integer.parseInt(userInput.nextLine());
  10.  
  11. String[] values = userInput.nextLine().split("\\s+");
  12. double[] codes = new double[n];
  13.  
  14. for (int i = 0; i < codes.length; i++) {
  15. codes[i] = Double.parseDouble(values[i]);
  16. }
  17. int[][] matrix = new int[r][c];
  18. int ro = -1;
  19.  
  20. for (int row = r - 1; row >= 0; row--) {
  21. ro++;
  22. matrix[row][0] = (int) (Math.pow(2, ro + 1)) / 2;
  23.  
  24. int column = 1;
  25. for (int col = (int) Math.pow(2, row); col <= (Math.pow(2, c - 1)) * ((Math.pow(2, row))) / 2; col *= 2) {
  26. matrix[ro][column++] = col * 2;
  27. }
  28. }
  29.  
  30. boolean[][] visited = new boolean[r][c];
  31.  
  32. int moves = 1;
  33. int currentRow = r - 1;
  34. int currentCol = 0;
  35. long sum = 0;
  36.  
  37. while (moves <= n) {
  38. for (int i = 0; i < codes.length; i++) {
  39. int coefficient = Math.max(r, c);
  40. int row = (int)codes[i] / coefficient;
  41. int col = (int)codes[i] % coefficient;
  42. if (row >= r) {
  43. row = r - 1;
  44. }
  45. if (col >= c) {
  46. col = c - 1;
  47. }
  48. if(row < 0){
  49. row = 0;
  50. }
  51. if(col < 0){
  52. col = 0;
  53. }
  54. if (currentCol < col) {
  55. sum = getSumColRight(matrix[currentRow], visited[currentRow], currentCol, sum, col);
  56. currentCol = col;
  57. if (currentRow > row) {
  58. sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
  59. } else if (currentRow < row) {
  60. for (int j = currentRow + 1; j <= row; j++) {
  61. if (!visited[j][currentCol]) {
  62. visited[j][currentCol] = true;
  63. sum += matrix[j][currentCol];
  64. }
  65. }
  66. }
  67.  
  68. } else if (currentCol > col) {
  69. for (int j = currentCol; j > col; j--) {
  70. if (!visited[currentRow][j]) {
  71. visited[currentRow][j] = true;
  72. sum += matrix[currentRow][j];
  73. }
  74. }
  75. currentCol = col;
  76. if (currentRow > row) {
  77. sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
  78. } else if (currentRow < row) {
  79. sum = getSumRowDown(matrix, visited, currentRow, currentCol, sum, row);
  80. }
  81.  
  82. } else {
  83. if (currentRow > row) {
  84. sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
  85. } else if (currentRow < row) {
  86. sum = getSumRowDown(matrix, visited, currentRow, currentCol, sum, row);
  87. }
  88. }
  89. currentRow = row;
  90.  
  91.  
  92. moves++;
  93. }
  94.  
  95. }
  96.  
  97. System.out.println(sum);
  98. }
  99.  
  100. private static long getSumRowDown(int[][] matrix, boolean[][] visited, int currentRow, int currentCol, long sum, int row) {
  101. for (int j = currentRow + 1; j <= row; j++) {
  102. if (!visited[j][currentCol]) {
  103. visited[j][currentCol] = true;
  104. sum += matrix[j][currentCol];
  105. }
  106. }
  107. return sum;
  108. }
  109.  
  110. private static long getSumRowUp(int[][] matrix, boolean[][] visited, int currentRow, int currentCol, long sum, int row) {
  111. for (int j = currentRow - 1; j >= row; j--) {
  112. if (!visited[j][currentCol]) {
  113. visited[j][currentCol] = true;
  114. sum += matrix[j][currentCol];
  115. }
  116. }
  117. return sum;
  118. }
  119.  
  120. private static long getSumColRight(int[] matrix, boolean[] booleans, int currentCol, long sum, int col) {
  121. for (int j = currentCol; j <= col; j++) {
  122. if (!booleans[j]) {
  123. booleans[j] = true;
  124. sum += matrix[j];
  125. }
  126. }
  127. return sum;
  128. }
  129.  
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement