Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34db10a

Browse files
feat: add solutions to lc problem: No.0994 (doocs#661)
No.0994.Rotting Oranges
1 parent b6f5a25 commit 34db10a

File tree

4 files changed

+340
-2
lines changed

4 files changed

+340
-2
lines changed

‎solution/0900-0999/0994.Rotting Oranges/README.md‎

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,139 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
class Solution:
67+
def orangesRotting(self, grid: List[List[int]]) -> int:
68+
R, C = len(grid), len(grid[0])
69+
70+
# queue - all starting cells with rotting oranges
71+
queue = collections.deque()
72+
for r, row in enumerate(grid):
73+
for c, val in enumerate(row):
74+
if val == 2:
75+
queue.append((r, c, 0))
76+
77+
def neighbors(r, c) -> (int, int):
78+
for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)):
79+
if 0 <= nr < R and 0 <= nc < C:
80+
yield nr, nc
81+
82+
d = 0
83+
while queue:
84+
r, c, d = queue.popleft()
85+
for nr, nc in neighbors(r, c):
86+
if grid[nr][nc] == 1:
87+
grid[nr][nc] = 2
88+
queue.append((nr, nc, d + 1))
89+
90+
if any(1 in row for row in grid):
91+
return -1
92+
return d
6793
```
6894

6995
### **Java**
7096

7197
<!-- 这里可写当前语言的特殊实现逻辑 -->
7298

7399
```java
100+
class Solution {
101+
int[] dr = new int[] { -1, 0, 1, 0 };
102+
int[] dc = new int[] { 0, -1, 0, 1 };
103+
104+
public int orangesRotting(int[][] grid) {
105+
int R = grid.length, C = grid[0].length;
106+
Queue<Integer> queue = new ArrayDeque<Integer>();
107+
Map<Integer, Integer> depth = new HashMap<Integer, Integer>();
108+
for (int r = 0; r < R; ++r) {
109+
for (int c = 0; c < C; ++c) {
110+
if (grid[r][c] == 2) {
111+
int code = r * C + c;
112+
queue.add(code);
113+
depth.put(code, 0);
114+
}
115+
}
116+
}
117+
int ans = 0;
118+
while (!queue.isEmpty()) {
119+
int code = queue.remove();
120+
int r = code / C, c = code % C;
121+
for (int k = 0; k < 4; ++k) {
122+
int nr = r + dr[k];
123+
int nc = c + dc[k];
124+
if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
125+
grid[nr][nc] = 2;
126+
int ncode = nr * C + nc;
127+
queue.add(ncode);
128+
depth.put(ncode, depth.get(code) + 1);
129+
ans = depth.get(ncode);
130+
}
131+
}
132+
}
133+
for (int[] row : grid) {
134+
for (int v : row) {
135+
if (v == 1) {
136+
return -1;
137+
}
138+
}
139+
}
140+
return ans;
141+
}
142+
}
143+
```
144+
145+
### **C++**
146+
147+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74148

149+
```cpp
150+
class Solution {
151+
int cnt;
152+
int dis[10][10];
153+
int dir_x[4] = {0, 1, 0, -1};
154+
int dir_y[4] = {1, 0, -1, 0};
155+
156+
public:
157+
int orangesRotting(vector<vector<int>> &grid) {
158+
queue<pair<int, int>> Q;
159+
memset(dis, -1, sizeof(dis));
160+
cnt = 0;
161+
int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0;
162+
for (int i = 0; i < n; ++i)
163+
{
164+
for (int j = 0; j < m; ++j)
165+
{
166+
if (grid[i][j] == 2)
167+
{
168+
Q.push(make_pair(i, j));
169+
dis[i][j] = 0;
170+
}
171+
else if (grid[i][j] == 1)
172+
cnt += 1;
173+
}
174+
}
175+
while (!Q.empty())
176+
{
177+
pair<int, int> x = Q.front();
178+
Q.pop();
179+
for (int i = 0; i < 4; ++i)
180+
{
181+
int tx = x.first + dir_x[i];
182+
int ty = x.second + dir_y[i];
183+
if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty])
184+
continue;
185+
dis[tx][ty] = dis[x.first][x.second] + 1;
186+
Q.push(make_pair(tx, ty));
187+
if (grid[tx][ty] == 1)
188+
{
189+
cnt -= 1;
190+
ans = dis[tx][ty];
191+
if (!cnt)
192+
break;
193+
}
194+
}
195+
}
196+
return cnt ? -1 : ans;
197+
}
198+
};
75199
```
76200
77201
### **...**

‎solution/0900-0999/0994.Rotting Oranges/README_EN.md‎

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,135 @@
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def orangesRotting(self, grid: List[List[int]]) -> int:
62+
R, C = len(grid), len(grid[0])
63+
64+
# queue - all starting cells with rotting oranges
65+
queue = collections.deque()
66+
for r, row in enumerate(grid):
67+
for c, val in enumerate(row):
68+
if val == 2:
69+
queue.append((r, c, 0))
70+
71+
def neighbors(r, c) -> (int, int):
72+
for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)):
73+
if 0 <= nr < R and 0 <= nc < C:
74+
yield nr, nc
75+
76+
d = 0
77+
while queue:
78+
r, c, d = queue.popleft()
79+
for nr, nc in neighbors(r, c):
80+
if grid[nr][nc] == 1:
81+
grid[nr][nc] = 2
82+
queue.append((nr, nc, d + 1))
83+
84+
if any(1 in row for row in grid):
85+
return -1
86+
return d
6187
```
6288

6389
### **Java**
6490

6591
```java
92+
class Solution {
93+
int[] dr = new int[] { -1, 0, 1, 0 };
94+
int[] dc = new int[] { 0, -1, 0, 1 };
95+
96+
public int orangesRotting(int[][] grid) {
97+
int R = grid.length, C = grid[0].length;
98+
Queue<Integer> queue = new ArrayDeque<Integer>();
99+
Map<Integer, Integer> depth = new HashMap<Integer, Integer>();
100+
for (int r = 0; r < R; ++r) {
101+
for (int c = 0; c < C; ++c) {
102+
if (grid[r][c] == 2) {
103+
int code = r * C + c;
104+
queue.add(code);
105+
depth.put(code, 0);
106+
}
107+
}
108+
}
109+
int ans = 0;
110+
while (!queue.isEmpty()) {
111+
int code = queue.remove();
112+
int r = code / C, c = code % C;
113+
for (int k = 0; k < 4; ++k) {
114+
int nr = r + dr[k];
115+
int nc = c + dc[k];
116+
if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
117+
grid[nr][nc] = 2;
118+
int ncode = nr * C + nc;
119+
queue.add(ncode);
120+
depth.put(ncode, depth.get(code) + 1);
121+
ans = depth.get(ncode);
122+
}
123+
}
124+
}
125+
for (int[] row : grid) {
126+
for (int v : row) {
127+
if (v == 1) {
128+
return -1;
129+
}
130+
}
131+
}
132+
return ans;
133+
}
134+
}
135+
```
66136

137+
### **C++**
138+
139+
```cpp
140+
class Solution {
141+
int cnt;
142+
int dis[10][10];
143+
int dir_x[4] = {0, 1, 0, -1};
144+
int dir_y[4] = {1, 0, -1, 0};
145+
146+
public:
147+
int orangesRotting(vector<vector<int>> &grid) {
148+
queue<pair<int, int>> Q;
149+
memset(dis, -1, sizeof(dis));
150+
cnt = 0;
151+
int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0;
152+
for (int i = 0; i < n; ++i)
153+
{
154+
for (int j = 0; j < m; ++j)
155+
{
156+
if (grid[i][j] == 2)
157+
{
158+
Q.push(make_pair(i, j));
159+
dis[i][j] = 0;
160+
}
161+
else if (grid[i][j] == 1)
162+
cnt += 1;
163+
}
164+
}
165+
while (!Q.empty())
166+
{
167+
pair<int, int> x = Q.front();
168+
Q.pop();
169+
for (int i = 0; i < 4; ++i)
170+
{
171+
int tx = x.first + dir_x[i];
172+
int ty = x.second + dir_y[i];
173+
if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty])
174+
continue;
175+
dis[tx][ty] = dis[x.first][x.second] + 1;
176+
Q.push(make_pair(tx, ty));
177+
if (grid[tx][ty] == 1)
178+
{
179+
cnt -= 1;
180+
ans = dis[tx][ty];
181+
if (!cnt)
182+
break;
183+
}
184+
}
185+
}
186+
return cnt ? -1 : ans;
187+
}
188+
};
67189
```
68190
69191
### **...**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
int cnt;
3+
int dis[10][10];
4+
int dir_x[4] = {0, 1, 0, -1};
5+
int dir_y[4] = {1, 0, -1, 0};
6+
7+
public:
8+
int orangesRotting(vector<vector<int>> &grid) {
9+
queue<pair<int, int>> Q;
10+
memset(dis, -1, sizeof(dis));
11+
cnt = 0;
12+
int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0;
13+
for (int i = 0; i < n; ++i)
14+
{
15+
for (int j = 0; j < m; ++j)
16+
{
17+
if (grid[i][j] == 2)
18+
{
19+
Q.push(make_pair(i, j));
20+
dis[i][j] = 0;
21+
}
22+
else if (grid[i][j] == 1)
23+
cnt += 1;
24+
}
25+
}
26+
while (!Q.empty())
27+
{
28+
pair<int, int> x = Q.front();
29+
Q.pop();
30+
for (int i = 0; i < 4; ++i)
31+
{
32+
int tx = x.first + dir_x[i];
33+
int ty = x.second + dir_y[i];
34+
if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty])
35+
continue;
36+
dis[tx][ty] = dis[x.first][x.second] + 1;
37+
Q.push(make_pair(tx, ty));
38+
if (grid[tx][ty] == 1)
39+
{
40+
cnt -= 1;
41+
ans = dis[tx][ty];
42+
if (!cnt)
43+
break;
44+
}
45+
}
46+
}
47+
return cnt ? -1 : ans;
48+
}
49+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
int[] dr = new int[] { -1, 0, 1, 0 };
3+
int[] dc = new int[] { 0, -1, 0, 1 };
4+
5+
public int orangesRotting(int[][] grid) {
6+
int R = grid.length, C = grid[0].length;
7+
Queue<Integer> queue = new ArrayDeque<Integer>();
8+
Map<Integer, Integer> depth = new HashMap<Integer, Integer>();
9+
for (int r = 0; r < R; ++r) {
10+
for (int c = 0; c < C; ++c) {
11+
if (grid[r][c] == 2) {
12+
int code = r * C + c;
13+
queue.add(code);
14+
depth.put(code, 0);
15+
}
16+
}
17+
}
18+
int ans = 0;
19+
while (!queue.isEmpty()) {
20+
int code = queue.remove();
21+
int r = code / C, c = code % C;
22+
for (int k = 0; k < 4; ++k) {
23+
int nr = r + dr[k];
24+
int nc = c + dc[k];
25+
if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
26+
grid[nr][nc] = 2;
27+
int ncode = nr * C + nc;
28+
queue.add(ncode);
29+
depth.put(ncode, depth.get(code) + 1);
30+
ans = depth.get(ncode);
31+
}
32+
}
33+
}
34+
for (int[] row : grid) {
35+
for (int v : row) {
36+
if (v == 1) {
37+
return -1;
38+
}
39+
}
40+
}
41+
return ans;
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /