Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Navigation {
  4. public static void main(String[] args){
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. int rows = Integer.parseInt(scanner.nextLine());
  8. int cols = Integer.parseInt(scanner.nextLine());
  9.  
  10. int maxMoves = Integer.parseInt(scanner.nextLine());
  11. String[] codes = scanner.nextLine().split(" ");
  12.  
  13. int[][] matrix = new int[rows][cols];
  14. int currentPowerOfTwo = 1;
  15. for(int row = rows - 1; row >= 0; row--){
  16. for(int col = 0; col < cols; col++){
  17. matrix[row][col] = currentPowerOfTwo;
  18. currentPowerOfTwo *= 2;
  19. }
  20. currentPowerOfTwo = matrix[row][0] * 2;
  21. }
  22.  
  23. int coef = Math.max(rows, cols);
  24.  
  25. int currentRow = rows - 1;
  26. int currentCol = 0;
  27.  
  28. int sum = 1;
  29. for(int i = 0; i < codes.length; i++){
  30. int code = Integer.parseInt(codes[i]);
  31.  
  32. int colPurpose = code % coef;
  33. int rowPurpose = code / coef;
  34.  
  35. if(currentCol < colPurpose && colPurpose < cols){
  36. // horizontal sum
  37. for (int col = currentCol + 1; col <= colPurpose; col++) {
  38. sum += matrix[currentRow][col];
  39. }
  40. currentCol = colPurpose;
  41.  
  42. if(currentRow > rowPurpose && rowPurpose >= 0){
  43. // vertical sum
  44. for(int row = currentRow - 1; row >= rowPurpose; row--){
  45. sum += matrix[row][currentCol];
  46. }
  47. currentRow = rowPurpose;
  48. }else if (currentRow < rowPurpose && rowPurpose < rows){
  49. // vertical sum
  50. for(int row = currentRow + 1; row <= rowPurpose; row++){
  51. sum += matrix[row][currentCol];
  52. }
  53. currentRow = rowPurpose;
  54. }
  55. }else if (currentCol > colPurpose && colPurpose >= 0){
  56. // horizontal sum
  57. for (int col = currentCol - 1; col >= colPurpose; col--) {
  58. sum += matrix[currentRow][col];
  59. }
  60. currentCol = colPurpose;
  61.  
  62. if(currentRow > rowPurpose && rowPurpose >= 0){
  63. // vertical sum
  64. for(int row = currentRow - 1; row >= rowPurpose; row--){
  65. sum += matrix[row][currentCol];
  66. }
  67. currentRow = rowPurpose;
  68. }else if (currentRow < rowPurpose && rowPurpose < rows){
  69. // vertical sum
  70. for(int row = currentRow + 1; row <= rowPurpose; row++){
  71. sum += matrix[row][currentCol];
  72. }
  73. currentRow = rowPurpose;
  74. }
  75. }
  76.  
  77.  
  78.  
  79.  
  80. // System.out.println(rowPurpose + " " + colPurpose);
  81. }
  82.  
  83. System.out.print(sum);
  84.  
  85. /* for(int row = 0; row < rows; row++){
  86. for(int col = 0; col < cols; col++){
  87. System.out.print(matrix[row][col] + " ");
  88. }
  89. System.out.println();
  90. }
  91. */
  92.  
  93.  
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement