• 문제 링크
6031번: Feeding Time
It's Bessie's feeding time, and Farmer John is trying to decide where to put her. FJ has a farm that comprises W x H (1 <= W <= 750; 1 <= H <= 750) squares and is partitioned into one or more separate pastures by rocks both large and small. Every pasture c
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.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int w, h;
static char[][] arr;
static int[] dy = {-1, 1, 0, 0, -1, -1, 1, 1}, dx = {0, 0, -1, 1, -1, 1, 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());
w = Integer.parseInt(st.nextToken());
h = Integer.parseInt(st.nextToken());
arr = new char[h][w];
for (int i = 0; i < h; i++)
arr[i] = br.readLine().toCharArray();
int max = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++)
if (arr[i][j] == '.') max = Math.max(max, bfs(i, j));
}
bw.write(String.valueOf(max));
bw.flush();
}
private static int bfs(int y, int x) {
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(y, x));
arr[y][x] = ' ';
int cnt = 1;
while (!q.isEmpty()) {
Pair pair = q.poll();
for (int i = 0; i < 8; i++) {
int ny = pair.y + dy[i];
int nx = pair.x + dx[i];
if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
if (arr[ny][nx] == '.' && arr[ny][nx] != ' ') {
arr[ny][nx] = ' ';
q.offer(new Pair(ny, nx));
cnt++;
}
}
}
return cnt;
}
private static class Pair {
int y, x;
public Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 17451 평행 우주 - Greedy / Java (0) | 2023.07.10 |
---|---|
[백준] 20363 당근 키우기 - Greedy / Java (0) | 2023.07.09 |
[백준] 5958 Space Exploration - Graph Theory / Java (0) | 2023.07.07 |
[백준] 10678 Meeting Time - Graph Theory / Java (0) | 2023.07.06 |
[백준] 5848 Message Relay - Graph Theory / Java (0) | 2023.07.05 |
댓글