[Swift]LeetCode999. 車的可用捕獲量 | Available Captures for Rook

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-vhvemxgs-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, and black pawns.  These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.git

The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.  Also, rooks cannot move into the same square as other friendly bishops.github

Return the number of pawns the rook can capture in one move.微信

Example 1:ide

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3 Explanation: In this example the rook is able to capture all the pawns. 

Example 2:this

Input: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 0 Explanation: Bishops are blocking the rook to capture any pawn. 

Example 3:spa

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3 Explanation: The rook can capture the pawns at positions b5, d6 and f5.

Note:code

  1. board.length == board[i].length == 8
  2. board[i][j] is either 'R''.''B', or 'p'
  3. There is exactly one cell with board[i][j] == 'R'

在一個 8 x 8 的棋盤上,有一個白色車(rook)。也可能有空方塊,白色的象(bishop)和黑色的卒(pawn)。它們分別以字符 「R」,「.」,「B」 和 「p」 給出。大寫字符表示白棋,小寫字符表示黑棋。htm

車按國際象棋中的規則移動:它選擇四個基本方向中的一個(北,東,西和南),而後朝那個方向移動,直到它選擇中止、到達棋盤的邊緣或移動到同一方格來捕獲該方格上顏色相反的卒。另外,車不能與其餘友方(白色)象進入同一個方格。blog

返回車可以在一次移動中捕獲到的卒的數量。

示例 1:

輸入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
輸出:3
解釋:
在本例中,車可以捕獲全部的卒。

示例 2:

輸入:[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
輸出:0
解釋:
象阻止了車捕獲任何卒。

示例 3:

輸入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
輸出:3
解釋: 
車能夠捕獲位置 b5,d6 和 f5 的卒。

提示:

  1. board.length == board[i].length == 8
  2. board[i][j] 能夠是 'R''.''B' 或 'p'
  3. 只有一個格子上存在 board[i][j] == 'R'

8ms
 1 class Solution {
 2     func numRookCaptures(_ board: [[Character]]) -> Int {
 3         var rii : Int?
 4         var rjj : Int?
 5         
 6         for i in 0...63 {
 7             if board[i%8][i/8] == "R" {
 8                 rii = i%8
 9                 rjj = i / 8
10                 break
11             }
12         }
13         guard var ri = rii, var rj = rjj else {
14             return 0
15         }
16         
17         var result = 0
18         var i = ri-1
19         while i >= 0 {
20             if board[i][rj] == "B" {
21                 break
22             }
23             if board[i][rj] == "p" {
24                 result += 1
25                 break
26             }
27             i -= 1
28         }
29         i = ri + 1
30         while i < 8 {
31              if board[i][rj] == "B" {
32                 break
33             }
34             if board[i][rj] == "p" {
35                 result += 1
36                 break
37             }
38             i += 1
39         }
40         
41         i = rj-1
42         while i >= 0 {
43             if board[ri][i] == "B" {
44                 break
45             }
46             if board[ri][i] == "p" {
47                 result += 1
48                 break
49             }
50             i -= 1
51         }
52         i = rj + 1
53         while i < 8 {
54              if board[ri][i] == "B" {
55                 break
56             }
57             if board[ri][i] == "p" {
58                 result += 1
59                 break
60             }
61             i += 1
62         }
63         return result
64     }
65 }

Runtime: 12 ms

Memory Usage: 19.8 MB
 1 class Solution {
 2     func numRookCaptures(_ board: [[Character]]) -> Int {
 3         var cnt:Int = 0
 4         var n:Int = board.count
 5         var m:Int = board[0].count
 6         var x:Int = 0
 7         var y:Int = 0
 8         for i in 0..<n
 9         {
10             for j in 0..<m
11             {
12                 if board[i][j] == "R"
13                 {
14                     x = i
15                     y = j
16                     break
17                 }
18             }
19         }
20         for i in stride(from:x - 1,through:0,by:-1)
21         {
22             if board[i][y] == "p"
23             {
24                 cnt += 1
25                 break
26             }
27             else if board[i][y] != "."
28             {
29                 break
30             }
31         }
32         for i in (x + 1)..<n
33         {
34             if board[i][y] == "p"
35             {
36                 cnt += 1
37                 break
38             }
39             else if board[i][y] != "."
40             {
41                 break
42             }            
43         }
44         for j in stride(from:y - 1,through:0,by:-1)
45         {
46             if board[x][j] == "p"
47             {
48                 cnt += 1
49                 break
50             }
51             else if board[x][j] != "."
52             {
53                  break
54             }
55         }
56         for j in (y + 1)..<m
57         {
58             if board[x][j] == "p"
59             {
60                 cnt += 1
61                 break
62             }
63             else if board[x][j] != "."
64             {
65                 break
66             }
67         }
68         return cnt        
69     }
70 }

12ms

 

 1 class Solution {
 2     func numRookCaptures(_ board: [[Character]]) -> Int {
 3         // store the number of black pawns the Rook can catch
 4         var capturablePawns = 0
 5 
 6         // determine where the rook (R) is
 7         var rookSpace = (row: 0, column: 0)
 8         for row in board {
 9             if row.contains("R") {
10                 rookSpace.row = board.index(of: row) ?? 0
11                 for column in row {
12                     if column == "R" {
13                         rookSpace.column = row.index(of: column) ?? 0
14                     }
15                 }
16             }
17         }
18 
19         var R = false
20         var p = false
21 
22         for col in board[rookSpace.row] {
23             if col == "R" {
24                 R = true
25             } else if col == "p" {
26                 p = true
27             } else if col == "B" && (R || p) {
28                 R = false
29                 p = false
30             }
31 
32             if R && p {
33                 capturablePawns += 1
34                 if col == "R" {
35                     p = false
36                 } else if col == "p" {
37                     R = false
38                 }
39             }
40         }
41 
42         R = false
43         p = false
44 
45         for row in board {
46             if row[rookSpace.column] == "R" {
47                 R = true
48             } else if row[rookSpace.column] == "p" {
49                 p = true
50             } else if row[rookSpace.column] == "B" && (R || p) {
51                 R = false
52                 p = false
53             }
54 
55             if R && p {
56                 capturablePawns += 1
57                 if row[rookSpace.column] == "R" {
58                     p = false
59                 } else if row[rookSpace.column] == "p" {
60                     R = false
61                 }
62             }
63         }
64 
65         // return the number of pawns
66         return capturablePawns
67     }
68 }

16ms

  1 class Solution {
  2     func numRookCaptures(_ board: [[Character]]) -> Int {
  3         var rookPosition: (row: Int, column: Int) = (-1, -1)
  4         var soliderPositions: [(row: Int, column: Int)]
  5         
  6         for (rowIndex, row) in board.enumerated(){
  7             for (columnIndex, chess) in row.enumerated(){
  8                 if chess == "R"{
  9                     rookPosition.row = rowIndex
 10                     rookPosition.column = columnIndex
 11                 }
 12             }
 13         }
 14         let up = checkUpper(board: board, rookPosition: rookPosition)
 15         let low = checkLower(board: board, rookPosition: rookPosition)
 16         let left = checkLeft(board: board, rookPosition: rookPosition)
 17         let right = checkRight(board: board, rookPosition: rookPosition)
 18 
 19         return up + low + left + right
 20     }
 21     
 22     func checkUpper(board: [[Character]], rookPosition: (row: Int, column: Int))-> Int{
 23         var count = 0
 24 
 25         if rookPosition.row >= 0 && rookPosition.column >= 0{
 26             var row = rookPosition.row - 1
 27             while row >= 0{
 28                 if board[row][rookPosition.column] == "."{
 29                     row -= 1
 30                 }else if board[row][rookPosition.column] == "p"{
 31                     count += 1
 32                     break
 33                 }else{
 34                     break
 35                 }
 36             }
 37             print(count)
 38         }
 39         return count
 40     }
 41     
 42     func checkLower(board: [[Character]], rookPosition: (row: Int, column: Int)) -> Int{
 43         var count = 0
 44 
 45         if rookPosition.row >= 0 && rookPosition.column >= 0{
 46             var row = rookPosition.row + 1
 47             while row < board.count{
 48                 if board[row][rookPosition.column] == "."{
 49                     row += 1
 50                 }else if board[row][rookPosition.column] == "p"{
 51                     count += 1
 52                     break
 53                 }else{
 54                     break
 55                 }
 56             }
 57             print(count)
 58         }
 59         return count
 60     }
 61     
 62     func checkLeft(board: [[Character]], rookPosition: (row: Int, column: Int))-> Int{
 63         var count = 0
 64         if rookPosition.row >= 0 && rookPosition.column >= 0{
 65             var column = rookPosition.column - 1
 66             
 67             while column > 0{
 68                 if board[rookPosition.row][column] == "."{
 69                     column -= 1
 70                 }else if board[rookPosition.row][column] == "p"{
 71                     count += 1
 72                     break
 73                 }else{
 74                     break
 75                 }
 76             }
 77             print(count)
 78         }
 79         return count
 80     }
 81     func checkRight(board: [[Character]], rookPosition: (row: Int, column: Int))-> Int{
 82         var count = 0
 83 
 84         if rookPosition.row >= 0 && rookPosition.column >= 0{
 85             var column = rookPosition.column + 1
 86             while column < board.count{
 87                 if board[rookPosition.row][column] == "."{
 88                     column += 1
 89                 }else if board[rookPosition.row][column] == "p"{
 90                     count += 1
 91                     break
 92                 }else{
 93                     break
 94                 }
 95             }
 96             print(count)
 97         }
 98         return count
 99     }
100 }
相關文章
相關標籤/搜索