diff --git a/solution/0900-0999/0999.Available Captures for Rook/README.md b/solution/0900-0999/0999.Available Captures for Rook/README.md index 4941ea9fd82ad..77db9ff3aa7a4 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/README.md +++ b/solution/0900-0999/0999.Available Captures for Rook/README.md @@ -80,11 +80,10 @@ tags: ### 方法一:模拟 -我们先遍历棋盘,找到车的位置 $(x, y)$,然后从 $(x, y)$ 出发,向上下左右四个方向遍历: +我们先遍历棋盘,找到车的位置 $(i, j)$,然后从 $(i, j)$ 出发,向上下左右四个方向遍历: -- 如果遇到象或者边界,那么该方向停止遍历; -- 如果遇到卒,那么答案加一,然后该方向停止遍历; -- 否则,继续遍历。 +- 如果不是边界且不是象,则继续向前走; +- 如果是卒,则答案加一,并停止该方向的遍历。 遍历完四个方向后,即可得到答案。 @@ -97,21 +96,20 @@ tags: ```python class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: - ans = 0 dirs = (-1, 0, 1, 0, -1) - for i in range(8): - for j in range(8): + n = len(board) + for i in range(n): + for j in range(n): if board[i][j] == "R": + ans = 0 for a, b in pairwise(dirs): - x, y = i, j - while 0 <= x + a < 8 and 0 <= y + b < 8: - x, y = x + a, y + b + x, y = i + a, j + b + while 0 <= x < n and 0 <= y < n and board[x][y] != "B": if board[x][y] == "p": ans += 1 break - if board[x][y] == "B": - break - return ans + x, y = x + a, y + b + return ans ``` #### Java @@ -119,28 +117,28 @@ class Solution: ```java class Solution { public int numRookCaptures(char[][] board) { - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + final int[] dirs = {-1, 0, 1, 0, -1}; + int n = board.length; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 - && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } } ``` @@ -151,27 +149,28 @@ class Solution { class Solution { public: int numRookCaptures(vector>& board) { - int ans = 0; - int dirs[5] = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + const int dirs[5] = {-1, 0, 1, 0, -1}; + int n = board.size(); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } }; ``` @@ -180,25 +179,93 @@ public: ```go func numRookCaptures(board [][]byte) (ans int) { - dirs := [5]int{-1, 0, 1, 0, -1} - for i := 0; i < 8; i++ { - for j := 0; j < 8; j++ { - if board[i][j] == 'R' { - for k := 0; k < 4; k++ { - x, y := i, j - a, b := dirs[k], dirs[k+1] - for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' { - x, y = x+a, y+b - if board[x][y] == 'p' { - ans++ - break - } - } - } - } - } - } - return + dirs := []int{-1, 0, 1, 0, -1} + n := len(board) + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + if board[i][j] == 'R' { + for k := 0; k < 4; k++ { + x, y := i + dirs[k], j + dirs[k+1] + for x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B' { + if board[x][y] == 'p' { + ans++ + break + } + x += dirs[k] + y += dirs[k+1] + } + } + return + } + } + } + return +} +``` + +#### TypeScript + +```ts +function numRookCaptures(board: string[][]): number { + const dirs = [-1, 0, 1, 0, -1]; + const n = board.length; + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === 'R') { + let ans = 0; + for (let k = 0; k < 4; k++) { + let [x, y] = [i + dirs[k], j + dirs[k + 1]]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] !== 'B') { + if (board[x][y] === 'p') { + ans++; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + return 0; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn num_rook_captures(board: Vec>) -> i32 { + let dirs = [-1, 0, 1, 0, -1]; + let n = board.len(); + for i in 0..n { + for j in 0..n { + if board[i][j] == 'R' { + let mut ans = 0; + for k in 0..4 { + let mut x = i as i32 + dirs[k]; + let mut y = j as i32 + dirs[k + 1]; + while x >= 0 + && x < n as i32 + && y >= 0 + && y < n as i32 + && board[x as usize][y as usize] != 'B' + { + if board[x as usize][y as usize] == 'p' { + ans += 1; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + 0 + } } ``` diff --git a/solution/0900-0999/0999.Available Captures for Rook/README_EN.md b/solution/0900-0999/0999.Available Captures for Rook/README_EN.md index fd5d8142ec6d5..b06c59c2811b4 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/README_EN.md +++ b/solution/0900-0999/0999.Available Captures for Rook/README_EN.md @@ -81,15 +81,14 @@ tags: ### Solution 1: Simulation -First, we traverse the chessboard to find the position of the rook $(x, y)$. Then, starting from $(x, y)$, we traverse in four directions: up, down, left, and right: +We first traverse the board to find the position of the rook $(i, j)$. Then, starting from $(i, j)$, we traverse in four directions: up, down, left, and right. -- If we encounter a bishop or a boundary, we stop traversing in that direction. -- If we encounter a pawn, we increment the answer by one, and then stop traversing in that direction. -- Otherwise, we continue traversing. +- If it is not the boundary and not a bishop, continue moving forward. +- If it is a pawn, increment the answer by one and stop traversing in that direction. -After traversing in all four directions, we can get the answer. +After traversing in all four directions, we get the answer. -The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the chessboard, respectively. In this problem, $m = n = 8$. The space complexity is $O(1)$. +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the board, respectively. In this problem, $m = n = 8$. The space complexity is $O(1)$. @@ -98,21 +97,20 @@ The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows ```python class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: - ans = 0 dirs = (-1, 0, 1, 0, -1) - for i in range(8): - for j in range(8): + n = len(board) + for i in range(n): + for j in range(n): if board[i][j] == "R": + ans = 0 for a, b in pairwise(dirs): - x, y = i, j - while 0 <= x + a < 8 and 0 <= y + b < 8: - x, y = x + a, y + b + x, y = i + a, j + b + while 0 <= x < n and 0 <= y < n and board[x][y] != "B": if board[x][y] == "p": ans += 1 break - if board[x][y] == "B": - break - return ans + x, y = x + a, y + b + return ans ``` #### Java @@ -120,28 +118,28 @@ class Solution: ```java class Solution { public int numRookCaptures(char[][] board) { - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + final int[] dirs = {-1, 0, 1, 0, -1}; + int n = board.length; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 - && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } } ``` @@ -152,27 +150,28 @@ class Solution { class Solution { public: int numRookCaptures(vector>& board) { - int ans = 0; - int dirs[5] = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + const int dirs[5] = {-1, 0, 1, 0, -1}; + int n = board.size(); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } }; ``` @@ -181,25 +180,93 @@ public: ```go func numRookCaptures(board [][]byte) (ans int) { - dirs := [5]int{-1, 0, 1, 0, -1} - for i := 0; i < 8; i++ { - for j := 0; j < 8; j++ { - if board[i][j] == 'R' { - for k := 0; k < 4; k++ { - x, y := i, j - a, b := dirs[k], dirs[k+1] - for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' { - x, y = x+a, y+b - if board[x][y] == 'p' { - ans++ - break - } - } - } - } - } - } - return + dirs := []int{-1, 0, 1, 0, -1} + n := len(board) + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + if board[i][j] == 'R' { + for k := 0; k < 4; k++ { + x, y := i + dirs[k], j + dirs[k+1] + for x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B' { + if board[x][y] == 'p' { + ans++ + break + } + x += dirs[k] + y += dirs[k+1] + } + } + return + } + } + } + return +} +``` + +#### TypeScript + +```ts +function numRookCaptures(board: string[][]): number { + const dirs = [-1, 0, 1, 0, -1]; + const n = board.length; + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === 'R') { + let ans = 0; + for (let k = 0; k < 4; k++) { + let [x, y] = [i + dirs[k], j + dirs[k + 1]]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] !== 'B') { + if (board[x][y] === 'p') { + ans++; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + return 0; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn num_rook_captures(board: Vec>) -> i32 { + let dirs = [-1, 0, 1, 0, -1]; + let n = board.len(); + for i in 0..n { + for j in 0..n { + if board[i][j] == 'R' { + let mut ans = 0; + for k in 0..4 { + let mut x = i as i32 + dirs[k]; + let mut y = j as i32 + dirs[k + 1]; + while x >= 0 + && x < n as i32 + && y >= 0 + && y < n as i32 + && board[x as usize][y as usize] != 'B' + { + if board[x as usize][y as usize] == 'p' { + ans += 1; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + 0 + } } ``` diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp b/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp index e76f34c9c061d..aa664b9e70c0f 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp @@ -1,26 +1,27 @@ class Solution { public: int numRookCaptures(vector>& board) { - int ans = 0; - int dirs[5] = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + const int dirs[5] = {-1, 0, 1, 0, -1}; + int n = board.size(); + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } -}; \ No newline at end of file +}; diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.go b/solution/0900-0999/0999.Available Captures for Rook/Solution.go index b3f2d85e42df6..38b82183d532b 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/Solution.go +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.go @@ -1,21 +1,23 @@ func numRookCaptures(board [][]byte) (ans int) { - dirs := [5]int{-1, 0, 1, 0, -1} - for i := 0; i < 8; i++ { - for j := 0; j < 8; j++ { - if board[i][j] == 'R' { - for k := 0; k < 4; k++ { - x, y := i, j - a, b := dirs[k], dirs[k+1] - for x+a >= 0 && x+a < 8 && y+b >= 0 && y+b < 8 && board[x+a][y+b] != 'B' { - x, y = x+a, y+b - if board[x][y] == 'p' { - ans++ - break - } - } - } - } - } - } - return -} \ No newline at end of file + dirs := []int{-1, 0, 1, 0, -1} + n := len(board) + for i := 0; i < n; i++ { + for j := 0; j < n; j++ { + if board[i][j] == 'R' { + for k := 0; k < 4; k++ { + x, y := i + dirs[k], j + dirs[k+1] + for x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B' { + if board[x][y] == 'p' { + ans++ + break + } + x += dirs[k] + y += dirs[k+1] + } + } + return + } + } + } + return +} diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.java b/solution/0900-0999/0999.Available Captures for Rook/Solution.java index 0a3d2a18d6145..5df81690615a6 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/Solution.java +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.java @@ -1,26 +1,26 @@ class Solution { public int numRookCaptures(char[][] board) { - int ans = 0; - int[] dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < 8; ++i) { - for (int j = 0; j < 8; ++j) { + final int[] dirs = {-1, 0, 1, 0, -1}; + int n = board.length; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == 'R') { + int ans = 0; for (int k = 0; k < 4; ++k) { - int x = i, y = j; - int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < 8 && y + b >= 0 && y + b < 8 - && board[x + a][y + b] != 'B') { - x += a; - y += b; + int x = i + dirs[k], y = j + dirs[k + 1]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] != 'B') { if (board[x][y] == 'p') { ++ans; break; } + x += dirs[k]; + y += dirs[k + 1]; } } + return ans; } } } - return ans; + return 0; } -} \ No newline at end of file +} diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.py b/solution/0900-0999/0999.Available Captures for Rook/Solution.py index e4624ad058cf0..acb7e73ef3fad 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/Solution.py +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.py @@ -1,17 +1,16 @@ class Solution: def numRookCaptures(self, board: List[List[str]]) -> int: - ans = 0 dirs = (-1, 0, 1, 0, -1) - for i in range(8): - for j in range(8): + n = len(board) + for i in range(n): + for j in range(n): if board[i][j] == "R": + ans = 0 for a, b in pairwise(dirs): - x, y = i, j - while 0 <= x + a < 8 and 0 <= y + b < 8: - x, y = x + a, y + b + x, y = i + a, j + b + while 0 <= x < n and 0 <= y < n and board[x][y] != "B": if board[x][y] == "p": ans += 1 break - if board[x][y] == "B": - break - return ans + x, y = x + a, y + b + return ans diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.rs b/solution/0900-0999/0999.Available Captures for Rook/Solution.rs new file mode 100644 index 0000000000000..9ad8722e15ff9 --- /dev/null +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.rs @@ -0,0 +1,32 @@ +impl Solution { + pub fn num_rook_captures(board: Vec>) -> i32 { + let dirs = [-1, 0, 1, 0, -1]; + let n = board.len(); + for i in 0..n { + for j in 0..n { + if board[i][j] == 'R' { + let mut ans = 0; + for k in 0..4 { + let mut x = i as i32 + dirs[k]; + let mut y = j as i32 + dirs[k + 1]; + while x >= 0 + && x < n as i32 + && y >= 0 + && y < n as i32 + && board[x as usize][y as usize] != 'B' + { + if board[x as usize][y as usize] == 'p' { + ans += 1; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + 0 + } +} diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.ts b/solution/0900-0999/0999.Available Captures for Rook/Solution.ts new file mode 100644 index 0000000000000..062e6e050c6da --- /dev/null +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.ts @@ -0,0 +1,24 @@ +function numRookCaptures(board: string[][]): number { + const dirs = [-1, 0, 1, 0, -1]; + const n = board.length; + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + if (board[i][j] === 'R') { + let ans = 0; + for (let k = 0; k < 4; k++) { + let [x, y] = [i + dirs[k], j + dirs[k + 1]]; + while (x >= 0 && x < n && y >= 0 && y < n && board[x][y] !== 'B') { + if (board[x][y] === 'p') { + ans++; + break; + } + x += dirs[k]; + y += dirs[k + 1]; + } + } + return ans; + } + } + } + return 0; +}