Advertisement
saurav_kalsoor

Robot - JAVA

Jul 17th, 2022
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  2. // Robot - JAVA
  3.  
  4. import java.util.*;
  5.  
  6. public class Test {
  7.     static Scanner sc = new Scanner(System.in);
  8.     public static void main(String[] args) {
  9.         int n = sc.nextInt();
  10.         int x = sc.nextInt();
  11.         int y = sc.nextInt();
  12.  
  13.         ArrayList<Integer> arr = new ArrayList<>();
  14.         for(int i = 0;i < n; i++){
  15.             arr.add(sc.nextInt());
  16.         }
  17.         robot(arr, x, y);
  18.     }
  19.  
  20.     public static void robot(ArrayList<Integer> moves, int x, int y) {
  21.         HashSet<String> set = new HashSet<>();
  22.         set.add("0 0");
  23.  
  24.         int x_dir = 0, y_dir = 0, x_val = 0, y_val = 0;
  25.         for(int i = 0;i < moves.size(); i++){
  26.             switch(moves.get(i)){
  27.                 case 0:
  28.                     x_dir = 0;
  29.                     y_dir = 1;
  30.                     break;
  31.                 case 2:
  32.                     x_dir = 0;
  33.                     y_dir = -1;
  34.                     break;
  35.                 case 1:
  36.                     x_dir = 1;
  37.                     y_dir = 0;
  38.                     break;
  39.                 case 3:
  40.                     x_dir = -1;
  41.                     y_dir = 0;
  42.                     break;
  43.             }
  44.             x_val += x_dir;
  45.             y_val += y_dir;
  46.             if(set.contains(x_val + " " + y_val)){
  47.                 i--;
  48.             }else{
  49.                 set.add(x_val + " " + y_val);
  50.             }
  51.         }
  52.        
  53.         System.out.println((x_val + x) + " " + (y_val + y));
  54.     }
  55.    
  56. }
  57.  
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement