06/10/2013

11507 - Bender B. Rodríguez Problem - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.util.*;  
 public class BenderTwo {  
      public static int[][] table = {{9, 9, 9, 9, 9, 9},  
                                              {9, 9, 9, 9, 9, 9},  
                                              {2, 3, 1, 0, 4, 5},  
                                              {3, 2, 0, 1, 4, 5},  
                                              {4, 5, 2, 3, 1, 0},  
                                              {5, 4, 2, 3, 0, 1}};  
      public static String[] coder = {"+x", "-x", "+y", "-y", "+z", "-z"};  
      public static void main(String[] args) throws Exception {  
           // FileWriter writer = new FileWriter("output.out");  
           // BufferedWriter out = new BufferedWriter(writer);  
           StringBuilder sb = new StringBuilder();  
           Reader.init(System.in);  
           // Reader.init(new FileInputStream(new File("input.in")));  
           int lenght = Reader.nextInt();  
           while (lenght > 0) {  
                int pos = 0;  
                for (int i = 1; i < lenght; i++) {  
                     String tmp = Reader.next();  
                     if (tmp.charAt(0) != 'N') pos = doStuff(tmp, pos);  
                }  
                sb.append(decode(pos)).append("\n");  
                lenght = Reader.nextInt();  
           }  
           System.out.print(sb);  
           // out.write(sb.toString());  
           // out.close();  
      }  
      public static int encode(char sign, char axis) {  
           if(sign == '+') {  
                if(axis == 'x') return 0;  
                if(axis == 'y') return 2;  
                return 4;  
           }  
           if(axis == 'x') return 1;  
           if(axis == 'y') return 3;  
           return 5;            
      }  
      public static String decode(int num) {  
           return coder[num];  
      }  
      public static int doStuff(String temp, int wire) {  
           int val = encode(temp.charAt(0), temp.charAt(1));  
           return table[val][wire];  
      }  
 }  
 /** 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()) {  
                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. Q: Why do programmers always mix up Halloween and Christmas?

    A: Because Oct 31 == Dec 25!

    ReplyDelete