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:
4Explanation:
There are four battleships: one horizontal ship at the top-left, one single-cell ship at the top-right, one horizontal ship in the third row, and one single-cell ship in the bottom-left corner.
Constraints
- •
1 ≤ m, n ≤ 200