Leetcode Note: Go - Available Captures for Rook

Available Captures for Rook - LeetCode
https://leetcode.com/problems/available-captures-for-rook/

  • Go 言語で取り組んだメモ

回答

Go Straightforward Solution - 0ms - LeetCode Discuss
https://leetcode.com/problems/available-captures-for-rook/discuss/846263/Go-Straightforward-Solution-0ms

func numRookCaptures(board [][]byte) int {
    var row, col int
    for i := 0; i < 8; i++ {
        for j := 0; j < 8; j++ {
            if board[i][j] == byte('R'){
                row, col = i, j
            }
        }
    }
    
    
    var count int
    // north
    for i := row-1; i >= 0; i-- {
        if board[i][col] == byte('p') {
            count++
            break
        }
        
        if board[i][col] == byte('B') {
            break
        }
    }
    
    //south
    for i := row+1; i < 8; i++ {
        if board[i][col] == byte('p') {
            count++
            break
        }
        
        if board[i][col] == byte('B') {
            break
        }
    }
    
    // east
    for j := col+1; j < 8; j++ {
        if board[row][j] == byte('p') {
            count++
            break
        }
        
        if board[row][j] == byte('B') {
            break
        }
    }
    
    // west
    for j := col-1; j >= 0; j-- {
        if board[row][j] == byte('p') {
            count++
            break
        }
        
        if board[row][j] == byte('B') {
            break
        }
    }
    
    return count
}