Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class b {
- static class Kattio extends PrintWriter {
- private BufferedReader r;
- private StringTokenizer st;
- // standard input
- public Kattio() { this(System.in, System.out); }
- public Kattio(InputStream i, OutputStream o) {
- super(o);
- r = new BufferedReader(new InputStreamReader(i));
- }
- // USACO-style file input
- public Kattio(String problemName) throws IOException {
- super(problemName + ".out");
- r = new BufferedReader(new FileReader(problemName + ".in"));
- }
- // returns null if no more input
- public String next() {
- try {
- while (st == null || !st.hasMoreTokens())
- st = new StringTokenizer(r.readLine());
- return st.nextToken();
- } catch (Exception e) { }
- return null;
- }
- public int nextInt() { return Integer.parseInt(next()); }
- public double nextDouble() { return Double.parseDouble(next()); }
- public long nextLong() { return Long.parseLong(next()); }
- }
- public static void main(String[] args) throws Exception {
- Kattio io = new Kattio("cruise");
- int numPorts = io.nextInt();
- int numDirections = io.nextInt();
- int numRepeats = io.nextInt();
- HashMap<Integer, int[]> portMap = new HashMap<Integer, int[]>();
- for (int i=0; i<numPorts; i++) {
- int[] connectedPorts = new int[2];
- connectedPorts[0] = io.nextInt()-1;
- connectedPorts[1] = io.nextInt()-1;
- portMap.put(i, connectedPorts);
- }
- int[] directionArr = new int[numDirections];
- for (int i=0; i<numDirections; i++) {
- String nextDir = io.next();
- if (nextDir.equals("L")) {
- directionArr[i] = 0;
- }
- else {
- directionArr[i] = 1;
- }
- }
- HashMap<Integer, Integer> positionMap = new HashMap<Integer, Integer>();
- int currentPos = 0;
- for (int i=0; i<numRepeats; i++) {
- if(positionMap.containsKey(currentPos)) {
- int last = positionMap.get(currentPos);
- int len = i - last;
- int left = numRepeats - i;
- int ind = left % len;
- for(int l = 0; l < ind; ++l) {
- for (int j=0; j<numDirections; j++) {
- currentPos = portMap.get(currentPos)[directionArr[j]];
- }
- }
- break;
- } else {
- positionMap.put(currentPos, i);
- }
- for (int j=0; j<numDirections; j++) {
- currentPos = portMap.get(currentPos)[directionArr[j]];
- }
- }
- //System.out.println(positionMap);
- io.println(currentPos+1);
- io.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement