Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Union Find
- ์์ด
- Floyd
- Django
- ์ด๋ถํ์
- ๋ถ๋ถ์งํฉ
- ๊ทธ๋ฆฌ๋
- LowerBound
- upperbound
- PriorityQueue
- BFS
- ํ์๊ฐ์
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐฑํธ๋ํน
- ํธ๋ผ์ด
- dfs
- Dijkstra
- ๋นํธ๋ง์คํน
- ๋ค์ต์คํธ๋ผ
- ๊ตฌํ
- ์ธ๊ทธ๋จผํธํธ๋ฆฌ
- ์กฐํฉ
- ์นด์นด์ค
- ํฌํฌ์ธํฐ
- ํ๋ก์ด๋์์
- ์๋ฎฌ๋ ์ด์
- dp
- ์ด์งํธ๋ฆฌ
- ์ฐ์ ์์ํ
- ์ฌ๊ท
Archives
- Today
- Total
J
[JAVA] BOJ ๋ฐฑ์ค 2842 ์ง๋ฐฐ์ ํ์๋ ๋ณธ๋ฌธ
๐ Problem Solving/๐ BOJ
[JAVA] BOJ ๋ฐฑ์ค 2842 ์ง๋ฐฐ์ ํ์๋
snowball๐ฐ 2023. 7. 24. 17:37ํ์ด
- BFS๋ก ๋ฌธ์ ํ์ด๋ฅผ ํ ๋ ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํ๋ ๋ฌธ์ ์ด๋ค.
- ํฌํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฅ ๋์ ๊ณ ๋์ ๊ฐ์ฅ ๋ฎ์ ๊ณ ๋๋ฅผ ์ง์ ํ์ฌ ๊ทธ ์ฌ์ด์์๋ง BFS๋ฅผ ํ๋๋ก ํ ๊ฒ์ด๋ค.
- isDelivery ํจ์๋ฅผ ํตํด์ ์ ํด์ง ๊ณ ๋ ๋ด์์ ๋ฐฉ๋ฌธํ ์ ์๋ ์ง์ ๊ฐ์๋ฅผ ๋ฐํํ๋ค.
- ๋ฐฉ๋ฌธํด์ผ ํ๋ ์ง์ ๊ฐ์์ ๋ฐฉ๋ฌธํ ์ ์๋ ์ง์ ๊ฐ์๊ฐ ๊ฐ๋ค๋ฉด ๊ณ ๋์ ์ฐจ์ด๋ฅผ ์ค์ด๊ธฐ ์ํด์ low๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ minIndex์ ๊ฐ์ ์ฆ๊ฐ์ํจ๋ค.
- ๋ง์ฝ ๋ฐฉ๋ฌธํ ์ ์๋ ์ง์ ๊ฐ์๊ฐ ๋ ์๋ค๋ฉด 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;
}
}
'๐ Problem Solving > ๐ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] BOJ ๋ฐฑ์ค 1202 ๋ณด์๋๋ (0) | 2023.08.09 |
---|---|
[JAVA] BOJ ๋ฐฑ์ค 12837 ๊ฐ๊ณ๋ถ (0) | 2023.08.07 |
[JAVA] BOJ ๋ฐฑ์ค 16472 ๊ณ ๋ฅ์ด (0) | 2023.07.24 |
[JAVA] BOJ ๋ฐฑ์ค 2470 ๋ ์ฉ์ก (0) | 2023.07.07 |
[JAVA] BOJ ๋ฐฑ์ค 14725 ๊ฐ๋ฏธ๊ตด (2) | 2023.05.10 |
Comments