-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA16.js
More file actions
68 lines (62 loc) · 1.61 KB
/
A16.js
File metadata and controls
68 lines (62 loc) · 1.61 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
const fs = require('fs');
const path = require('path')
const input = fs.readFileSync(path.join(__dirname,'/A16.txt')).toString().trim().split("\n");
const [n, m] = input[0].split(" ").map((v) => +v);
let data = [];
for (let i = 1; i <= n; i++) {
data.push(input[i].split(" ").map((v) => +v));
}
let temp = Array.from(Array(n), () => Array(m).fill(0));
let dx = [-1, 0, 1, 0];
let dy = [0, 1, 0, -1];
function virus(x, y) {
for (let i = 0; i < 4; i++) {
let nx = x + dx[i];
let ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
if (temp[nx][ny] === 0) {
temp[nx][ny] = 2;
virus(nx, ny);
}
}
}
}
function total_score() {
let score = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (temp[i][j] === 0) score++;
}
}
return score;
}
let result = 0;
function dfs(count) {
if (count === 3) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
temp[i][j] = data[i][j];
}
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (temp[i][j] === 2) virus(i, j);
}
}
result = Math.max(result, total_score());
return;
}
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (data[i][j] === 0) {
data[i][j] = 1;
count++;
dfs(count);
data[i][j] = 0;
count--;
}
}
}
}
dfs(0);
console.log(result);