Skip to content

battleship

📖 Rule Reference: Read full rules on external site

🎮 Play Online: Play at puzz.link • janko


Input Format

The battleship-style grid follows structure:

1. Header Line [ROWS] [COLS] [NUM OF SHIPS]

Note: [NUM OF SHIPS] is adaptively updated, you can specify the shape via sequance.

e.g., 5 4 0 2 1 means the number of ships with size 1,2,3,4,5 is 5,4,0,2,1 respectively. The largest ship is not limited, which means you can use: 5 8 8 9 0 0 3 0 5 6 2 sequcece to represent your fleet where the longest ship is 11.

2. Clue Lines (Next 4 lines)

Space-separated characters representing hints (cols and rows):

  • Line 2: Top column numbers.
  • Line 3: Left row numbers.

3. Clue Grid (Next [ROWS] lines)

Represents the numbers/clues given in the problem.

  • -: Empty cell (no number or ship).
  • x: forbidden water, no ships filled.
  • n: a ship pointing "North" (up).
  • s: a ship pointing "South" (Down).
  • w: a ship pointing "West" (Left).
  • e: a ship pointing "East" (Right).
  • o: a ship with length 1.
  • m: a middle segment of ship (with length > 1).

Output Format

Same format as input desc, only to omit the row/col hints.

Examples

Python Quick Start

Use the following code to solve this puzzle directly:

import puzzlekit

# Raw input data
problem_str = """
11 11 5 4 3 2 1
2 5 3 2 4 4 2 4 2 1 6
2 6 1 7 0 1 8 1 5 0 4
- - - - - - - x - - -
w - - x - - - - - - -
- - - - - - - - - - x
- - - - - x - x - - -
- - - - - - - - - - -
- - - - - - - - - - -
- - - - x - - - - - -
- - - - - - - - - - s
- - - - - - - - - - -
- - - - - - - - - - -
- - - - - - - - - - -
"""

# Solve
res = puzzlekit.solve(problem_str, puzzle_type="battleship")

# Print solution grid
print(res.solution_data.get('solution_grid', []))

# Visualize (optional)
res.show()

Solution Output

11 11 5 4 3 2 1
- - - - - - - - o - n
w m e - w e - - - - s
- - - - - - - o - - -
w m m m e - - - - w e
- - - - - - - - - - -
- - - - - - - - - - n
- w m e - w m m e - m
- - - - - - - - - - s
- o - - w m m e - - -
- - - - - - - - - - -
- o - - w e - o - - -