Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 2589번 보물섬 (gold 5 본문
import java.io.*;
import java.util.*;
class Point{
int x;
int y;
int v;
public Point(int x, int y, int v){
this.x=x;
this.y=y;
this.v=v;
}
}
public class Main{
static int[] numx={-1,1,0,0};
static int[] numy={0,0,-1,1};
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());
int n=Integer.valueOf(st.nextToken());
int m=Integer.valueOf(st.nextToken());
char[][] map=new char[n+1][m+1];
for(int i=1; i<=n; i++){
String a=br.readLine();
for(int j=1; j<=m; j++){
map[i][j]=a.charAt(j-1);
}
}
Queue<Point> que=new LinkedList<>();
int min=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(map[i][j]=='W'){
continue;
}
que.clear();
boolean[][] visit=new boolean[n+1][m+1];
visit[i][j]=true;
que.offer(new Point(j,i,0));
while(!que.isEmpty()){
Point temp=que.poll();
min= min<temp.v ? temp.v : min;
for(int k=0; k<4; k++) {
int tx = temp.x + numx[k];
int ty = temp.y + numy[k];
if (tx < 1 || tx > m || ty < 1 || ty > n || map[ty][tx] == 'W' || visit[ty][tx]) {
continue;
}
visit[ty][tx] = true;
que.offer(new Point(tx, ty, temp.v + 1));
}
}
}
}
bw.write(min+"");
bw.flush();
}
}
// 항상 모든 케이스들의 최단경로를 구하면서 비교하는 문제.
// 어딘가를 경유하는 걸 고려할 필요x
// 너비탐색으로 다른 점에 도달할때마다가 최단의 거리임.
'백준공부 > java' 카테고리의 다른 글
[백준] 2636번 치즈 (gold 4 (2) | 2024.03.06 |
---|---|
[백준] 1759번 암호 만들기 (gold 5 (0) | 2024.03.05 |
[백준] 6593번 상범 빌딩 (gold 5 (0) | 2024.03.04 |
[백준] 17836번 공주님을 구해라! (gold 5 (0) | 2024.03.03 |
[백준] 1922번 네트워크 연결 (gold 4 (2) | 2024.02.28 |