11/08/2013

12503 - Robot Instructions - Java

 import java.io.BufferedReader;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.InputStreamReader;  
 import java.util.StringTokenizer;  
 public class RobotInstructions {  
      public static void main(String[] args) throws Exception {  
           Reader.init(System.in);  
 //          Reader.init(new FileInputStream(new File("input.in")));  
           int noOfTestCases = Reader.nextInt();  
           while((noOfTestCases--) > 0) {  
                int noOfInstructions = Reader.nextInt();  
                int currentPos = 0;  
                boolean[] instructions = new boolean[noOfInstructions];  
                for(int i = 0; i < noOfInstructions; i++) {  
                     String currentInstruction = Reader.next();  
                     if(currentInstruction.startsWith("L")) {  
                          instructions[i] = false;  
                          currentPos--;  
                     } else if(currentInstruction.startsWith("R")) {  
                          instructions[i] = true;  
                          currentPos++;  
                     } else {  
                          Reader.next();  
                          int tmp = Reader.nextInt();  
                          instructions[i] = instructions[tmp-1];  
                          if(instructions[i] == false) currentPos--;  
                          else currentPos++;  
                     }  
                }  
                System.out.println(currentPos);                 
           }  
      }  
 }  
 ///** Class for buffered reading int and double values */  
 ///** http://www.cpe.ku.ac.th/~jim/java-io.html */  
 class Reader {  
      static BufferedReader reader;  
      static StringTokenizer tokenizer;  
      /** call this method to initialize reader for InputStream */  
      static void init(InputStream input) {  
           reader = new BufferedReader(new InputStreamReader(input));  
           tokenizer = new StringTokenizer("");  
      }  
      /** get next word */  
      static String next() throws IOException {  
           while ( ! tokenizer.hasMoreTokens() ) {  
                //TODO add check for eof if necessary  
                tokenizer = new StringTokenizer(reader.readLine());  
           }  
           return tokenizer.nextToken();  
      }  
      static int nextInt() throws IOException {  
           return Integer.parseInt( next() );  
      }  
      static double nextDouble() throws IOException {  
           return Double.parseDouble( next() );  
      }  
 }  

1 comment:


  1. A foo walks into a bar, takes a look around and says "Hello World!" and meet up his friend Baz

    ReplyDelete