Advertisement
a_chn

Untitled

Dec 3rd, 2023
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class b {
  5.     static class Kattio extends PrintWriter {
  6.         private BufferedReader r;
  7.         private StringTokenizer st;
  8.         // standard input
  9.         public Kattio() { this(System.in, System.out); }
  10.         public Kattio(InputStream i, OutputStream o) {
  11.             super(o);
  12.             r = new BufferedReader(new InputStreamReader(i));
  13.         }
  14.         // USACO-style file input
  15.         public Kattio(String problemName) throws IOException {
  16.             super(problemName + ".out");
  17.             r = new BufferedReader(new FileReader(problemName + ".in"));
  18.         }
  19.         // returns null if no more input
  20.         public String next() {
  21.             try {
  22.                 while (st == null || !st.hasMoreTokens())
  23.                     st = new StringTokenizer(r.readLine());
  24.                 return st.nextToken();
  25.             } catch (Exception e) { }
  26.             return null;
  27.         }
  28.         public int nextInt() { return Integer.parseInt(next()); }
  29.         public double nextDouble() { return Double.parseDouble(next()); }
  30.         public long nextLong() { return Long.parseLong(next()); }
  31.     }
  32.     public static void main(String[] args) throws Exception {
  33.         Kattio io = new Kattio("cruise");
  34.         int numPorts = io.nextInt();
  35.         int numDirections = io.nextInt();
  36.         int numRepeats = io.nextInt();
  37.         HashMap<Integer, int[]> portMap = new HashMap<Integer, int[]>();
  38.         for (int i=0; i<numPorts; i++) {
  39.             int[] connectedPorts = new int[2];
  40.             connectedPorts[0] = io.nextInt()-1;
  41.             connectedPorts[1] = io.nextInt()-1;
  42.             portMap.put(i, connectedPorts);
  43.         }
  44.         int[] directionArr = new int[numDirections];
  45.         for (int i=0; i<numDirections; i++) {
  46.             String nextDir = io.next();
  47.             if (nextDir.equals("L")) {
  48.                 directionArr[i] = 0;
  49.             }
  50.             else {
  51.                 directionArr[i] = 1;
  52.             }
  53.         }
  54.         HashMap<Integer, Integer> positionMap = new HashMap<Integer, Integer>();
  55.         int currentPos = 0;
  56.         for (int i=0; i<numRepeats; i++) {
  57.             if(positionMap.containsKey(currentPos)) {
  58.                 int last = positionMap.get(currentPos);
  59.                 int len = i - last;
  60.                 int left = numRepeats - i;
  61.                 int ind = left % len;
  62.                 for(int l = 0; l < ind; ++l) {
  63.                     for (int j=0; j<numDirections; j++) {
  64.                         currentPos = portMap.get(currentPos)[directionArr[j]];
  65.                     }
  66.                 }
  67.                 break;
  68.             } else {
  69.                 positionMap.put(currentPos, i);
  70.             }
  71.             for (int j=0; j<numDirections; j++) {
  72.                 currentPos = portMap.get(currentPos)[directionArr[j]];
  73.             }
  74.         }
  75.         //System.out.println(positionMap);
  76.         io.println(currentPos+1);
  77.         io.close();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement