Advertisement
Guest User

Untitled

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