https://www.acmicpc.net/problem/2589
첫째 줄에는 보물 지도의 세로의 크기와 가로의 크기가 빈칸을 사이에 두고 주어진다. 이어 L과 W로 표시된 보물 지도가 아래의 예와 같이 주어지며, 각 문자 사이에는 빈 칸이 없다. 보물 지도의 가로, 세로의 크기는 각각 50이하이다.
첫째 줄에 보물이 묻혀 있는 두 곳 사이를 최단 거리로 이동하는 시간을 출력한다.
5 7
WLLWWWL
LLLWLLL
LWLWLWW
LWLWLLL
WLLWLWW
8
package com.day1017;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ2589 {
static class Point{ // 1.
int x, y, count;
public Point(int x, int y, int count) {
this.x = x;
this.y = y;
this.count = count;
}
}
static int N, M;
static char[][] map;
static boolean[][] visited;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
static int cnt;
static int tmp;
static int result;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken()); //행
M = Integer.parseInt(st.nextToken()); //열
map = new char[N][M];
for(int i=0; i<N; i++) {
String s = br.readLine();
for(int j=0; j<M; j++) {
map[i][j] = s.charAt(j);
}
} //입력완료
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
if(map[i][j] == 'L') {
visited = new boolean[N][M]; // 3.
bfs(i, j); // 2.
}
}
}
System.out.println(result);
}
private static void bfs(int r, int c) {
Queue<Point> q = new ArrayDeque<>();
q.add(new Point(r, c, 0));
while(!q.isEmpty()) {
Point cur = q.poll();
int x = cur.x;
int y = cur.y;
result = Math.max(result, cur.count); // 4.
visited[x][y] = true;
for(int k=0; k<4; k++) {
int nx = x+dx[k];
int ny = y+dy[k];
if(nx >= 0 && ny >=0 && nx < N && ny < M && map[nx][ny] == 'L') {
if(!visited[nx][ny]) {
visited[nx][ny] = true; // 5.
q.add(new Point(nx, ny, cur.count+1)); // 5.
}
}
}
}
}
}