J

[JAVA] BOJ ๋ฐฑ์ค€ 2842 ์ง‘๋ฐฐ์› ํ•œ์ƒ๋• ๋ณธ๋ฌธ

๐Ÿ”‘ Problem Solving/๐Ÿ‡ BOJ

[JAVA] BOJ ๋ฐฑ์ค€ 2842 ์ง‘๋ฐฐ์› ํ•œ์ƒ๋•

snowball๐Ÿฐ 2023. 7. 24. 17:37

ํ’€์ด

  1. BFS๋กœ ๋ฌธ์ œ ํ’€์ด๋ฅผ ํ• ๋•Œ ํˆฌํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
  2. ํˆฌํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๋†’์€ ๊ณ ๋„์™€ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ณ ๋„๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ทธ ์‚ฌ์ด์—์„œ๋งŒ BFS๋ฅผ ํ•˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.
  3. isDelivery ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์ •ํ•ด์ง„ ๊ณ ๋„ ๋‚ด์—์„œ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ง‘์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  4. ๋ฐฉ๋ฌธํ•ด์•ผ ํ•˜๋Š” ์ง‘์˜ ๊ฐœ์ˆ˜์™€ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ง‘์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ณ ๋„์˜ ์ฐจ์ด๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ low๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” minIndex์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
  5. ๋งŒ์•ฝ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ง‘์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด high๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” maxIndex์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

import java.io.*;
import java.util.*;

public class Main {
	static int N;
	static char[][] map;
	static int[][] height;

	static int sx, sy, visit, result = Integer.MAX_VALUE;

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

	public static void main(String[] args) throws Exception{
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(bf.readLine());
		int min = Integer.MIN_VALUE; int max = 0;

		map = new char[N][N]; height = new int[N][N];
		for(int i=0; i<N; i++){
			String s = bf.readLine();
			for(int j=0; j<N; j++){
				map[i][j] = s.charAt(j);
				if(map[i][j] == 'P'){sx = i; sy = j;}
				else if(map[i][j] == 'K') visit++;
			}
		}

		List<Integer> mapHeight = new ArrayList<>();
		for(int i=0; i<N; i++){
			StringTokenizer st = new StringTokenizer(bf.readLine());
			for(int j=0; j<N; j++){
				height[i][j] = Integer.parseInt(st.nextToken());
				if(map[i][j] != '.'){
					min = Math.min(min, height[i][j]);
					max = Math.max(max, height[i][j]); // ๋ฐฉ๋ฌธํ•ด์•ผํ•˜๋Š” ๊ณณ๋“ค ์ค‘ ๊ฐ€์žฅ ๋‚ฎ์€, ๋†’์€ ๊ณ ๋„
				}
				mapHeight.add(height[i][j]);
			}
		}

		Collections.sort(mapHeight);

		int minIndex = 0; int maxIndex = 0;
		for(int i=0; i<N*N; i++){
			if(max == mapHeight.get(i)){
				maxIndex = i; // ๋ฐฉ๋ฌธ ํ•ด์•ผ ํ•  ์ง‘์ค‘ ๊ฐ€์žฅ ๋†’์€ ๊ณ ๋„๋ฅผ ๊ฐ€์ง„ ์ง‘ index
			}
		}

		while(minIndex <= maxIndex && maxIndex < N*N){
			if(isDelivery(mapHeight.get(minIndex), mapHeight.get(maxIndex)) == visit){ // ๋ชจ๋“  ๊ณณ์„ ๋ฐฉ๋ฌธํ• ์ˆ˜ ์žˆ๋‹ค๋ฉด ๊ณ ๋„๋ฅผ ์ค„์ธ๋‹ค.
				result = Math.min(result, mapHeight.get(maxIndex) - mapHeight.get(minIndex));
				minIndex++;
			}
			else
				maxIndex++;
		}
		System.out.println(result);

	}
	static int isDelivery(int low, int high){
		if(height[sx][sy] < low || height[sx][sy] > high) return -1;

		int r = 0;
		boolean[][] visited = new boolean[N][N];
		ArrayDeque<int[]> q = new ArrayDeque<>();
		q.offer(new int[]{sx, sy});
		visited[sx][sy] = true;

		while(!q.isEmpty()){
			int[] now = q.poll();
			for(int d=0; d < 8; d++){
				int nx = now[0] + dx[d];
				int ny = now[1] + dy[d];
				if(nx < 0 || ny < 0 || nx >= N || ny >= N || visited[nx][ny]) continue;
				if(height[nx][ny] < low || height[nx][ny] > high) continue;

				q.offer(new int[]{nx, ny});
				visited[nx][ny] = true;
				if(map[nx][ny] == 'K') r++;
			}

		}
		return r;
	}
}
Comments