๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ 2589๋ฒ ๋ณด๋ฌผ์ฌ ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ BFS ๋ก ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ์๋ค.
๊ณจ๋ 5๋ผ๊ณ ํ๋๋ฐ ๋ง์ ํ๋ฉด ์ค๋ฒ 1? ์ ๋ ๋๋์ ๋ฌธ์ ์๋ค.
ํด๊ฒฐ๋ฒ
๊ฐ๊ฐ ๋ฐฐ์ด์ ๋๋ฉฐ L์ผ ๋ BFS ๋ก ๊ฐ ์ ์๋ ์ต๋ ๊ธธ์ด๋ฅผ ๋งค๋ฒ ๊ฐฑ์ ํด์ฃผ๋ฉด ๋๋ค.
์ฃผ์ํด์ผํ ์ ์ด ๋ฐฐ์ด์ ํ ๋ฒ์ฉ ๋ค ๋๋ฉด ์๊ฐ ์ด๊ณผ๊ฐ ๋์ค๊ธฐ ๋๋ฌธ์ L ์ธ ๊ฒ๋ง ๋์์ผ ํ๋ค.
์ ๋ต ์ฝ๋
public class Main {
private static int l;
private static int w;
private static String[][] arr;
private static int[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] lw = br.readLine().split(" ");
l = Integer.parseInt(lw[0]);
w = Integer.parseInt(lw[1]);
arr = new String[l][w];
for (int i = 0; i < l; i++) {
String[] s = br.readLine().split("");
for (int j = 0; j < w; j++) {
arr[i][j] = s[j];
}
}
int max = 0;
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
if(arr[i][j].equals("L")) {
visited = new int[l][w];
max = Math.max(max, getTreasure(j, i));
}
}
}
bw.write(String.valueOf(max - 1));
bw.flush();
bw.close();
}
private static int getTreasure(int x, int y) {
int[] xPos = {1, -1, 0, 0};
int[] yPos = {0, 0, 1, -1};
Queue<Pair> queue = new LinkedList<>();
queue.add(new Pair(x, y));
visited[y][x] = 1;
while(!queue.isEmpty()) {
Pair front = queue.remove();
for (int i = 0; i < 4; i++) {
int xx = front.x + xPos[i];
int yy = front.y + yPos[i];
if(!(0 <= xx && xx < w && 0 <= yy && yy < l)) continue;
if(visited[yy][xx] != 0) continue;
if(arr[yy][xx].equals("W")) continue;
visited[yy][xx] = visited[front.y][front.x] + 1;
queue.add(new Pair(xx, yy));
}
}
int max = 0;
for (int i = 0; i < visited.length; i++) {
for (int j = 0; j < visited[i].length; j++) {
max = Math.max(max, visited[i][j]);
}
}
return max;
}
private static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
}
๋๊ธ