leetcode 749. 隔离病毒

题解

找出每一处病毒区域和所需防火墙数的对应virusMap,以及每一处病毒区域威胁到的未感染的区块数目uninfectedCells,并找出最具威胁的病毒区域maxVirus;然后maxVirus对应的病毒区域变为2表示已隔离,其他病毒区域则感染周边区块变为1;继续下次循环直到virusMap[maxVirus] == 0

示例代码(go)

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
107
108
109
110
111
112
113
114
115
116
117
118
func containVirus(grid [][]int) int {
firewalls := 0
if len(grid) == 0 {
return firewalls
}

for {
virusMap, maxVirus := getVirusMap(grid)
if virusMap[maxVirus] == 0 {
break
}
cellsTo2(grid, virusMap, maxVirus)
cellsTo1(grid, virusMap, maxVirus)
firewalls += virusMap[maxVirus]
}
return firewalls
}

func getVirusMap(grid [][]int) (map[int]int, int) {
col := len(grid)
row := len(grid[0])
direction := [][]int{[]int{0, -1}, []int{-1, 0}, []int{0, 1}, []int{1, 0}}
maxUninfectedCells := 0
maxVirus := 0
virusMap := make(map[int]int)
infectedCellsVisted := make(map[int]bool)
for i := 0; i < col; i++ {
for j := 0; j < row; j++ {
if grid[i][j] == 1 && !infectedCellsVisted[i*row+j] {
virusList := []int{i*row+j}
infectedCellsVisted[i*row+j] = true
uninfectedCellsVisted := make(map[int]bool)
needWalls := 0
uninfectedCells := 0
for len(virusList) > 0 {
virus := virusList[0]
virusList = virusList[1:]
for _, v := range direction {
ii := virus / row + v[0]
jj := virus % row + v[1]
if ii >= 0 && ii < col && jj >= 0 && jj < row {
if grid[ii][jj] == 0 {
needWalls++
}
if grid[ii][jj] == 0 && !uninfectedCellsVisted[ii*row+jj] {
uninfectedCellsVisted[ii*row+jj] = true
uninfectedCells++
}
if grid[ii][jj] == 1 && !infectedCellsVisted[ii*row+jj] {
infectedCellsVisted[ii*row+jj] = true
virusList = append(virusList, ii*row+jj)
}
}
}
}
virusMap[i*row+j] = needWalls
if uninfectedCells > maxUninfectedCells {
maxUninfectedCells = uninfectedCells
maxVirus = i * row +j
}
}
}
}
return virusMap, maxVirus
}

func cellsTo2(grid [][]int, virusMap map[int]int, maxVirus int) {
col := len(grid)
row := len(grid[0])
direction := [][]int{[]int{0, -1}, []int{-1, 0}, []int{0, 1}, []int{1, 0}}
virusList := []int{maxVirus}
grid[maxVirus / row][maxVirus % row] = 2
for len(virusList) > 0 {
virus := virusList[0]
virusList = virusList[1:]
for _, v := range direction {
ii := virus / row + v[0]
jj := virus % row + v[1]
if ii >= 0 && ii < col && jj >= 0 && jj < row {
if grid[ii][jj] == 1 {
grid[ii][jj] = 2
virusList = append(virusList, ii*row+jj)
}
}
}
}
}

func cellsTo1(grid [][]int, virusMap map[int]int, maxVirus int) {
visted := make(map[int]bool)
col := len(grid)
row := len(grid[0])
direction := [][]int{[]int{0, -1}, []int{-1, 0}, []int{0, 1}, []int{1, 0}}
for k,_ := range virusMap {
if k != maxVirus {
virusList := []int{k}
visted[k] = true
for len(virusList) > 0 {
virus := virusList[0]
virusList = virusList[1:]
for _, v := range direction {
ii := virus / row + v[0]
jj := virus % row + v[1]
if ii >= 0 && ii < col && jj >= 0 && jj < row {
if grid[ii][jj] == 0 {
grid[ii][jj] = 1
visted[ii*row+jj] = true
}
if grid[ii][jj] == 1 && !visted[ii*row+jj] {
visted[ii*row+jj] = true
virusList = append(virusList, ii*row+jj)
}
}
}
}
}
}
}