• 문제 링크
9790번: Elephant Show
There are a lot of elephants in Chiang Mai and many elephant camps can be seen around this city. Elephant camps are among the most popular attractions in Chiang Mai, but Khun-LungKen Elephant Farm is very different from the others. At Khun-Lung-Ken Elephan
www.acmicpc.net
• 풀이 코드
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 w, h;
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));
StringBuilder sb = new StringBuilder();
String s;
while (!(s = br.readLine()).equals("0 0")) {
StringTokenizer st = new StringTokenizer(s);
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
mat = new boolean[h][w];
int y = 0, x = 0;
for (int i = 0; i < h; i++) {
s = br.readLine();
for (int j = 0; j < w; j++) {
char c = s.charAt(j);
if (c != '#') {
if (c == 'A') {
y = i;
x = j;
}
mat[i][j] = true;
}
}
}
sb.append(bfs(new Pair(y, x))).append("\n");
}
bw.write(sb.toString());
bw.flush();
}
private static int bfs(Pair root) {
Queue<Pair> q = new ArrayDeque<>();
q.offer(root);
mat[root.y][root.x] = false;
int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int cnt = 1;
while (!q.isEmpty()) {
Pair cur = q.poll();
for (int[] d : dir) {
int ny = cur.y + d[0], nx = cur.x + d[1];
if (ny < 0 || ny >= h || nx < 0 || nx >= w || !mat[ny][nx]) continue;
q.offer(new Pair(ny, nx));
mat[ny][nx] = false;
cnt++;
}
}
return cnt;
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 3578 Holes - Greedy / Java (0) | 2024.04.10 |
---|---|
[백준] 5992 The Leisurely Stroll - Graph Theory / Java (0) | 2024.04.09 |
[백준] 5975 Pathfinding - Graph Theory / Java (0) | 2024.04.07 |
[백준] 31575 도시와 비트코인 - Graph Theory / Java (0) | 2024.04.06 |
[백준] 13450 László Babai - Graph Theory / Java (0) | 2024.04.05 |
댓글