419. Battleships in a Board

419. Battleships in a Board

class Solution:
    def countBattleships(self, board: List[List[str]]) -> int:
        ans=0
        for i in range(len(board)):
            # print(i)
            for j in range(len(board[0])):
                if board[i][j]=='X' and (i==0 or board[i-1][j]=='.') and (j==0 or board[i][j-1]=='.'):
                    ans+=1
            # print()
        
        return ans

什么情况下会产生新的联通区域

你可能感兴趣的:(leetcode)