Battleships in a Board - Problem
Naval Battle Grid Scanner
You are a naval intelligence officer analyzing enemy positions on a battle grid. Given an
The grid uses the following symbols:
•
•
Battleship Rules:
1. Battleships can only be placed horizontally or vertically
2. Each battleship forms either a
3. Battleships are never adjacent - they're separated by at least one cell of water
4. A single
Your task is to return the total count of individual battleships on the board.
Example:
This represents 2 battleships: one single cell and one vertical 3-cell battleship.
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 waterBattleship Rules:
1. Battleships can only be placed horizontally or vertically
2. Each battleship forms either a
1 × k (horizontal) or k × 1 (vertical) shape3. 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 1Your 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
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
✓ Linear Growth
Space Complexity
O(1)
Only uses a few variables, no additional data structures
✓ 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?
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code