From 3f9c4c4e9ef9794288bcfb4505a55ce8a6d2af91 Mon Sep 17 00:00:00 2001 From: shishitao <85476205+shishitao@users.noreply.github.com> Date: Thu, 6 Jan 2022 09:26:03 -0600 Subject: [PATCH 01/11] Update README.md --- .../0900-0999/0994.Rotting Oranges/README.md | 115 +++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 49267da7f1cb2..466ff12e1bc7f 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,94 @@ ```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++** + + +```c++ +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; + } +}; ``` ### **...** From c444154559de8cf95de1ba66d7fdf18139432b34 Mon Sep 17 00:00:00 2001 From: shishitao <85476205+shishitao@users.noreply.github.com> Date: Thu, 6 Jan 2022 09:48:02 -0600 Subject: [PATCH 02/11] Update README_EN.md --- .../0994.Rotting Oranges/README_EN.md | 113 +++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index 11251c49a5efd..ec3a187676856 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -57,13 +57,124 @@ ### **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++** + +```c++ +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; + } +}; ``` ### **...** From ad8181329fc462cdf046da1c4f5c7185bb22b3c8 Mon Sep 17 00:00:00 2001 From: shishitao <85476205+shishitao@users.noreply.github.com> Date: Thu, 6 Jan 2022 10:03:24 -0600 Subject: [PATCH 03/11] Update README_EN.md --- .../0994.Rotting Oranges/README_EN.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index ec3a187676856..75061b6956ee4 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -136,36 +136,37 @@ class Solution { ### **C++** -```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; + int orangesRotting(vector < vector < int >> & grid) { + queue < pair < int, int > > 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){ + 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; + } + else if (grid[i][j] == 1) cnt += 1; } } - while (!Q.empty()){ - pair x = Q.front();Q.pop(); - for (int i = 0; i < 4; ++i){ + while (!Q.empty()) { + pair < int, int > 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; + 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){ + if (grid[tx][ty] == 1) { cnt -= 1; ans = dis[tx][ty]; if (!cnt) break; From 20329bb966d974f0c2743f1148ab21c0f4779c2c Mon Sep 17 00:00:00 2001 From: shishitao <85476205+shishitao@users.noreply.github.com> Date: Thu, 6 Jan 2022 10:05:44 -0600 Subject: [PATCH 04/11] Update README.md --- .../0900-0999/0994.Rotting Oranges/README.md | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 466ff12e1bc7f..4c99f67e1f826 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -146,36 +146,37 @@ class Solution { -```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; + int orangesRotting(vector < vector < int >> & grid) { + queue < pair < int, int > > 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){ + 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; + } + else if (grid[i][j] == 1) cnt += 1; } } - while (!Q.empty()){ - pair x = Q.front();Q.pop(); - for (int i = 0; i < 4; ++i){ + while (!Q.empty()) { + pair < int, int > 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; + 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){ + if (grid[tx][ty] == 1) { cnt -= 1; ans = dis[tx][ty]; if (!cnt) break; From 5e1c62ecb1ff9cbe46109e97388fa1528d3de96d Mon Sep 17 00:00:00 2001 From: shishitao <85476205+shishitao@users.noreply.github.com> Date: Thu, 6 Jan 2022 10:08:59 -0600 Subject: [PATCH 05/11] Add files via upload --- .../0994.Rotting Oranges/Solution.cpp | 39 +++++++++++++++++ .../0994.Rotting Oranges/Solution.java | 43 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 solution/0900-0999/0994.Rotting Oranges/Solution.cpp create mode 100644 solution/0900-0999/0994.Rotting Oranges/Solution.java 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..252e3a2bbfc60 --- /dev/null +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp @@ -0,0 +1,39 @@ +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 < vector < int >> & grid) { + queue < pair < int, int > > 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 < int, int > 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; + } +}; \ No newline at end of file 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..087084a09b938 --- /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; + } +} \ No newline at end of file From 0096b30fa70dbdc5fa50c932df7770584b70e241 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:15:38 +0800 Subject: [PATCH 06/11] Update Solution.cpp --- .../0994.Rotting Oranges/Solution.cpp | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp index 252e3a2bbfc60..42178e4429268 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp @@ -1,39 +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}; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + public: - int orangesRotting(vector < vector < int >> & grid) { - queue < pair < int, int > > Q; + 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) { + 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; + } + else if (grid[i][j] == 1) + cnt += 1; } } - while (!Q.empty()) { - pair < int, int > x = Q.front(); + while (!Q.empty()) + { + pair x = Q.front(); Q.pop(); - for (int i = 0; i < 4; ++i) { + 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; + 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) { + if (grid[tx][ty] == 1) + { cnt -= 1; ans = dis[tx][ty]; - if (!cnt) break; + if (!cnt) + break; } } } return cnt ? -1 : ans; } -}; \ No newline at end of file +}; From b40b49c5406612b32799921bbf1c04ba800c3c8e Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:16:06 +0800 Subject: [PATCH 07/11] Update README.md --- .../0900-0999/0994.Rotting Oranges/README.md | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 4c99f67e1f826..6bb7580194f6f 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -150,36 +150,46 @@ class Solution { class Solution { int cnt; int dis[10][10]; - int dir_x[4]={0, 1, 0, -1}; - int dir_y[4]={1, 0, -1, 0}; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + public: - int orangesRotting(vector < vector < int >> & grid) { - queue < pair < int, int > > Q; + 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) { + 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; + } + else if (grid[i][j] == 1) + cnt += 1; } } - while (!Q.empty()) { - pair < int, int > x = Q.front(); + while (!Q.empty()) + { + pair x = Q.front(); Q.pop(); - for (int i = 0; i < 4; ++i) { + 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; + 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) { + if (grid[tx][ty] == 1) + { cnt -= 1; ans = dis[tx][ty]; - if (!cnt) break; + if (!cnt) + break; } } } From 22dea69be70c3f8bb08a638b18b1281b0ed2f198 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:16:56 +0800 Subject: [PATCH 08/11] Update README_EN.md --- .../0994.Rotting Oranges/README_EN.md | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index 75061b6956ee4..84c1d05960010 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -140,36 +140,46 @@ class Solution { class Solution { int cnt; int dis[10][10]; - int dir_x[4]={0, 1, 0, -1}; - int dir_y[4]={1, 0, -1, 0}; + int dir_x[4] = {0, 1, 0, -1}; + int dir_y[4] = {1, 0, -1, 0}; + public: - int orangesRotting(vector < vector < int >> & grid) { - queue < pair < int, int > > Q; + 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) { + 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; + } + else if (grid[i][j] == 1) + cnt += 1; } } - while (!Q.empty()) { - pair < int, int > x = Q.front(); + while (!Q.empty()) + { + pair x = Q.front(); Q.pop(); - for (int i = 0; i < 4; ++i) { + 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; + 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) { + if (grid[tx][ty] == 1) + { cnt -= 1; ans = dis[tx][ty]; - if (!cnt) break; + if (!cnt) + break; } } } From c26c855d8d94f0f569abc745107c178c4f8ebf7c Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:19:24 +0800 Subject: [PATCH 09/11] Update README.md --- solution/0900-0999/0994.Rotting Oranges/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README.md b/solution/0900-0999/0994.Rotting Oranges/README.md index 6bb7580194f6f..e7f49817005c3 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README.md +++ b/solution/0900-0999/0994.Rotting Oranges/README.md @@ -98,8 +98,8 @@ class Solution: ```java class Solution { - int[] dr = new int[]{-1, 0, 1, 0}; - int[] dc = new int[]{0, -1, 0, 1}; + 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; @@ -130,8 +130,8 @@ class Solution { } } } - for (int[] row: grid) { - for (int v: row) { + for (int[] row : grid) { + for (int v : row) { if (v == 1) { return -1; } From 0531003d0b724f9f7b609bc70748e773e13d3fd3 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:20:01 +0800 Subject: [PATCH 10/11] Update README_EN.md --- solution/0900-0999/0994.Rotting Oranges/README_EN.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/README_EN.md b/solution/0900-0999/0994.Rotting Oranges/README_EN.md index 84c1d05960010..28e030cce7850 100644 --- a/solution/0900-0999/0994.Rotting Oranges/README_EN.md +++ b/solution/0900-0999/0994.Rotting Oranges/README_EN.md @@ -90,8 +90,8 @@ class Solution: ```java class Solution { - int[] dr = new int[]{-1, 0, 1, 0}; - int[] dc = new int[]{0, -1, 0, 1}; + 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; @@ -122,8 +122,8 @@ class Solution { } } } - for (int[] row: grid) { - for (int v: row) { + for (int[] row : grid) { + for (int v : row) { if (v == 1) { return -1; } From a56432364d10dd08fe92375e4f9bfad101409b3a Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Fri, 7 Jan 2022 00:20:21 +0800 Subject: [PATCH 11/11] Update Solution.java --- solution/0900-0999/0994.Rotting Oranges/Solution.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.java b/solution/0900-0999/0994.Rotting Oranges/Solution.java index 087084a09b938..5a7b8de2bf728 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.java +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.java @@ -1,6 +1,6 @@ class Solution { - int[] dr = new int[]{-1, 0, 1, 0}; - int[] dc = new int[]{0, -1, 0, 1}; + 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; @@ -31,8 +31,8 @@ public int orangesRotting(int[][] grid) { } } } - for (int[] row: grid) { - for (int v: row) { + for (int[] row : grid) { + for (int v : row) { if (v == 1) { return -1; } @@ -40,4 +40,4 @@ public int orangesRotting(int[][] grid) { } return ans; } -} \ No newline at end of file +}