Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- public void solution() throws IOException {
- int q = in.nextInt();
- while (q-- > 0) {
- int n = in.nextInt();
- double x = 0;
- double y = 0;
- double dx = 1;
- double dy = 0;
- double angle = 0;
- for (int i = 0; i < n; ++i) {
- String cmd = in.next();
- int val = in.nextInt();
- if (cmd.equals("fd")) {
- x += val * dx;
- y += val * dy;
- } else if (cmd.equals("bk")) {
- x -= val * dx;
- y -= val * dy;
- } else if (cmd.equals("lt")) {
- angle += Math.toRadians(val);
- dx = Math.cos(angle);
- dy = Math.sin(angle);
- } else if (cmd.equals("rt")) {
- angle -= Math.toRadians(val);
- dx = Math.cos(angle);
- dy = Math.sin(angle);
- }
- }
- out.println((int) Math.round(Math.sqrt(x * x + y * y)));
- }
- out.flush();
- }
- private class Scanner {
- BufferedReader reader;
- StringTokenizer tokenizer;
- public Scanner(BufferedReader reader) {
- this.reader = reader;
- this.tokenizer = new StringTokenizer("");
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String next = reader.readLine();
- if (next == null) {
- return false;
- }
- tokenizer = new StringTokenizer(next);
- }
- return true;
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- public double nextDouble() throws IOException {
- return Double.parseDouble(next());
- }
- }
- public static void main(String[] args) throws IOException {
- new Main().solution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement