728x90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
class Dot{
int x;
int y;
public Dot(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
static int map[][]; // 2차원 미로 배열
static boolean visit[][]; // 2차원 방문여부 배열
static ArrayList<Integer> list;
static int n;
static int[] dx = {-1,0,1,0};
static int[] dy = {0,1,0,-1};
public static void dfs(int x, int y, int depth) { //BFS메소드
visit[x][y] = true;
for(int i = 0 ; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && ny >= 0 && nx < n && ny < n) {
if(map[nx][ny] > depth && !visit[nx][ny])
dfs(nx,ny,depth);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
map = new int [n][n];
visit = new boolean[n][n];
list = new ArrayList<>();
int max = 0, count = 0;
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < n; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
max = Math.max(max, map[i][j]);
}
}
for(int depth = 0; depth <= max; depth++) {
count = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(map[i][j] > depth && !visit[i][j]) {
dfs(i,j,depth);
count++;
}
list.add(count);
for(boolean a[] : visit)
Arrays.fill(a, false);//visit배열을 다시 false로 채워줌
}
int maxCount = Collections.max(list);//가장 큰 값 골라줌
System.out.println(maxCount);
}
}
728x90
'CS > Algorithm' 카테고리의 다른 글
[백준 : 11725] 트리의 부모 찾기 - JAVA (0) | 2021.05.23 |
---|---|
[백준 : 7562] 나이트의 이동 - JAVA (0) | 2021.05.23 |
[백준 : 1012] 유기농 배추 - JAVA (0) | 2021.05.23 |
[백준 : 7576] 토마토 - JAVA (0) | 2021.05.23 |
[백준 : 2156] 포도주 시식 (0) | 2021.05.16 |