๋ฌธ์ ์ค๋ช
์๊ธฐ ์์ด
- ์ฒ์ ํฌ๊ธฐ : 2
- 1์ด์ ์ํ์ข์ฐ๋ก ์ธ์ ํ ํ ์นธ์ฉ ์ด๋ ๊ฐ๋ฅ
- ์๊ธฐ๋ณด๋ค ํฐ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ ๊ณณ์ ํตํ ๋ถ๊ฐ
- ์๊ธฐ๋ณด๋ค ์์ ๋ฌผ๊ณ ๊ธฐ๋ง ๋จน์ ์ ์๋ค.
- ์๊ธฐ ํฌ๊ธฐ ๋งํผ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์ผ๋ฉด ํฌ๊ธฐ += 1
์๊ธฐ ์์ด๊ฐ ์ด๋๋ก ์ด๋ํ ์ง ๊ฒฐ์ ํ๋ ๋ฐฉ๋ฒ
- ๋ ์ด์ ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ โ ๐ ์๋ง ์์ด์๊ฒ ๋์ ์์ฒญ
- ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ 1๋ง๋ฆฌ ๐ ๊ทธ ๋ฌผ๊ณ ๊ธฐ ๋จน์ผ๋ก ๊ฐ๊ธฐ
- ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๊ฐ 2๋ง๋ฆฌ ์ด์ ๐ ๊ฐ์ฅ ๊ฐ๊น์ด ๋ฌผ๊ณ ๊ธฐ ๋จน์ผ๋ฌ ๊ฐ๊ธฐ
- ๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์กด์ฌํ๋ฉด ๐ ๊ฐ์ฅ ์์ ์๋ ๋ฌผ๊ณ ๊ธฐ,
- ๊ทธ๊ฒ๋ ์ฌ๋ฌ ๋ง๋ฆฌ๋ผ๋ฉด ๐ ๊ฐ์ฅ ์ผ์ชฝ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๋๋ค.
๋ฌธ์ ํ์ด
1. ์ ๋ ฅ๋ฐ๊ธฐ
map[i][j]
: ๊ณต๊ฐ์ ์ํsharkX, sharkY
: ์๊ธฐ ์์ด์ ์์น
2. ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ ํ์
checkMinDist(sharkX, sharkY)
BFS์ด์ฉdist[x][y]
๐ (sharkX, sharkY)๋ถํฐ (x,y)๊น์ง ์ต์๊ฑฐ๋ฆฌ
map[x][y]์ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์กด์ฌํ๊ณ , ํฌ๊ธฐ๊ฐ ์๊ธฐ ์์ด๋ณด๋ค ์์ผ๋ฉด,
๐ (minX, minY), minDist
์ (x,y), dist[x][y]
์ ์ฅ
minDist๊ฐ ๊ฐ์ผ๋ฉด ๊ฐ์ฅ ์์ ์๋ ๋ฌผ๊ณ ๊ธฐ, ๊ทธ๊ฒ๋ ๊ฐ์ผ๋ฉด ๊ฐ์ฅ ์ผ์ชฝ ๋ฌผ๊ณ ๊ธฐ์ ์์น ์ ์ฅ
3. ๋ฌผ๊ณ ๊ธฐ ๋จน๊ธฐ
eatFish()
๋ง์ฝ (minX, minY)๊ฐ ์ด๊ธฐ๊ฐ์ด๋ผ๋ฉด ๋ ์ด์ ๋จน์ ๋ฌผ๊ณ ๊ธฐ ์กด์ฌโ ๐ ์ข
๋ฃ
์๊ธฐ ์์ด๊ฐ ๋จน์ ๋ฌผ๊ณ ๊ธฐ ์๋ฅผ ์ฆ๊ฐ
(sharkX, sharkY)๋ฅผ (minX, minY)๋ก ๋ณ๊ฒฝ
๊ฑธ๋ฆฐ ์๊ฐ time += minDist
์๊ธฐ ์์ด๊ฐ ๋ณธ์ธ ํฌ๊ธฐ๋งํผ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์๋ค๋ฉด, ํฌ๊ธฐ ์ฆ๊ฐ
์ฝ๋
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_BOJ_16236_์๊ธฐ_์์ด {
static int N, sharkX, sharkY, minX, minY, minDist, sharkSize = 2, eating = 0, time = 0;
static int[][] map, dist;
static int[] dx = {-1, 0, 0, 1};
static int[] dy = {0, -1, 1, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stringTokenizer;
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
stringTokenizer = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(stringTokenizer.nextToken());
if (map[i][j] == 9) {
sharkX = i;
sharkY = j;
map[i][j] = 0;
}
}
}
while(true){
dist = new int[N][N];
minX = Integer.MAX_VALUE;
minY = Integer.MAX_VALUE;
minDist = Integer.MAX_VALUE;
checkMinDist(sharkX, sharkY);
if(eatFish()) break;
}
System.out.println(time);
}
private static boolean eatFish() {
if (minX != Integer.MAX_VALUE && minY != Integer.MAX_VALUE) {
eating++;
map[minX][minY] = 0;
sharkX = minX;
sharkY = minY;
time += dist[minX][minY];
if (eating == sharkSize) {
sharkSize++;
eating = 0;
}
return false;
}
return true;
}
private static void checkMinDist(int sharkX, int sharkY) {
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{sharkX, sharkY});
while (!q.isEmpty()) {
int[] now = q.poll();
for (int i = 0; i < 4; i++) {
int nx = now[0] + dx[i];
int ny = now[1] + dy[i];
if(nx < 0 || ny < 0 || N <= nx || N <= ny || map[nx][ny] > sharkSize || dist[nx][ny] != 0) continue;
dist[nx][ny] = dist[now[0]][now[1]] + 1;
if(map[nx][ny] != 0 && map[nx][ny] < sharkSize){
if(minDist > dist[nx][ny]){
minDist = dist[nx][ny];
minX = nx;
minY = ny;
} else if (minDist == dist[nx][ny]) {
if (minX == nx) {
minY = Math.min(minY, ny);
} else if (minX > nx) {
minX = nx;
minY = ny;
}
}
}
q.offer(new int[]{nx, ny});
}
}
}
}
'์๊ณ ๋ฆฌ์ฆ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ๋ฐฑ์ค / BOJ 17140 ] ์ด์ฐจ์ ๋ฐฐ์ด๊ณผ ์ฐ์ฐ ( ์๋ฐ / JAVA ) (0) | 2022.02.02 |
---|---|
[ ๋ฐฑ์ค / BOJ 17135 ] ์บ์ฌ ๋ํ์ค ( ์๋ฐ / JAVA ) (0) | 2022.01.31 |
[ ๋ฐฑ์ค / BOJ 17144 ] ๋ฏธ์ธ๋จผ์ง ์๋ ! ( JAVA ) (0) | 2022.01.30 |
[ ๋ฐฑ์ค / BOJ 21610 ] ๋ง๋ฒ์ฌ ์์ด์ ๋น๋ฐ๋ฆฌ๊ธฐ ( JAVA ) (0) | 2022.01.30 |
[ ๋ฐฑ์ค / BOJ 16234 ] ์ธ๊ตฌ ์ด๋ ( JAVA ) (0) | 2022.01.30 |