• 문제 링크
26999번: Satellite Photographs
Farmer John purchased satellite photos of W x H pixels of his farm (1 ≤ W ≤ 80, 1 ≤ H ≤ 1000) and wishes to determine the largest 'contiguous' (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by tra
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.StringTokenizer;
public class Main {
static int w, h, cnt;
static char[][] mat;
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 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());
mat = new char[h][w];
for (int i = 0; i < h; i++) mat[i] = br.readLine().toCharArray();
int max = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (mat[i][j] == '*') {
cnt = 0;
dfs(i, j);
max = Math.max(max, cnt);
}
}
}
bw.write(String.valueOf(max));
bw.flush();
}
private static void dfs(int y, int x) {
for (int[] d : dir) {
int ny = y + d[0], nx = x + d[1];
if (ny < 0 || ny >= h || nx < 0 || nx >= w || mat[ny][nx] == '.') continue;
mat[ny][nx] = '.';
cnt++;
dfs(ny, nx);
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 10774 저지 - Greedy / Java (0) | 2024.01.11 |
---|---|
[백준] 5991 Papaya Jungle - Graph Theory / Java (0) | 2024.01.10 |
[백준] 6018 Tea Time - Graph Theory / Java (0) | 2024.01.08 |
[백준] 6188 Clear Cold Water - Graph Theory / Java (0) | 2024.01.07 |
[백준] 9255 The Friend of My Enemy is My Enemy - Graph Theory / Java (0) | 2024.01.06 |
댓글