๊ธฐ๋ก๋ฐฉ

BOJ_1600 : ๋ง์ด ๋˜๊ณ ํ”ˆ ์›์ˆญ์ด ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1600 : ๋ง์ด ๋˜๊ณ ํ”ˆ ์›์ˆญ์ด

Soom_1n 2023. 4. 6. 16:01

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

 

1600๋ฒˆ: ๋ง์ด ๋˜๊ณ ํ”ˆ ์›์ˆญ์ด

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ๊ฒฉ์žํŒ์˜ ๊ฐ€๋กœ๊ธธ์ด W, ์„ธ๋กœ๊ธธ์ด H๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ H์ค„์— ๊ฑธ์ณ W๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, 0์€ ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ํ‰์ง€, 1์€ ์žฅ์• ๋ฌผ์„ ๋œปํ•œ๋‹ค. ์žฅ์• ๋ฌผ์ด ์žˆ

www.acmicpc.net



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

  • H x W ๊ฒฉ์ž ํŒ์—์„œ ์™ผ์ชฝ ์œ„ ๋ ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋ ๊นŒ์ง€ ๊ฐ€๋Š” ๋™์ž‘์˜ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์›์ˆญ์ด์˜ ์›€์ง์ž„ : ์ธ์ ‘ 4์นธ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅ
    • ๋ง์˜ ์›€์ง์ž„ : k๋ฒˆ ๋งŒํผ ์ด๋™ ๊ฐ€๋Šฅ
  • BFS๋กœ 2๊ฐ€์ง€ ํ’€์ด๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
    1. ๋ง ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ ๊ฒŒ ์“ธ ์ˆ˜๋ก ์ข‹๋‹ค.(๊ทธ๋ฆฌ๋””)
    2. 3์ค‘ ๋ฐฐ์—ด๋กœ ๋ฐฉ๋ฌธ์„ ์ฒดํฌํ•œ๋‹ค.

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

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

public class Main {
    private static int K, H, W;
    private static int[][] map;

    private static class Monkey {
        int r, c, horse;

        public Monkey(int r, int c, int horse) {
            this.r = r;
            this.c = c;
            this.horse = horse;
        }

        @Override
        public String toString() {
            return "Monkey{" +
                    "r=" + r +
                    ", c=" + c +
                    ", horse=" + horse +
                    '}';
        }
    }

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        K = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        W = Integer.parseInt(st.nextToken());
        H = Integer.parseInt(st.nextToken());
        map = new int[H][W];

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

        //BFS + Output
        System.out.println(bfs());
    }

    private static int bfs() {
        int[] dr = {-1, 0, 1, 0, -2, -1, 1, 2, 2, 1, -1, -2};
        int[] dc = {0, 1, 0, -1, 1, 2, 2, 1, -1, -2, -2, -1};

        int[][] visited = new int[H][W];
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                visited[i][j] = Integer.MAX_VALUE; // ์ด๊ฑฐ๋ณด๋‹ค ์Šคํ‚ฌ ์ ๊ฒŒ ์“ด ์›์ˆญ์ด? ์—ฌ๊ธฐ์„œ๋„ ํ•ด๋ด๋ฐ”
            }
        }

        Queue<Monkey> que = new LinkedList<>();

        que.add(new Monkey(0, 0, 0));
        visited[0][0] = 0;

        int cnt = 0;
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                Monkey m = que.poll();
//                System.out.println(m);
                if (m.r == H - 1 && m.c == W - 1) return cnt;

                for (int j = 0; j < 4; j++) {
                    int r = m.r + dr[j];
                    int c = m.c + dc[j];

                    if (0 <= r && r < H && 0 <= c && c < W && visited[r][c] > m.horse && map[r][c] == 0) {
                        que.add(new Monkey(r, c, m.horse));
                        visited[r][c] = m.horse;
                    }
                }

                if (m.horse < K) {
                    for (int j = 4; j < 12; j++) {
                        int r = m.r + dr[j];
                        int c = m.c + dc[j];

                        if (0 <= r && r < H && 0 <= c && c < W && visited[r][c] > m.horse+1 && map[r][c] == 0) {
                            que.add(new Monkey(r, c, m.horse + 1));
                            visited[r][c] = m.horse+1;
                        }
                    }
                }
            }
            cnt++;
        }
        return -1;
    }
}

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

  • BFS๋กœ ๊ฐ ์›€์ง์ž„ ๋ณ„ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•œ๋‹ค.
    • ๊ฐ€์žฅ ๋จผ์ € ๋„์ฐฉํ•œ ๊ฒฝ์šฐ๊ฐ€ ์›€์ง์ž„์˜ ์ตœ์†Œ๊ฐ’์ด๋‹ค.
    • ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฅผ int 2์ฐจ์› ๋ฐฐ์—ด visited๋ฅผ ์ด์šฉํ•œ๋‹ค.
      • ๋ง ์ด๋™์„ ์ตœ์†Œ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋ก ์ข‹์œผ๋ฏ€๋กœ, ๋” ์ ์€ ๊ฒฝ์šฐ ๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
    • ์›์ˆญ์ด์˜ ์ด๋™๊ณผ ๋ง ์ด๋™์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํ์— ๋‹ด๊ณ  ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฒ˜์Œ์—” ๋ง ์ด๋™์„ K๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ฐจ๊ฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ๋Š”๋ฐ, ์ฝ”๋“œ๊ฐ€ ๊ผฌ์—ฌ์„œ ์˜ค๋‹ต์ด ๋งŽ์•„์กŒ๋‹ค.
    • ๋‹ค์‹œํ‘ธ๋Š” ๊ณผ์ •์—์„œ 0๋ถ€ํ„ฐ K์ „๊นŒ์ง€ ๋ง ์ด๋™์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ”๊ฟจ๋‹ค.

 
728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_17141 : ์—ฐ๊ตฌ์†Œ 2  (0) 2023.04.06
BOJ_2636 : ์น˜์ฆˆ  (0) 2023.04.06
BOJ_10971 : ์™ธํŒ์› ์ˆœํšŒ 2  (0) 2023.04.06
BOJ_1010 : ๋‹ค๋ฆฌ ๋†“๊ธฐ  (0) 2023.04.06
BOJ_1463 : 1๋กœ ๋งŒ๋“ค๊ธฐ  (0) 2023.03.30