diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 49267da7f1cb2..e7f49817005c3 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -63,7 +63,33 @@ ```python - +class Solution: + def orangesRotting(self, grid: List[List[int]]) -> int: + R, C = len(grid), len(grid[0]) + + # queue - all starting cells with rotting oranges + queue = collections.deque() + for r, row in enumerate(grid): + for c, val in enumerate(row): + if val == 2: + queue.append((r, c, 0)) + + def neighbors(r, c) -> (int, int): + for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)): + if 0 <= nr < R and 0 <= nc < C: + yield nr, nc + + d = 0 + while queue: + r, c, d = queue.popleft() + for nr, nc in neighbors(r, c): + if grid[nr][nc] == 1: + grid[nr][nc] = 2 + queue.append((nr, nc, d + 1)) + + if any(1 in row for row in grid): + return -1 + return d ``` ### **Java** @@ -71,7 +97,105 @@ ```java +class Solution { + int[] dr = new int[] { -1, 0, 1, 0 }; + int[] dc = new int[] { 0, -1, 0, 1 }; + + public int orangesRotting(int[][] grid) { + int R = grid.length, C = grid[0].length; + Queue queue = new ArrayDeque(); + Map depth = new HashMap(); + for (int r = 0; r < R; ++r) { + for (int c = 0; c < C; ++c) { + if (grid[r][c] == 2) { + int code = r * C + c; + queue.add(code); + depth.put(code, 0); + } + } + } + int ans = 0; + while (!queue.isEmpty()) { + int code = queue.remove(); + int r = code / C, c = code % C; + for (int k = 0; k < 4; ++k) { + int nr = r + dr[k]; + int nc = c + dc[k]; + if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) { + grid[nr][nc] = 2; + int ncode = nr * C + nc; + queue.add(ncode); + depth.put(ncode, depth.get(code) + 1); + ans = depth.get(ncode); + } + } + } + for (int[] row : grid) { + for (int v : row) { + if (v == 1) { + return -1; + } + } + } + return ans; + } +} +``` + +### **C++** + + +```cpp +class Solution { + int cnt; + int dis[10][10]; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + +public: + int orangesRotting(vector> &grid) { + queue> Q; + memset(dis, -1, sizeof(dis)); + cnt = 0; + int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0; + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grid[i][j] == 2) + { + Q.push(make_pair(i, j)); + dis[i][j] = 0; + } + else if (grid[i][j] == 1) + cnt += 1; + } + } + while (!Q.empty()) + { + pair x = Q.front(); + Q.pop(); + for (int i = 0; i < 4; ++i) + { + int tx = x.first + dir_x[i]; + int ty = x.second + dir_y[i]; + if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty]) + continue; + dis[tx][ty] = dis[x.first][x.second] + 1; + Q.push(make_pair(tx, ty)); + if (grid[tx][ty] == 1) + { + cnt -= 1; + ans = dis[tx][ty]; + if (!cnt) + break; + } + } + } + return cnt ? -1 : ans; + } +}; ``` ### **...** diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index 11251c49a5efd..28e030cce7850 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -57,13 +57,135 @@ ### **Python3** ```python - +class Solution: + def orangesRotting(self, grid: List[List[int]]) -> int: + R, C = len(grid), len(grid[0]) + + # queue - all starting cells with rotting oranges + queue = collections.deque() + for r, row in enumerate(grid): + for c, val in enumerate(row): + if val == 2: + queue.append((r, c, 0)) + + def neighbors(r, c) -> (int, int): + for nr, nc in ((r - 1, c), (r, c - 1), (r + 1, c), (r, c + 1)): + if 0 <= nr < R and 0 <= nc < C: + yield nr, nc + + d = 0 + while queue: + r, c, d = queue.popleft() + for nr, nc in neighbors(r, c): + if grid[nr][nc] == 1: + grid[nr][nc] = 2 + queue.append((nr, nc, d + 1)) + + if any(1 in row for row in grid): + return -1 + return d ``` ### **Java** ```java +class Solution { + int[] dr = new int[] { -1, 0, 1, 0 }; + int[] dc = new int[] { 0, -1, 0, 1 }; + + public int orangesRotting(int[][] grid) { + int R = grid.length, C = grid[0].length; + Queue queue = new ArrayDeque(); + Map depth = new HashMap(); + for (int r = 0; r < R; ++r) { + for (int c = 0; c < C; ++c) { + if (grid[r][c] == 2) { + int code = r * C + c; + queue.add(code); + depth.put(code, 0); + } + } + } + int ans = 0; + while (!queue.isEmpty()) { + int code = queue.remove(); + int r = code / C, c = code % C; + for (int k = 0; k < 4; ++k) { + int nr = r + dr[k]; + int nc = c + dc[k]; + if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) { + grid[nr][nc] = 2; + int ncode = nr * C + nc; + queue.add(ncode); + depth.put(ncode, depth.get(code) + 1); + ans = depth.get(ncode); + } + } + } + for (int[] row : grid) { + for (int v : row) { + if (v == 1) { + return -1; + } + } + } + return ans; + } +} +``` +### **C++** + +```cpp +class Solution { + int cnt; + int dis[10][10]; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + +public: + int orangesRotting(vector> &grid) { + queue> Q; + memset(dis, -1, sizeof(dis)); + cnt = 0; + int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0; + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grid[i][j] == 2) + { + Q.push(make_pair(i, j)); + dis[i][j] = 0; + } + else if (grid[i][j] == 1) + cnt += 1; + } + } + while (!Q.empty()) + { + pair x = Q.front(); + Q.pop(); + for (int i = 0; i < 4; ++i) + { + int tx = x.first + dir_x[i]; + int ty = x.second + dir_y[i]; + if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty]) + continue; + dis[tx][ty] = dis[x.first][x.second] + 1; + Q.push(make_pair(tx, ty)); + if (grid[tx][ty] == 1) + { + cnt -= 1; + ans = dis[tx][ty]; + if (!cnt) + break; + } + } + } + return cnt ? -1 : ans; + } +}; ``` ### **...** diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp new file mode 100644 index 0000000000000..42178e4429268 --- /dev/null +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp @@ -0,0 +1,49 @@ +class Solution { + int cnt; + int dis[10][10]; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + +public: + int orangesRotting(vector> &grid) { + queue> Q; + memset(dis, -1, sizeof(dis)); + cnt = 0; + int n = (int)grid.size(), m = (int)grid[0].size(), ans = 0; + for (int i = 0; i < n; ++i) + { + for (int j = 0; j < m; ++j) + { + if (grid[i][j] == 2) + { + Q.push(make_pair(i, j)); + dis[i][j] = 0; + } + else if (grid[i][j] == 1) + cnt += 1; + } + } + while (!Q.empty()) + { + pair x = Q.front(); + Q.pop(); + for (int i = 0; i < 4; ++i) + { + int tx = x.first + dir_x[i]; + int ty = x.second + dir_y[i]; + if (tx < 0 || tx >= n || ty < 0 || ty >= m || ~dis[tx][ty] || !grid[tx][ty]) + continue; + dis[tx][ty] = dis[x.first][x.second] + 1; + Q.push(make_pair(tx, ty)); + if (grid[tx][ty] == 1) + { + cnt -= 1; + ans = dis[tx][ty]; + if (!cnt) + break; + } + } + } + return cnt ? -1 : ans; + } +}; diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.java b/solution/0900-0999/0994.Rotting Oranges/Solution.java new file mode 100644 index 0000000000000..5a7b8de2bf728 --- /dev/null +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.java @@ -0,0 +1,43 @@ +class Solution { + int[] dr = new int[] { -1, 0, 1, 0 }; + int[] dc = new int[] { 0, -1, 0, 1 }; + + public int orangesRotting(int[][] grid) { + int R = grid.length, C = grid[0].length; + Queue queue = new ArrayDeque(); + Map depth = new HashMap(); + for (int r = 0; r < R; ++r) { + for (int c = 0; c < C; ++c) { + if (grid[r][c] == 2) { + int code = r * C + c; + queue.add(code); + depth.put(code, 0); + } + } + } + int ans = 0; + while (!queue.isEmpty()) { + int code = queue.remove(); + int r = code / C, c = code % C; + for (int k = 0; k < 4; ++k) { + int nr = r + dr[k]; + int nc = c + dc[k]; + if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) { + grid[nr][nc] = 2; + int ncode = nr * C + nc; + queue.add(ncode); + depth.put(ncode, depth.get(code) + 1); + ans = depth.get(ncode); + } + } + } + for (int[] row : grid) { + for (int v : row) { + if (v == 1) { + return -1; + } + } + } + return ans; + } +}