๊ธฐ๋ก๋ฐฉ

BOJ_16928 : ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_16928 : ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

Soom_1n 2023. 4. 7. 16:59

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

16928๋ฒˆ: ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

์ฒซ์งธ ์ค„์— ๊ฒŒ์ž„ํŒ์— ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ˆ˜ N(1 ≤ N ≤ 15)๊ณผ ๋ฑ€์˜ ์ˆ˜ M(1 ≤ M ≤ 15)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š” x, y (x < y)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. x๋ฒˆ ์นธ์— ๋„์ฐฉํ•˜๋ฉด, y๋ฒˆ ์นธ์œผ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • 10 x 10 ๊ฒŒ์ž„ํŒ์—์„œ 100๊ฐœ์˜ ์นธ ์ค‘ ์ฒซ ์นธ์—์„œ ๋งˆ์ง€๋ง‰ ์นธ ๊นŒ์ง€ ๊ฐ€์•ผํ•œ๋‹ค.
    • ์ฃผ์‚ฌ์œ„ ๊ฐ’์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ๋˜์งˆ ์ˆ˜ ์žˆ๋‹ค.
    • ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ๋งŒ๋‚˜๋ฉด ์—ฐ๊ฒฐ ๋œ ์นธ์œผ๋กœ ๋‚ด๋ ค๊ฐ€๊ณ , ๋ฑ€์„ ๋งŒ๋‚˜๋ฉด ์—ฐ๊ฒฐ ๋œ ์นธ์œผ๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค.
    • ์‚ฌ๋‹ค๋ฆฌ์™€ ๋ฑ€์ด ๊ฒน์น˜๊ฑฐ๋‚˜ ์—ฐ์† ๋œ ๊ณณ์€ ์—†๋‹ค.
  • ์ฃผ์‚ฌ์œ„ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ BFS๋กœ ๋ชจ๋‘ ํ™•์ธํ•˜๋ฉฐ ๋„์ฐฉํ•˜๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    private static class Game {
        int num, cnt;

        public Game(int num, int cnt) {
            this.num = num;
            this.cnt = cnt;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int[] map = new int[101];
        boolean[] visited = new boolean[101];

        for (int i = 0; i <= 100; i++) {
            map[i] = i;
        }

        for (int i = 0; i < N + M; i++) {
            st = new StringTokenizer(br.readLine());
            map[Integer.parseInt(st.nextToken())] = Integer.parseInt(st.nextToken());
        }

        Queue<Game> que = new ArrayDeque<>();
        int now = 1;
        int cnt = 0;

        que.offer(new Game(now, cnt));

        while (!que.isEmpty()) {
            Game game = que.poll();
            now = game.num;
            cnt = game.cnt;

            if (now == 100) {
                break;
            }

            visited[now] = true;

            for (int i = 1; i <= 6; i++) {
                if (now + i <= 100 && !visited[now + i]) {
                    que.offer(new Game(map[now + i], cnt + 1));
                }
            }
        }
        System.out.println(cnt);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ๊ตฌ๋ถ„ ํ•  ํ•„์š” ์—†์ด, ์นธ์„ ๋ฐŸ์œผ๋ฉด ํ•ด๋‹น ๊ฐ’์˜ ์ธ๋ฑ์Šค๋กœ ์ด๋™ํ•˜๋ฉด ๋œ๋‹ค.
  • BFS์—์„œ 100์นธ์— ๋„์ฐฉ ํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ์ด๋™ ํšŸ์ˆ˜๋ฅผ Game ๊ฐ์ฒด์— ์ €์žฅํ•œ๋‹ค..

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ €์žฅ ํ•  ํ•„์š” ์—†์ด, ํ ์‚ฌ์ด์ฆˆ ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ์‹์œผ๋กœ BFS๋ฅผ ๊ตฌํ˜„ํ–ˆ์œผ๋ฉด Game ํด๋ž˜์Šค๋„ ๋งŒ๋“ค ํ•„์š” ์—†์ด ๋” ๊น”๋”ํ•œ ์ฝ”๋“œ๊ฐ€ ๋์„ ๊ฒƒ ๊ฐ™๋‹ค.

728x90