-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
67 lines (56 loc) Β· 1.81 KB
/
Main.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
package BFS.P1743;
import java.io.*;
import java.util.*;
public class Main {
static int N, M, K, r, c, max = 0;
static int[][] map;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/DFS/P1743/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < K; i++) {
st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
map[r-1][c-1] = 1;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] == 1) max = Math.max(max, bfs(i, j));
}
}
System.out.println(max);
}
static int bfs(int i, int j) {
Queue<Pos> q= new LinkedList<>();
q.offer(new Pos(i, j));
map[i][j] = 0;
int cnt = 1;
while (!q.isEmpty()) {
Pos p = q.poll();
for (int k = 0; k < 4; k++) {
int ti = p.i + di[k];
int tj = p.j + dj[k];
if (0 <= ti && ti < N && 0 <= tj && tj < M && map[ti][tj] == 1) {
map[ti][tj] = 0;
q.offer(new Pos(ti, tj));
cnt ++;
}
}
}
return cnt;
}
static class Pos {
int i, j;
public Pos(int i, int j) {
this.i = i;
this.j = j;
}
}
}