[BOJ] ์๊ธฐ ์์ด(16236)
[๋ฐฑ์ค(BOJ)] 16236_์๊ธฐ ์์ด JAVA
๋ฌธ์ : BOJ_16236๋ฒ ์๊ธฐ ์์ด
๋ฌธ์ ์ค๋ช
์๊ธฐ ์์ด๊ฐ ์ํ์ข์ฐ๋ก ์์ง์ด๋ฉด์ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์ต๋๋ค.
์ด๋ ๋ฌผ๊ณ ๊ธฐ๋ ์๊ธฐ ์์ด์ ํฌ๊ธฐ๋ณด๋ค ์์ ๋ฌผ๊ณ ๊ธฐ๋ง ๋จน์ ์ ์๊ณ , ์์ด์ ํฌ๊ธฐ๋ณด๋ค ํฐ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์๋ ๊ณณ์ ์ง๋๊ฐ ์ ์์ต๋๋ค.
๊ฐ์ฅ ๊ฐ๊น์ด ๊ฑฐ๋ฆฌ์ ์๋ ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๋ถํฐ ๋จน๋๋ฐ, ๊ฐ์ ๊ฑฐ๋ฆฌ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๊ฐ ์ฌ๋ฌ ๋ฌผ๊ณ ๊ธฐ๋ผ๋ฉด ์ ๋ค์ ์ผ์ชฝ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๋ถํฐ ๋จน์ต๋๋ค.
๋ ํ ์๊ธฐ์ ๋ชธ ํฌ๊ธฐ ์๋งํผ์ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์ผ๋ฉด ๋ชธ์ ํฌ๊ธฐ๊ฐ 1 ์ฆ๊ฐํฉ๋๋ค.
์ด๋ฌํ ์กฐ๊ฑด์์ ๋ฌผ๊ณ ๊ธฐ๊ฐ ํ ๋ฒ ์ด๋ํ๋ ์๊ฐ์ 1์ด๋ผ๊ณ ํ ๋ ๋ฌผ๊ณ ๊ธฐ๊ฐ ๋จน์ ์ ์๋ ๋ชจ๋ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ถ๋ ฅํด์ผ ํฉ๋๋ค.
Solution
bfs, ์์ ํ์
์ฐ์ bfs๋ก ์์ด์ ์์น๋ถํฐ ์์ํด ๋๋ฌํ ์ ์๋ ๋ชจ๋ ๊ณณ์ ๋๋ฌํ์ฌ ๋จน์ ์ ์๋ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ํ์ธํฉ๋๋ค.
ํ์ธํ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์ฐ์ ์์ ํ์ ๋ฃ์ด๊ฐ๋ฉฐ ์ ๋ ฌํ๋๋ฐ, ์ด๋ ์ ๋ ฌ์ ๊ธฐ์ค์ ๋ฌผ๊ณ ๊ธฐ์ ํฌ๊ธฐ, ๋ฌผ๊ณ ๊ธฐ์ ์ธ๋ก ์ขํ, ๋ฌผ๊ณ ๊ธฐ์ ๊ฐ๋ก ์ขํ
์์๋ก ์ ๋ ฌํฉ๋๋ค.
์ฐ์ ์์ ํ์ ๋ค์ด์๋ ์ฒซ ๋ฒ์งธ ๋ฌผ๊ณ ๊ธฐ์ ํฌ๊ธฐ๊ฐ ์์ด์ ๋ชธ ํฌ๊ธฐ ๋ณด๋ค ์๋ค๋ฉด ๊ทธ๊ฒ์ ๋จน์ ๊ฑธ๋ก ์ฒ๋ฆฌํ๊ณ ๋ค์ bfs๋ฅผ ์งํํด ์ค๋๋ค.
N์ ํฌ๊ธฐ๊ฐ 20์ด๋ฏ๋ก ์ด์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ญ์ทจํ ์ ์๋ ๋ชจ๋ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์ญ์ทจํ๊ฒ ํ๋ฉด ์ ๋ต์ ์ถ๋ ฅํ ์ ์์ต๋๋ค.
Description
- ์ธ ๊ฐ์ง ์์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์ ๋ ฌํด์ผ ํ๊ธฐ ๋๋ฌธ์, Comparable์ ์ด์ฉํ์ฌ Fish๋ผ๋ ํด๋์ค๋ฅผ ๋ง๋ค์์ต๋๋ค.
- ๋ฌผ๊ณ ๊ธฐ๋ฅผ ์ญ์ทจํ๋ฉด ๊ทธ ์๋ฆฌ๊ฐ 0์ด ๋๋ ๊ฒ, ์์ด์ ๋ชธ ํฌ๊ธฐ ์๋งํผ ์ญ์ทจํ๋ฉด ์์ด์ ๋ชธ์ด ์ฆ๊ฐํ๋ ๊ฒ, ์ฒ์ ์์ด ์์น ์ขํ์ ํ ๋น๋ ๊ฐ์ 9์ธ ๊ฒ ๋ฑ ๋์น ์ ์๋ ์กฐ๊ฑด๋ค์ ์ ๋ฐ์ ธ์ ์ค์๋ฅผ ์์ ๊ฐ๋ฉฐ ํ์ด์ผ ํฉ๋๋ค.
import java.io.*;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
class Fish implements Comparable<Fish>{
private int x;
private int y;
private int value;
public Fish(int x, int y, int value){
this.x = x;
this.y = y;
this.value = value;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getValue() {
return value;
}
@Override
public int compareTo(Fish other) {
if(this.value==other.value && this.x == other.x) return Integer.compare(this.y, other.y);
else if(this.value == other.value) return Integer.compare(this.x, other.x);
else return Integer.compare(this.value, other.value);
}
}
class Pair{
private int x;
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public Pair(int x,int y){
this.x = x;
this.y = y;
}
}
public class Main {
public static int n, cnt, sharkSize=2, nowEat;
public static int[][] map;
public static int nowX, nowY;
public static int[] dx = {-1,1,0,0};
public static int[] dy = {0,0,-1,1};
public static boolean bfs(){
PriorityQueue<Fish> pq = new PriorityQueue<>();
int[][] vis = new int[n+1][n+1];
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
vis[i][j]=-1;
}
}
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(nowX,nowY));
vis[nowX][nowY] = 0;
while(!q.isEmpty()){
Pair pair = q.poll();
int x = pair.getX();
int y = pair.getY();
for(int i=0;i<4;i++){
int nx = x+dx[i];
int ny = y+dy[i];
if(nx<1||ny<1||nx>n||ny>n) continue;
if(vis[nx][ny]!=-1) continue;
if(map[nx][ny]> sharkSize) continue;
vis[nx][ny] = vis[x][y]+1;
q.offer(new Pair(nx,ny));
if(map[nx][ny]>0 && map[nx][ny]<sharkSize) pq.offer(new Fish(nx,ny,vis[nx][ny]));
}
}
if(pq.isEmpty()) return false;
else {
Fish fish = pq.poll();
int nx = fish.getX();
int ny = fish.getY();
nowEat++;
cnt+=vis[nx][ny];
map[nx][ny] = 0;
nowX = nx;
nowY = ny;
if(nowEat==sharkSize) {
sharkSize++;
nowEat = 0;
}
return true;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
map = new int[n+1][n+1];
for(int i=1;i<=n;i++){
st = new StringTokenizer(br.readLine());
for(int j=1; j<=n; j++){
map[i][j] = Integer.parseInt(st.nextToken());
if(map[i][j]==9){
nowX = i;
nowY = j;
map[i][j]=0;
}
}
}
while (true){
if(!bfs()) break;
}
bw.write(String.valueOf(cnt));
bw.flush();
br.close();
bw.close();
}
}