-
Notifications
You must be signed in to change notification settings - Fork 0
/
안전 영역.java
85 lines (67 loc) · 2.08 KB
/
안전 영역.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.io.*;
import java.util.*;
public class Main {
static int dx[] = {-1,1,0,0};
static int dy[] = {0,0,-1,1};
static boolean[][] visited;
static int[][] city;
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N= Integer.parseInt(br.readLine());
city = new int[N][N];
visited = new boolean[N][N];
StringTokenizer st;
for(int i =0; i<N; i++)
{
st = new StringTokenizer(br.readLine());
for(int j =0; j<N; j++)
{
city[i][j] = Integer.parseInt(st.nextToken());
}
}
int max =1;
for(int i =1; i<=100; i++)
{
visited = new boolean[N][N];
int answer = 0;
for(int j = 0; j<N; j++)
{
for(int k = 0; k<N; k++)
{
if(visited[j][k]== false && city[j][k] > i )
{
bfs(j,k,i);
answer++;
}
}
if(max < answer)
max = answer;
}
}
System.out.println(max);
}
public static void bfs(int x, int y, int width)
{
visited[x][y]= true;
Queue<int []> queue = new LinkedList<>();
queue.add(new int [] {x,y});
while(!queue.isEmpty())
{
int []cur = queue.poll();
int cx = cur[0];
int cy = cur[1];
for(int i =0; i<4; i++)
{
int curx = cx + dx[i];
int cury = cy + dy[i];
if(curx>=0 && curx <N && cury>=0 && cury<N &&
visited[curx][cury] == false && city[curx][cury]>width)
{
queue.add (new int[] { curx, cury});
visited[curx][cury]= true;
}
}
}
}
}