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 userInput = new Scanner(System.in);
- int r = Integer.parseInt(userInput.nextLine());
- int c = Integer.parseInt(userInput.nextLine());
- int n = Integer.parseInt(userInput.nextLine());
- String[] values = userInput.nextLine().split("\\s+");
- double[] codes = new double[n];
- for (int i = 0; i < codes.length; i++) {
- codes[i] = Double.parseDouble(values[i]);
- }
- int[][] matrix = new int[r][c];
- int ro = -1;
- for (int row = r - 1; row >= 0; row--) {
- ro++;
- matrix[row][0] = (int) (Math.pow(2, ro + 1)) / 2;
- int column = 1;
- for (int col = (int) Math.pow(2, row); col <= (Math.pow(2, c - 1)) * ((Math.pow(2, row))) / 2; col *= 2) {
- matrix[ro][column++] = col * 2;
- }
- }
- boolean[][] visited = new boolean[r][c];
- int moves = 1;
- int currentRow = r - 1;
- int currentCol = 0;
- long sum = 0;
- while (moves <= n) {
- for (int i = 0; i < codes.length; i++) {
- int coefficient = Math.max(r, c);
- int row = (int)codes[i] / coefficient;
- int col = (int)codes[i] % coefficient;
- if (row >= r) {
- row = r - 1;
- }
- if (col >= c) {
- col = c - 1;
- }
- if(row < 0){
- row = 0;
- }
- if(col < 0){
- col = 0;
- }
- if (currentCol < col) {
- sum = getSumColRight(matrix[currentRow], visited[currentRow], currentCol, sum, col);
- currentCol = col;
- if (currentRow > row) {
- sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
- } else if (currentRow < row) {
- for (int j = currentRow + 1; j <= row; j++) {
- if (!visited[j][currentCol]) {
- visited[j][currentCol] = true;
- sum += matrix[j][currentCol];
- }
- }
- }
- } else if (currentCol > col) {
- for (int j = currentCol; j > col; j--) {
- if (!visited[currentRow][j]) {
- visited[currentRow][j] = true;
- sum += matrix[currentRow][j];
- }
- }
- currentCol = col;
- if (currentRow > row) {
- sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
- } else if (currentRow < row) {
- sum = getSumRowDown(matrix, visited, currentRow, currentCol, sum, row);
- }
- } else {
- if (currentRow > row) {
- sum = getSumRowUp(matrix, visited, currentRow, currentCol, sum, row);
- } else if (currentRow < row) {
- sum = getSumRowDown(matrix, visited, currentRow, currentCol, sum, row);
- }
- }
- currentRow = row;
- moves++;
- }
- }
- System.out.println(sum);
- }
- private static long getSumRowDown(int[][] matrix, boolean[][] visited, int currentRow, int currentCol, long sum, int row) {
- for (int j = currentRow + 1; j <= row; j++) {
- if (!visited[j][currentCol]) {
- visited[j][currentCol] = true;
- sum += matrix[j][currentCol];
- }
- }
- return sum;
- }
- private static long getSumRowUp(int[][] matrix, boolean[][] visited, int currentRow, int currentCol, long sum, int row) {
- for (int j = currentRow - 1; j >= row; j--) {
- if (!visited[j][currentCol]) {
- visited[j][currentCol] = true;
- sum += matrix[j][currentCol];
- }
- }
- return sum;
- }
- private static long getSumColRight(int[] matrix, boolean[] booleans, int currentCol, long sum, int col) {
- for (int j = currentCol; j <= col; j++) {
- if (!booleans[j]) {
- booleans[j] = true;
- sum += matrix[j];
- }
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement