Advertisement
Mishakis

MoveAround

Dec 3rd, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.io.ByteArrayInputStream;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     static void fakeInput() {
  6.         String test = "6 7 3\n" +
  7.                 "0 0\n" +
  8.                 "2 2\n" +
  9.                 "-2 2\n" +
  10.                 "3 -1";
  11.         System.setIn(new ByteArrayInputStream(test.getBytes()));
  12.     }
  13.  
  14.  
  15.     public static void main(String[] args) {
  16.         fakeInput();
  17.         Scanner scanner = new Scanner(System.in);
  18.  
  19.         int N = scanner.nextInt();
  20.         int M = scanner.nextInt();
  21.  
  22.         int[][] field = new int[N][M];
  23.  
  24.         int startValue = 1;
  25.  
  26.  
  27.         int row = 0;
  28.         int col = 0;
  29.  
  30.         for (row = 0; row < field.length; row++) {
  31.             for (col = 0; col < field[row].length; col++) {
  32.                 field[row][col] = startValue;
  33.                 startValue++;
  34.             }
  35.  
  36.         }
  37.  
  38.  
  39.         int J = scanner.nextInt();
  40.  
  41.  
  42.         int R = scanner.nextInt();
  43.         int C = scanner.nextInt();
  44.  
  45.  
  46.         long sum = field[R][C];
  47.         int jumps = 0;
  48.         field[R][C] = 0;
  49.  
  50.         boolean escaped = true;
  51.  
  52.         int[] RowMoves = new int[J];
  53.         int[] Colmoves = new int[J];
  54.  
  55.         for (int i = 0; i < J; i++) {
  56.  
  57.  
  58.             int RowMove = scanner.nextInt();
  59.  
  60.             int ColMove = scanner.nextInt();
  61.  
  62.             RowMoves[i] = RowMove;
  63.             Colmoves[i] = ColMove;
  64.  
  65.             R += RowMove;
  66.             C += ColMove;
  67.  
  68.  
  69.  
  70.             if (R >= N || C >= M) {
  71.                 escaped = true;
  72.                 break;
  73.             } else if (field[R][C] == 0) {
  74.                 escaped = false;
  75.                 break;
  76.             }
  77.  
  78.             sum += field[R][C];
  79.             jumps++;
  80.             field[R][C] = 0;
  81.  
  82.  
  83.             if (i + 1 == J) {
  84.                 for (int k = 0; k < J; k++) {
  85.  
  86.                     RowMove = RowMoves[k];
  87.                     ColMove = Colmoves[k];
  88.  
  89.                     R += RowMove;
  90.                     C += ColMove;
  91.  
  92.  
  93.  
  94.                     if (R >= N || C >= M) {
  95.                         escaped = true;
  96.                         break;
  97.                     } else if (field[R][C] == 0) {
  98.                         escaped = false;
  99.                         break;
  100.                     }
  101.  
  102.                     sum += field[R][C];
  103.                     jumps++;
  104.                     field[R][C] = 0;
  105.  
  106.  
  107.                 }
  108.             }
  109.         }
  110.         if(escaped == true){
  111.             System.out.println("escaped" + " " + sum);
  112.         }else if(escaped == false){
  113.             System.out.println("caught" + " " + jumps);
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement