• 문제 링크
https://www.acmicpc.net/problem/5993
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int r, c;
static boolean[][] mat;
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());
c = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken()) - 1, y = r - Integer.parseInt(st.nextToken());
Pair src = new Pair(y, x, 0);
mat = new boolean[r][c];
for (int i = 0; i < r; i++) {
String s = br.readLine();
for (int j = 0; j < c; j++) mat[i][j] = s.charAt(j) == '*';
}
bw.write(String.valueOf(bfs(src)));
bw.flush();
}
private static int bfs(Pair src) {
int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
Queue<Pair> q = new ArrayDeque<>();
q.offer(src);
mat[src.y][src.x] = true;
int cnt = 0;
while (!q.isEmpty()) {
Pair cur = q.poll();
cnt = cur.cnt;
for (int[] d : dir) {
int ny = cur.y + d[0], nx = cur.x + d[1];
if (ny < 0 || ny >= r || nx < 0 || nx >= c || mat[ny][nx]) continue;
q.offer(new Pair(ny, nx, cur.cnt + 1));
mat[ny][nx] = true;
}
}
return cnt;
}
private static class Pair {
int y, x, cnt;
Pair(int y, int x, int cnt) {
this.y = y;
this.x = x;
this.cnt = cnt;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 30610 A-maze-ing Lakes - Graph Theory / Java (0) | 2024.06.18 |
---|---|
[백준] 26942 Gruppindelning - Graph Theory / Java (0) | 2024.06.17 |
[백준] 10320 Button Bashing - Graph Theory / Java (0) | 2024.06.15 |
[백준] 2545 팬케익 먹기 - Greedy / Java (0) | 2024.06.14 |
[백준] 6123 O Those Fads - Greedy / Java (0) | 2024.06.13 |
댓글