在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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. 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. Return the number of pawns the rook can capture in one move. Example 1: Input: 3
Explanation:
In this example the rook is able to capture all the pawns.
Example 2: Input: 0
Explanation:
Bishops are blocking the rook to capture any pawn.
Example 3: Input: 3
Explanation:
The rook can capture the pawns at positions b5, d6 and f5.
Note:
在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。 车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。 返回车能够在一次移动中捕获到的卒的数量。 示例 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 的卒。 提示:
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 }
|
请发表评论