๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 18405_๊ฒฝ์Ÿ์  ์ „์—ผ

๋ฟŒ์•ผ._. 2022. 12. 12. 22:15

Gold V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/18405)

<๊ฒฝ์Ÿ์  ์ „์—ผ>

 

๋ฌธ์ œ ํ’€์ด

 

๋ฒˆํ˜ธ๊ฐ€ ๋‚ฎ์€ ์ข…๋ฅ˜์˜ ๋ฐ”์ด๋Ÿฌ์Šค๋ถ€ํ„ฐ ์ฆ์‹์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

 

 - my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class _18405_ { // ๊ฒฝ์Ÿ์  ์ „์—ผ
	static int[] dx= {-1,1,0,0}; // ์ƒํ•˜์ขŒ์šฐ
	static int[] dy= {0,0,-1,1};
	static PriorityQueue<int[]>queue;
	static int n, map[][];

	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer(bf.readLine());
		
		n=Integer.parseInt(st.nextToken());
		int k=Integer.parseInt(st.nextToken());
		
		queue=new PriorityQueue<>(new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[0]-o2[0];
			}
		});
		
		map=new int[n][n];
		boolean[][]depth=new boolean[n][n];
		for(int i=0; i<n; i++) {
			st=new StringTokenizer(bf.readLine());
			for(int j=0; j<n; j++) {
				map[i][j]=Integer.parseInt(st.nextToken());
				if(map[i][j]!=0) { // ๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆํ˜ธ์™€ ์œ„์น˜ ์ €์žฅ
					queue.add(new int[] {map[i][j],i,j});
					depth[i][j]=true;
				}
			}
		}
		
		st=new StringTokenizer(bf.readLine());
		int s=Integer.parseInt(st.nextToken()); // ์ดˆ
		int x=Integer.parseInt(st.nextToken());
		int y=Integer.parseInt(st.nextToken());
		
		if(s!=0) {
			search(); 
			for(int i=0; i<s-1; i++) {
				for(int j=0; j<n; j++) {
					for(int l=0; l<n; l++) {
						if(depth[j][l]==false && map[j][l]>0) { // ์•„์ง ์ฆ์‹ํ•˜์ง€ ์•Š์€ ๋ฐ”์ด๋Ÿฌ์Šค
							depth[j][l]=true;
							queue.add(new int[] {map[j][l], j, l});
						}
					}
				}
				search(); 
				if(map[x-1][y-1]>0) break;
			}
		}
		System.out.println(map[x-1][y-1]);
	}

	private static void search() {
		while(!queue.isEmpty()) {
			int[] temp=queue.poll();
			for(int i=0; i<4; i++) {
				int a=temp[1]+dx[i];
				int b=temp[2]+dy[i];
				if(a>=0 && a<n && b>=0 && b<n && map[a][b]==0) {
					map[a][b]=temp[0];
				}
			}
		}
	}
}

 

  •  Main
    • n, k์ž…๋ ฅ
    • map : ์‹œํ—˜๊ด€์˜ ์ •๋ณด
    • depth : ๋งค ์ดˆ๋งˆ๋‹ค ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํ‘œ์‹œ
    • map์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์šฐ์„ ์ˆœ์œ„ ํ์— [๋ฐ”์ด๋Ÿฌ์Šค ๋ฒˆํ˜ธ, x์ขŒํ‘œ,  y์ขŒํ‘œ]๋กœ ๋„ฃ๊ธฐ
    • 0์ดˆ์ธ ๊ฒฝ์šฐ์—๋Š” ๋ฐ”๋กœ answer ์ถœ๋ ฅ!
    • 0์ดˆ๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ search() ํ˜ธ์ถœ + s-1์ดˆ๋งŒํผ ์•„์ง ์ฆ์‹ํ•˜์ง€ ์•Š์€ ๋ฐ”์ด๋Ÿฌ์Šค ์ฐพ์•„์„œ ๋ฐ˜๋ณต! ์ด๋ฏธ (x-1, y-1) ์ž๋ฆฌ์— ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ค‘๊ฐ„์— break! (=์ด๋ฏธ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉด ๋‹ค๋ฅธ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ)
  • search ( )
    • queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต 
      -> queue์—์„œ poll ํ•œ ๊ฐ’ ์œ„์น˜์—์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํŒ๋ณ„ํ•˜์—ฌ ์•„์ง ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์—†๋Š” ๊ณณ์ด๋ผ๋ฉด map์— ๋ฐ”์ด๋Ÿฌ์Šค ์ฆ์‹ ํ‘œ์‹œ

์ƒ๊ฐ๐Ÿค”

 

๋ถ„๋ช… ๋‹ต์ด ๋งž๋Š”๋ฐ ์™œ ํ‹€๋ ธ์„๊นŒ... ํ•œ์ฐธ ๋ด๋„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๋‹ค.

์•„๋ฌด๋ฆฌ ๋ด๋„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๋‹ค..!!

 

๋ฐ˜๋ก€ ์งˆ๋ฌธ์„ ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ S๊ฐ€ 0์ดˆ์ผ ๋•Œ๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋ชปํ•œ ๊ฒƒ์ด์—ˆ๋‹ค.

S!=0 ์กฐ๊ฑด์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‚˜์„œ์•ผ ๋ฐ”๋กœ ํ•ด๊ฒฐ ~_~