Battleships in a Board - Problem
Naval Battle Grid Scanner

You are a naval intelligence officer analyzing enemy positions on a battle grid. Given an m x n matrix representing the ocean battlefield, your mission is to count the total number of enemy battleships deployed.

The grid uses the following symbols:
'X' - Part of a battleship
'.' - Empty water

Battleship Rules:
1. Battleships can only be placed horizontally or vertically
2. Each battleship forms either a 1 × k (horizontal) or k × 1 (vertical) shape
3. Battleships are never adjacent - they're separated by at least one cell of water
4. A single 'X' cell also counts as a battleship of size 1

Your task is to return the total count of individual battleships on the board.

Example:
[["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
This represents 2 battleships: one single cell and one vertical 3-cell battleship.

Input & Output

example_1.py — Multiple Battleships
$ Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2
💡 Note: The first battleship is a single cell at (0,0). The second battleship is a vertical 3-cell ship occupying (0,3), (1,3), and (2,3). Total count: 2 battleships.
example_2.py — Single Cell Battleships
$ Input: board = [["X"]]
Output: 1
💡 Note: A single 'X' cell represents one battleship of size 1x1.
example_3.py — Horizontal and Vertical Ships
$ Input: board = [["X","X",".","X"],[".",".",".","X"],[".",".",".","."]]
Output: 2
💡 Note: One horizontal 2-cell battleship at (0,0)-(0,1) and one vertical 2-cell battleship at (0,3)-(1,3). The empty space ensures proper separation.

Visualization

Tap to expand
XXXX12RADAR CONTROL PANEL● Ship Origins Detected: 2▫ Single-cell battleship at (0,0)▫ Multi-cell battleship at (0,3)⚡ Algorithm: Top-Left Detection✓ O(1) space complexity✓ Single pass required
Understanding the Visualization
1
Radar Sweep
Scan the ocean grid systematically from top-left to bottom-right
2
Ship Detection
When radar detects a ship part ('X'), check if it's the ship's origin point
3
Origin Verification
A point is an origin if no ship parts exist above it or to its left
4
Count Ships
Each origin point represents exactly one distinct battleship
Key Takeaway
🎯 Key Insight: Every battleship has exactly one top-left corner. By counting only these origin points, we achieve optimal efficiency while maintaining perfect accuracy in our naval reconnaissance mission!

Time & Space Complexity

Time Complexity
⏱️
O(m×n)

Single pass through all cells in the matrix

n
2n
Linear Growth
Space Complexity
O(1)

Only uses a few variables, no additional data structures

n
2n
Linear Space

Constraints

  • m == board.length
  • n == board[i].length
  • 1 ≤ m, n ≤ 200
  • board[i][j] is either '.' or 'X'
  • Follow up: Could you do it in one-pass, using only O(1) extra memory and without modifying the values board?
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 15
89.5K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen