๐Ÿฅ‡ [Baekjoon / Java] 18405. ๊ฒฝ์Ÿ์  ์ „์—ผ

์ดํ•˜์–€ยท2024๋…„ 6์›” 18์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
26/33

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

NxN ํฌ๊ธฐ์˜ ์‹œํ—˜๊ด€์ด ์žˆ๋‹ค. ์‹œํ—˜๊ด€์€ 1x1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง€๋ฉฐ, ํŠน์ •ํ•œ ์œ„์น˜์—๋Š” ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค๋Š” 1๋ฒˆ๋ถ€ํ„ฐ K๋ฒˆ๊นŒ์ง€์˜ ๋ฐ”์ด๋Ÿฌ์Šค ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜์— ์†ํ•œ๋‹ค.

์‹œํ—˜๊ด€์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค๋Š” 1์ดˆ๋งˆ๋‹ค ์ƒ, ํ•˜, ์ขŒ, ์šฐ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ์ฆ์‹ํ•ด ๋‚˜๊ฐ„๋‹ค. ๋‹จ, ๋งค ์ดˆ๋งˆ๋‹ค ๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ์ข…๋ฅ˜์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ถ€ํ„ฐ ๋จผ์ € ์ฆ์‹ํ•œ๋‹ค. ๋˜ํ•œ ์ฆ์‹ ๊ณผ์ •์—์„œ ํŠน์ •ํ•œ ์นธ์— ์ด๋ฏธ ์–ด๋– ํ•œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ๊ทธ ๊ณณ์—๋Š” ๋‹ค๋ฅธ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๋‹ค.

์‹œํ—˜๊ด€์˜ ํฌ๊ธฐ์™€ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, S์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— (X,Y)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋งŒ์•ฝ S์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, 0์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ด ๋•Œ X์™€ Y๋Š” ๊ฐ๊ฐ ํ–‰๊ณผ ์—ด์˜ ์œ„์น˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ์‹œํ—˜๊ด€์˜ ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์€ (1,1)์— ํ•ด๋‹นํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด 3x3 ํฌ๊ธฐ์˜ ์‹œํ—˜๊ด€์ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. ์„œ๋กœ ๋‹ค๋ฅธ 1๋ฒˆ, 2๋ฒˆ, 3๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๊ฐ๊ฐ (1,1), (1,3), (3,1)์— ์œ„์น˜ํ•ด ์žˆ๋‹ค. ์ด ๋•Œ 2์ดˆ๊ฐ€ ์ง€๋‚œ ๋’ค์— (3,2)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด์ž.

1์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— ์‹œํ—˜๊ด€์˜ ์ƒํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

2์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— ์‹œํ—˜๊ด€์˜ ์ƒํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๊ฒฐ๊ณผ์ ์œผ๋กœ 2์ดˆ๊ฐ€ ์ง€๋‚œ ๋’ค์— (3,2)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋Š” 3๋ฒˆ ๋ฐ”์ด๋Ÿฌ์Šค๋‹ค. ๋”ฐ๋ผ์„œ 3์„ ์ถœ๋ ฅํ•˜๋ฉด ์ •๋‹ต์ด๋‹ค.

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N, K๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 200, 1 โ‰ค K โ‰ค 1,000)
  • ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์‹œํ—˜๊ด€์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ๊ฐ ํ–‰์€ N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ•ด๋‹น ์œ„์น˜์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‹จ, ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ 0์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋˜ํ•œ ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค์˜ ๋ฒˆํ˜ธ๋Š” K์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ๋งŒ ์ฃผ์–ด์ง„๋‹ค.
  • N+2๋ฒˆ์งธ ์ค„์—๋Š” S, X, Y๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. (0 โ‰ค S โ‰ค 10,000, 1 โ‰ค X, Y โ‰ค N)
//์˜ˆ1
3 3
1 0 2
0 0 0
3 0 0
2 3 2
//์˜ˆ2
3 3
1 0 2
0 0 0
3 0 0
1 2 2

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • S์ดˆ ๋’ค์— (X,Y)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋งŒ์•ฝ S์ดˆ ๋’ค์— ํ•ด๋‹น ์œ„์น˜์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
//์˜ˆ1
3
//์˜ˆ2
0


๋ฌธ์ œ ์ดํ•ด


  • ์‹œํ—˜๊ด€ ํฌ๊ธฐ : NxN
  • ๋ฐ”์ด๋Ÿฌ์Šค ์ข…๋ฅ˜ : 1 ~ K๋ฒˆ๊นŒ์ง€ K๊ฐ€์ง€
  • ์‹œํ—˜๊ด€์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ฐ”์ด๋Ÿฌ์Šค๋Š” 1์ดˆ๋งˆ๋‹ค -> ์ƒ, ํ•˜, ์ขŒ, ์šฐ ๋ฐฉํ–ฅ์œผ๋กœ ์ฆ์‹
  • ์‹œํ—˜๊ด€์˜ ํฌ๊ธฐ์™€ ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, s์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„์— (X,Y)์— ์กด์žฌํ•˜๋Š” ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์ข…๋ฅ˜๋ฅผ ์ถœ๋ ฅ


์•Œ๊ณ ๋ฆฌ์ฆ˜


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 25๋ถ„

  • ์ž…๋ ฅ

    • N, K ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋ฐฐ์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ

    • BFS ๊ตฌํ•˜๊ธฐ
  • ์ถœ๋ ฅ

    • result ์ถœ๋ ฅ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ

  • BFS

    • ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ -> ๋ฐ”์ด๋Ÿฌ์Šค ์ข…๋ฅ˜, ์‹œ๊ฐ„, ์œ„์น˜(์ขŒํ‘œ) ์‚ฝ์ž…
    • sort ํ›„ ํ๋กœ ์˜ฎ๊ธฐ๊ธฐ
import java.util.*;

public class Main {
    static int N;
    static int K;
    static int[][] arr;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static int result = 0;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        K = sc.nextInt();

        arr = new int[N][K];
        for(int i=0; i<N; i++){
            for(int j=0; j<K; j++){
                arr[i][j] = sc.nextInt();
            }
        }

        bfs(0,0);
        Arrays.sort(arr);
        System.out.println(result);

    }

    public static int bfs(int x, int y){
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x,y});

        while(!queue.isEmpty()){
            int[] current = queue.poll();
            x = current[0];
            y = current[1];
            for(int i=0; i<4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];

                if(nx < 0 || ny < 0 || nx >= N || ny >= K){
                    continue;
                }

                if(arr[nx][ny] == 0){
                    continue;
                }

                if(arr[nx][ny] == 0){
                    arr[nx][ny] = arr[x][y] + 1;
                    queue.offer(new int[]{nx, ny});
                }
            }
        }
        return arr[N-1][K-1];
    }
}


์˜ค๋‹ต์ฒดํฌ


  • ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์‹œ๊ฐ„ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•ด bfs๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ํ•จ.
  • ๋˜ํ•œ, ์ •๋ ฌ์„ ๋ณ„๋„ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ.(์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‚˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์€ ์‹œ๊ฐ„ ์ •๋ณด๋•Œ๋ฌธ์— ์ •ํ™•ํžˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ)
...
static class Virus implements Comparable<Virus> {
        int type;
        int x;
        int y;
        int time;

        public Virus(int type, int x, int y, int time) {
            this.type = type;
            this.x = x;
            this.y = y;
            this.time = time;
        }

        @Override
        public int compareTo(Virus o) {
            return Integer.compare(this.type, o.type);
        }
    }
    
...

	List<Virus> list = new ArrayList<>();
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                arr[i][j] = sc.nextInt();
                if(arr[i][j] != 0){
                    list.add(new Virus(arr[i][j], i , j, 0));
                }
            }
        }

        Collections.sort(list);

        tT = sc.nextInt();
        tX = sc.nextInt()-1;
        tY = sc.nextInt()-1;

        bfs(list, tT, tX, tY);
        
        
        ...
        
        public static void bfs(List<Virus> list, int tT, int tX, int tY) {
        Queue<Virus> queue = new LinkedList<>(list);

        while(!queue.isEmpty()){
            Virus current = queue.poll();
            int x = current.x;
            int y = current.y;
            int time = current.time;

            if(time == tT) {
                System.out.println(arr[tX][tY]);
                return;
            }

            for(int i = 0; i < 4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];

                if(nx < 0 || ny < 0 || nx >= N || ny >= N) {
                    continue;
                }

                if(arr[nx][ny] == 0){
                    arr[nx][ny] = current.type;
                    queue.offer(new Virus(current.type, nx, ny, time + 1));
                }
            }
        }
        System.out.println(arr[tX][tY]);
    }


์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 1์‹œ๊ฐ„ 15๋ถ„(์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ ํฌํ•จ)

  • ์ž…๋ ฅ

    • N, K ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋ฐฐ์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๋ณ„๋„ ํด๋ž˜์Šค
    - Virus ํด๋ž˜์Šค ์ •์˜ (๋ฐ”์ด๋Ÿฌ์Šค ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค)

  • ๊ณ„์‚ฐ

    • ๋ฐ”์ด๋Ÿฌ์Šค ์ž…๋ ฅ ์ •๋ณด ์ •๋ ฌํ•ด์„œ ํ์— ๋„ฃ๊ธฐ
    • BFS ์ง„ํ–‰
  • ์ถœ๋ ฅ

    • ๋ชฉํ‘œ ์‹œ๊ฐ„์— ๋„๋‹ฌ ์‹œ ๊ทธ ์œ„์น˜ ๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆํ˜ธ ์ถœ๋ ฅ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ

    • BFS
      • ํ -> ๋ฐ”์ด๋Ÿฌ์Šค ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ํ•ด๋‹น ์œ„์น˜์—์„œ ์ƒ, ํ•˜, ์ขŒ, ์šฐ๋กœ ์ „ํŒŒ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ ํƒ์ƒ‰, ์ „ํŒŒ๋œ ๋ฐ”์ด๋Ÿฌ์Šค ์ •๋ณด๋ฅผ ํ์— ์ถ”๊ฐ€
      • ํ empty๊นŒ์ง€ ๋ฐ˜๋ณต
import java.util.*;

public class Main {

    static int N;
    static int K;
    static int[][] arr;

    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};

    static int result;

    static int tT;
    static int  tX;
    static int tY;


    static class Virus implements Comparable<Virus> {
        int type;
        int x;
        int y;
        int time;

        public Virus(int type, int x, int y, int time) {
            this.type = type;
            this.x = x;
            this.y = y;
            this.time = time;
        }

        @Override
        public int compareTo(Virus o) {
            return Integer.compare(this.type, o.type);
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        K = sc.nextInt();

        arr = new int[N][N];
        List<Virus> list = new ArrayList<>();
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                arr[i][j] = sc.nextInt();
                if(arr[i][j] != 0){
                    list.add(new Virus(arr[i][j], i , j, 0));
                }
            }
        }

        Collections.sort(list);

        tT = sc.nextInt();
        tX = sc.nextInt()-1;
        tY = sc.nextInt()-1;

        bfs(list, tT, tX, tY);
    }

    public static void bfs(List<Virus> list, int tT, int tX, int tY) {
        Queue<Virus> queue = new LinkedList<>(list);

        while(!queue.isEmpty()){
            Virus current = queue.poll();
            int x = current.x;
            int y = current.y;
            int time = current.time;

            if(time == tT) {
                System.out.println(arr[tX][tY]);
                return;
            }

            for(int i = 0; i < 4; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];

                if(nx < 0 || ny < 0 || nx >= N || ny >= N) {
                    continue;
                }

                if(arr[nx][ny] == 0){
                    arr[nx][ny] = current.type;
                    queue.offer(new Virus(current.type, nx, ny, time + 1));
                }
            }
        }
        System.out.println(arr[tX][tY]);
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€