Advertisement
Guest User

Untitled

a guest
May 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.    
  6.     Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  7.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  8.    
  9.     public void solution() throws IOException {
  10.         int q = in.nextInt();
  11.         while (q-- > 0) {
  12.             int n = in.nextInt();
  13.             double x = 0;
  14.             double y = 0;
  15.             double dx = 1;
  16.             double dy = 0;
  17.             double angle = 0;
  18.             for (int i = 0; i < n; ++i) {
  19.                 String cmd = in.next();
  20.                 int val = in.nextInt();
  21.                 if (cmd.equals("fd")) {
  22.                     x += val * dx;
  23.                     y += val * dy;
  24.                 } else if (cmd.equals("bk")) {
  25.                     x -= val * dx;
  26.                     y -= val * dy;
  27.                 } else if (cmd.equals("lt")) {
  28.                     angle += Math.toRadians(val);
  29.                     dx = Math.cos(angle);
  30.                     dy = Math.sin(angle);
  31.                 } else if (cmd.equals("rt")) {
  32.                     angle -= Math.toRadians(val);
  33.                     dx = Math.cos(angle);
  34.                     dy = Math.sin(angle);
  35.                 }
  36.             }
  37.             out.println((int) Math.round(Math.sqrt(x * x  + y * y)));
  38.         }
  39.         out.flush();
  40.     }
  41.  
  42.     private class Scanner {
  43.         BufferedReader reader;
  44.         StringTokenizer tokenizer;
  45.        
  46.         public Scanner(BufferedReader reader) {
  47.             this.reader = reader;
  48.             this.tokenizer = new StringTokenizer("");
  49.         }
  50.        
  51.         public boolean hasNext() throws IOException {
  52.             while (!tokenizer.hasMoreTokens()) {
  53.                 String next = reader.readLine();
  54.                 if (next == null) {
  55.                     return false;
  56.                 }
  57.                 tokenizer = new StringTokenizer(next);
  58.             }
  59.             return true;
  60.         }
  61.        
  62.         public String next() throws IOException {
  63.             hasNext();
  64.             return tokenizer.nextToken();
  65.         }
  66.        
  67.         public int nextInt() throws IOException {
  68.             return Integer.parseInt(next());
  69.         }
  70.        
  71.         public double nextDouble() throws IOException {
  72.             return Double.parseDouble(next());
  73.         }
  74.        
  75.     }
  76.    
  77.     public static void main(String[] args) throws IOException {
  78.         new Main().solution();
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement