Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Navigation {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- int rows = Integer.parseInt(scanner.nextLine());
- int cols = Integer.parseInt(scanner.nextLine());
- int maxMoves = Integer.parseInt(scanner.nextLine());
- String[] codes = scanner.nextLine().split(" ");
- int[][] matrix = new int[rows][cols];
- int currentPowerOfTwo = 1;
- for(int row = rows - 1; row >= 0; row--){
- for(int col = 0; col < cols; col++){
- matrix[row][col] = currentPowerOfTwo;
- currentPowerOfTwo *= 2;
- }
- currentPowerOfTwo = matrix[row][0] * 2;
- }
- int coef = Math.max(rows, cols);
- int currentRow = rows - 1;
- int currentCol = 0;
- int sum = 1;
- for(int i = 0; i < codes.length; i++){
- int code = Integer.parseInt(codes[i]);
- int colPurpose = code % coef;
- int rowPurpose = code / coef;
- if(currentCol < colPurpose && colPurpose < cols){
- // horizontal sum
- for (int col = currentCol + 1; col <= colPurpose; col++) {
- sum += matrix[currentRow][col];
- }
- currentCol = colPurpose;
- if(currentRow > rowPurpose && rowPurpose >= 0){
- // vertical sum
- for(int row = currentRow - 1; row >= rowPurpose; row--){
- sum += matrix[row][currentCol];
- }
- currentRow = rowPurpose;
- }else if (currentRow < rowPurpose && rowPurpose < rows){
- // vertical sum
- for(int row = currentRow + 1; row <= rowPurpose; row++){
- sum += matrix[row][currentCol];
- }
- currentRow = rowPurpose;
- }
- }else if (currentCol > colPurpose && colPurpose >= 0){
- // horizontal sum
- for (int col = currentCol - 1; col >= colPurpose; col--) {
- sum += matrix[currentRow][col];
- }
- currentCol = colPurpose;
- if(currentRow > rowPurpose && rowPurpose >= 0){
- // vertical sum
- for(int row = currentRow - 1; row >= rowPurpose; row--){
- sum += matrix[row][currentCol];
- }
- currentRow = rowPurpose;
- }else if (currentRow < rowPurpose && rowPurpose < rows){
- // vertical sum
- for(int row = currentRow + 1; row <= rowPurpose; row++){
- sum += matrix[row][currentCol];
- }
- currentRow = rowPurpose;
- }
- }
- // System.out.println(rowPurpose + " " + colPurpose);
- }
- System.out.print(sum);
- /* for(int row = 0; row < rows; row++){
- for(int col = 0; col < cols; col++){
- System.out.print(matrix[row][col] + " ");
- }
- System.out.println();
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement