Algorithm/baekjoon

소가 길을 건너간 이유 1

마닐라 2022. 3. 3. 14:44

📍 문제 설명

https://www.acmicpc.net/problem/14467

 

14467번: 소가 길을 건너간 이유 1

3번 소는 위치 1, 0, 1에서 관찰되었으므로 길을 최소 두 번 건넜음을 확인할 수 있다. 4번 소도 길을 한 번 건넜으며, 나머지 소는 길을 건넌 기록이 확인되지 않는다.

www.acmicpc.net

 

💡 접근

맵을 이용해서 소가 길을 건넜는지 확인해주고 소의 위치를 변경해주면된다.

 

👩‍💻 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int n, m, v, e, k, r, t, answer, cnt, sum, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE, firstX, firstY;
    static int[] arr, dx = {1,0,-1,0}, dy = {0,-1,0,1};
    static long[] dp;
    static int[][] board, clone, nearEmptySeatCnt;
    static boolean[][] visited;
    static ArrayList<Integer> list;
    static HashMap<Integer, Integer> map;
    static HashSet<Integer> set;
    static StringBuilder sb;
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {
        Main T = new Main();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        map = new HashMap<>();
        arr = new int[11];

        for(int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int cowNumber = Integer.parseInt(st.nextToken());
            int location = Integer.parseInt(st.nextToken());

            //처음 소의 위치인지 확인
            if(!map.containsKey(cowNumber)) {
                map.put(cowNumber, location);
                continue;
            }

            //길을 건넌건지 확인
            if(map.get(cowNumber) != location) {
                arr[cowNumber]++;
            }

            //해당 위치로 소의 위치 변경
            map.put(cowNumber, location);

        }

        for(int i = 1; i <= 10; i++) {
            sum += arr[i];
        }
        System.out.println(sum);
    }

    private void solution() {

    }


}