본문 바로가기
Problem Solving/Baekjoon

[백준] 23568 Find the House - Data Structure / Java

by graycode 2023. 7. 1.

 문제 링크

 

23568번: Find the House

Your program is to read from standard input. The input starts with a line containing an integer $n$ ($1 ≤ n ≤ 10,000$), where $n$ is the number of triples in the list. In the following $n$ lines, $n$ triples are given where each triple is represented a

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());

        Map<Integer, Pair> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            map.put(Integer.parseInt(st.nextToken()), new Pair(st.nextToken(), Integer.parseInt(st.nextToken())));
        }

        int cur = Integer.parseInt(br.readLine());
        while (n-- > 0) {
            Pair pair = map.get(cur);
            if (pair.dir.equals("L")) cur -= pair.dist;
            else cur += pair.dist;
        }

        bw.write(String.valueOf(cur));
        bw.flush();
    }

    private static class Pair {
        String dir;
        int dist;

        public Pair(String dir, int dist) {
            this.dir = dir;
            this.dist = dist;
        }
    }

}

댓글