-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1743.java
More file actions
65 lines (58 loc) · 2.09 KB
/
1743.java
File metadata and controls
65 lines (58 loc) · 2.09 KB
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
/**
* 백준 1743번 음식물 피하기
* bfs/dfs 알고리즘
* 제발 Point 사용할 때는 Point(x,y)로 집어넣고 사용합시다...
*/
import java.util.*;
import java.awt.*;
import java.io.*;
public class Main {
static int n;
static int m;
static int k;
static int map[][] = new int[101][101];
static int visited[][] = new int[101][101];
static int dx[] = {0,1,-1,0}; // 하, 우, 좌, 상
static int dy[] = {1,0,0,-1};
static int bfs(int y, int x){
Deque<Point> queue = new ArrayDeque<>();
queue.add(new Point(x,y));
visited[y][x] = 1;
int cnt = 1;
while(!queue.isEmpty()){
Point point = queue.poll();
for(int i=0; i<4; i++){
int nextY = point.y + dy[i];
int nextX = point.x + dx[i];
// 벽을 안 넘고, 방문하지 않았고, 음식물 쓰레기가 있으면
if(1<=nextY&&nextY<=n && 1<=nextX&&nextX<=m && visited[nextY][nextX]==0 && map[nextY][nextX] == 1){
queue.add(new Point(nextX,nextY));
visited[nextY][nextX] = 1;
cnt++;
}
}
}
return cnt;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
n = Integer.parseInt(st.nextToken()); // y
m = Integer.parseInt(st.nextToken()); // x
k = Integer.parseInt(st.nextToken()); // 음식물 개수
for(int i=0; i<k; i++){
st = new StringTokenizer(br.readLine()," ");
map[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1;
}
int max = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(map[i][j]==1&&visited[i][j]==0){
int cnt = bfs(i,j);
if(cnt > max) max = cnt;
}
}
}
System.out.println(max);
}
}