Problem 500
Battleships in a Board
Medium · 20 pts · Matrix, Depth-First Search
Problem
A board shows battleships as 'X' and empty water as '.'. Every battleship is a straight line of cells, either horizontal (1 x k) or vertical (k x 1), and ships never touch each other: there is always at least one empty cell between two ships horizontally or vertically. Count the battleships. You do not need a flood fill: count only the 'X' cells that have no 'X' directly above them and no 'X' directly to their left, since each ship has exactly one such first cell.
Input
First line: R and C. Next R lines: C characters, each 'X' or '.'.
Output
Print the number of battleships.
Constraints
1 <= R, C <= 200; the board is valid
Example input
3 4 X..X ...X ...X
Example output
2
Explanation
In X..X / ...X / ...X there is a single-cell ship in the top-left corner and a vertical ship of length 3 in the last column, so there are 2 battleships.