Description
Count battleships on a board. Battleships are represented by X. They can only be placed horizontally or vertically with spaces between them. Return the result as an integer.
Examples
Input:
board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]Output:
2Explanation:
Count only top-left cells of each ship: X at [0,0] has no left/top neighbor (ship 1), X at [0,3] has no top (ship 2).
Input:
board = [["X"]]Output:
1Explanation:
A single 'X' represents one battleship of length 1. Since battleships can be a single cell, this counts as 1 battleship.
Input:
board = [["X","X",".","X"],[".",".",".","."],[".",."X","X","X"],["X",".",".","."]]Output:
11Explanation:
There are three battleships: one horizontal battleship in the first row (positions [0,0] and [0,1]), one horizontal battleship in the third row (positions [2,1], [2,2], and [2,3]), and one single-cell battleship at position [3,0]. The isolated 'X' at [0,3] forms another single-cell battleship, making it 4 total battleships.
Constraints
- •
1 ≤ m, n ≤ 200