-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyess.java
More file actions
106 lines (98 loc) · 2.3 KB
/
yess.java
File metadata and controls
106 lines (98 loc) · 2.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
```
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int n ;
static int m ;
static BufferedReader br ;
static int [][] map;
static BufferedWriter bw;
static int [] x = {1,-1,0,0};
static int [] y = {0,0,1,-1};
static int max = 0 ;
public static void main(String [] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter (new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m= Integer.parseInt(st.nextToken());
map = new int [n][m];
for(int i = 0 ; i < n ; i ++) {
st = new StringTokenizer(br.readLine());
for(int j = 0 ; j < m ; j ++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0);
bw.write(String.valueOf(max));
bw.flush();
bw.close();
br.close();
}
static void find() {
int [][] copy_map = new int [n][m];
for(int i = 0 ; i < n ; i ++) {
for(int j = 0 ; j < m ; j ++) {
copy_map[i][j] = map[i][j];
}
}
Queue<int [] > queue = new ArrayDeque<>();
for(int i = 0 ; i < n ; i ++) {
for(int j = 0 ; j < m ; j ++) {
if(copy_map[i][j] == 2) {
int temp [] = {i,j};
queue.add(temp);
}
}
}
while(!queue.isEmpty()) {
int [] now =queue.poll();
int tx = now[0];
int ty = now[1];
for(int i = 0 ; i < x.length; i ++) {
int nx = tx + x[i];
int ny = ty + y[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= m )continue;
if(copy_map[nx][ny] == 0 ) {
copy_map[nx][ny] = 2;
queue.add(new int[] {nx,ny});
}
}
}
count_virus(copy_map);
}
static void count_virus(int [][] cmap) {
int cnt = 0 ;
for(int i = 0 ; i < n ; i ++)
{
for(int j = 0 ; j < m ; j ++) {
if(cmap[i][j] == 0 ) {
cnt ++;
}
}
}
if(cnt > max) max = cnt;
}
static void dfs(int idx ) {
if(idx == 3) {
find();
return ;
}
for(int i= 0 ; i < n ; i ++) {
for(int j = 0 ; j < m ; j ++) {
if(map[i][j] == 0 ) {
map[i][j] = 1;
dfs(idx + 1);
map[i][j] = 0 ;
}
}
}
}
}
```