문제
https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는
www.acmicpc.net
풀이
import java.util.*;
import java.io.*;
public class Main {
static int n;
static int arr[][];
static int result = 0;
static int dx [] = {1,0,-1,0};
static int dy [] = {0,-1,0,1};
public static void main (String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
n = Integer.parseInt(br.readLine());
arr = new int[n][n];
int max = 0;
for(int i=0;i<n;i++) {
st = new StringTokenizer(br.readLine());
for(int j=0;j<n;j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
max = Math.max(arr[i][j], max);
}
}
for(int i=0;i<max;i++) {
bfs(i);
}
System.out.print(result);
}
static void bfs(int depth) {
Queue<int []> que = new LinkedList<>();
boolean visit[][] = new boolean [n][n];
int count = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(arr[i][j] <= depth || visit[i][j]) continue;
count++;
que.add(new int[] {i, j});
visit[i][j] = true;
while(!que.isEmpty()) {
int [] cur = que.poll();
for(int k=0;k<4;k++) {
int nx = cur[0] + dx[k];
int ny = cur[1] + dy[k];
if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if(arr[nx][ny] <= depth || visit[nx][ny]) continue;
que.add(new int[] {nx, ny});
visit[nx][ny] = true;
}
}
}
}
result = Math.max(result, count);
}
}
문제
https://www.acmicpc.net/problem/2468
2468번: 안전 영역
재난방재청에서는 많은 비가 내리는 장마철에 대비해서 다음과 같은 일을 계획하고 있다. 먼저 어떤 지역의 높이 정보를 파악한다. 그 다음에 그 지역에 많은 비가 내렸을 때 물에 잠기지 않는
www.acmicpc.net
풀이
import java.util.*; import java.io.*; public class Main { static int n; static int arr[][]; static int result = 0; static int dx [] = {1,0,-1,0}; static int dy [] = {0,-1,0,1}; public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; n = Integer.parseInt(br.readLine()); arr = new int[n][n]; int max = 0; for(int i=0;i<n;i++) { st = new StringTokenizer(br.readLine()); for(int j=0;j<n;j++) { arr[i][j] = Integer.parseInt(st.nextToken()); max = Math.max(arr[i][j], max); } } for(int i=0;i<max;i++) { bfs(i); } System.out.print(result); } static void bfs(int depth) { Queue<int []> que = new LinkedList<>(); boolean visit[][] = new boolean [n][n]; int count = 0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(arr[i][j] <= depth || visit[i][j]) continue; count++; que.add(new int[] {i, j}); visit[i][j] = true; while(!que.isEmpty()) { int [] cur = que.poll(); for(int k=0;k<4;k++) { int nx = cur[0] + dx[k]; int ny = cur[1] + dy[k]; if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue; if(arr[nx][ny] <= depth || visit[nx][ny]) continue; que.add(new int[] {nx, ny}); visit[nx][ny] = true; } } } } result = Math.max(result, count); } }