Skip to content

Commit 800326c

Browse files
committed
598.zombie-in-matrix
1 parent 74d08fa commit 800326c

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ This is a **continually updated, open source** project.
416416
| 589.connecting-graph | [cpp](./lintcode/589.connecting-graph.cpp), [python](./lintcode/589.connecting-graph.py) | O(1) | O(N) | Medium | union-find
417417
| 590.connecting-graph-ii | [cpp](./lintcode/590.connecting-graph-ii.cpp), [python](./lintcode/590.connecting-graph-ii.py) | O(1) | O(N) | Medium | union-find
418418
| 591.connecting-graph-iii | [cpp](./lintcode/591.connecting-graph-iii.cpp), [python](./lintcode/591.connecting-graph-iii.py) | O(1) | O(N) | Medium | union-find
419+
| 598.zombie-in-matrix | [cpp](./lintcode/598.zombie-in-matrix.cpp), [python](./lintcode/598.zombie-in-matrix.py) | O(M * N) | O(M * N) | Medium | graph-traversal
419420
| 618.search-graph-nodes | [cpp](./lintcode/618.search-graph-nodes.cpp), [python](./lintcode/618.search-graph-nodes.py) | O(V + E) | O(V) | Medium | tree-traversal
420421
| 629.minimum-spanning-tree | [cpp](./lintcode/629.minimum-spanning-tree.cpp), [python](./lintcode/629.minimum-spanning-tree.py) | O(N * logN) | O(N) | Hard | union-find | Prim, Kruskal
421422
| 652.factorization | [cpp](./lintcode/652.factorization.cpp), [python](./lintcode/652.factorization.py) | O(N) | O(LogN) | Medium/S++ | N = input number

lintcode/598.zombie-in-matrix.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Solution {
2+
3+
private:
4+
vector<vector<int>> directions = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
5+
6+
bool in_bound(vector<vector<int>> &grid, int x, int y)
7+
{
8+
return x >= 0 && x < grid.size() && y >= 0 && y < grid[x].size();
9+
}
10+
11+
int convert(vector<vector<int>> &grid, int x, int y)
12+
{
13+
return x * grid[x].size() + y;
14+
}
15+
16+
public:
17+
/**
18+
* @param grid: a 2D integer grid
19+
* @return: an integer
20+
*/
21+
int zombie(vector<vector<int>> &grid) {
22+
// write your code here
23+
24+
int peopel_count = 0;
25+
queue<pair<int, int>> q;
26+
unordered_set<int> visited;
27+
28+
for (auto i = 0; i < grid.size(); ++i)
29+
{
30+
for (auto j = 0; j < grid[i].size(); ++j)
31+
{
32+
if (grid[i][j] == 0)
33+
{
34+
peopel_count++;
35+
continue;
36+
}
37+
38+
if (grid[i][j] == 1)
39+
{
40+
q.push(make_pair(i, j));
41+
visited.insert(convert(grid, i, j));
42+
}
43+
}
44+
}
45+
46+
int days = 0;
47+
while(!q.empty() && peopel_count > 0)
48+
{
49+
int size = q.size();
50+
for (auto i = 0; i < size; ++i)
51+
{
52+
pair<int, int>tmp = q.front();
53+
q.pop();
54+
55+
for (auto i = 0; i < 4; ++i)
56+
{
57+
int new_x = tmp.first + directions[i][0];
58+
int new_y = tmp.second + directions[i][1];
59+
int point = convert(grid, new_x, new_y);
60+
61+
if (in_bound(grid, new_x, new_y) && grid[new_x][new_y] == 0 && visited.count(point) == 0)
62+
{
63+
visited.insert(point);
64+
q.push(make_pair(new_x, new_y));
65+
grid[new_x][new_y] = 1;
66+
peopel_count--;
67+
}
68+
}
69+
}
70+
71+
days++;
72+
}
73+
74+
return peopel_count == 0 ? days : -1;
75+
}
76+
};

lintcode/598.zombie-in-matrix.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution:
2+
"""
3+
@param grid: a 2D integer grid
4+
@return: an integer
5+
"""
6+
def zombie(self, grid):
7+
# write your code here
8+
people = 0
9+
visited = set()
10+
q = []
11+
12+
for i in range(len(grid)):
13+
for j in range(len(grid[i])):
14+
if grid[i][j] == 0:
15+
people += 1
16+
17+
if grid[i][j] == 1:
18+
q.append((i, j))
19+
visited.add((i, j))
20+
21+
22+
directions = [[0, -1], [-1, 0], [0, 1], [1, 0]]
23+
24+
days = 0
25+
while people > 0 and len(q) > 0:
26+
size = len(q)
27+
28+
for _ in range(size):
29+
tmp = q.pop(0)
30+
31+
for j in range(4):
32+
new_x = tmp[0] + directions[j][0]
33+
new_y = tmp[1] + directions[j][1]
34+
35+
if self.in_bounds(grid, new_x, new_y) and grid[new_x][new_y] == 0 and (new_x, new_y) not in visited:
36+
visited.add((new_x, new_y))
37+
q.append((new_x, new_y))
38+
grid[new_x][new_y] = 1
39+
people -= 1
40+
41+
days += 1
42+
return days if people == 0 else -1
43+
44+
def in_bounds(self, grid, x, y):
45+
return 0 <= x < len(grid) and 0 <= y < len(grid[x])

0 commit comments

Comments
 (0)