grid where 'X' is a ship cell and '.' is water. Count the ships on the board.
Every ship is a straight strip of cells: either one row and several consecutive columns, or one column and several consecutive rows. Two different ships never share a side (they may touch at a corner).
Try to do it in a single pass, with O(1) extra memory and without modifying grid.grid=[['X', 'X', '.', 'X'], ['.', '.', '.', 'X'], ['X', '.', '.', '.']] → 3
A horizontal two-cell ship in the top-left corner, a vertical two-cell ship on the right, and a single-cell ship at the bottom left: 3 in total.
grid=[['.', 'X'], ['X', '.']] → 2
The cells touch only at a corner, so they are two separate ships.
grid=[['.', '.', '.']] → 0
There are no ships.
grid[i][j] is 'X' or '.''X' cells is a straight stripgrid=[['X', 'X', '.', 'X'], ['.', '.', '.', 'X'], ['X', '.', '.', '.']]
3
Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.