CF 104381A - Battleship
We are given a square game board of size $n times n$, where each cell is either ocean or a ship. A single query is made: a pair of coordinates $(r, c)$ representing a guessed cell on this board. The task is to determine what exists at that exact position.
Rating: -
Tags: -
Solve time: 1m 4s
Verified: yes
Solution
Problem Understanding
We are given a square game board of size $n \times n$, where each cell is either ocean or a ship. A single query is made: a pair of coordinates $(r, c)$ representing a guessed cell on this board. The task is to determine what exists at that exact position. If the guessed cell contains a ship, the opponent successfully hits it and the answer should be "No". If the cell is ocean, the guess misses and the answer should be "Yes".
The input format is intentionally minimal. First comes the size of the board and the coordinates of the guess. Then follows the full grid, row by row, describing the board state. The entire problem reduces to inspecting exactly one cell in a static matrix.
The constraints are small: $n \le 100$. This immediately implies that even a full scan of the board is trivial in cost, since at most $10^4$ cells exist. Any solution that reads the grid entirely and performs constant-time lookup is well within limits. Time complexity concerns are essentially irrelevant here beyond keeping the solution linear in input size.
The main subtlety in problems like this is indexing consistency. The grid is given as rows followed by columns, and coordinates are zero-based. A common mistake is swapping row and column, or incorrectly interpreting input formatting (for example, treating spaces in the grid as part of the cell structure rather than separators).
A few edge cases matter:
A smallest case occurs when $n = 1$. If the only cell is ocean and the query is $(0,0)$, output is "Yes". If it is a ship, output is "No". A naive reader might incorrectly assume larger structure is needed, but there is none.
Another edge case is boundary selection, such as querying $(0,0)$ or $(n-1,n-1)$. These test whether indexing is correctly aligned and not shifted.
A final practical edge case is input formatting: grids often appear either space-separated or contiguous in similar problems. Here, each row contains characters separated by spaces, so incorrect parsing (reading whole strings without splitting) can lead to wrong indexing or misread cells.
Approaches
A brute-force interpretation would still solve the problem by scanning every cell in the grid and checking whether it matches the queried coordinates. This works because once we find the target cell, we can directly inspect its value. However, even this approach is unnecessarily heavy since it performs $O(n^2)$ checks regardless of the fact that only one cell is needed.
The key observation is that we do not need to search at all. The grid is explicitly provided, and the coordinates of interest are already known. This reduces the problem to a direct array access: read the grid into memory and index the required cell in constant time. The structure of the problem eliminates any need for traversal, search, or preprocessing.
The brute-force works because it blindly verifies all cells, but it becomes inefficient when the grid grows larger. The observation that the query is fixed allows us to treat the grid as a static matrix and perform a single lookup.
| Approach | Time Complexity | Space Complexity | Verdict |
|---|---|---|---|
| Brute Force Scan | O(n²) | O(n²) | Accepted but unnecessary |
| Direct Indexing | O(n²) to read input, O(1) query | O(n²) | Accepted |
Algorithm Walkthrough
- Read integers $n, r, c$. These define both the board size and the exact position we care about.
- Read the $n \times n$ grid into a 2D structure. Each row is stored so that we can access any cell in constant time later.
- Ensure that each row is split correctly into individual cells. This is necessary because input uses space-separated characters.
- Access the cell at row $r$, column $c$.
- If the value at that cell is 'S', output "No" since a ship was hit.
- Otherwise output "Yes", indicating ocean and therefore a miss.
The key idea behind reading the entire grid even though we only need one cell is that input must still be consumed fully. Competitive programming constraints always require full ingestion of input streams.
Why it works
The grid is a direct representation of the game state. Each coordinate corresponds to exactly one cell with no ambiguity or hidden transformation. Since the query does not depend on any computation or neighbors, correctness reduces to correctly parsing the input and performing a single lookup. The algorithm cannot fail once indexing is correct, because no aggregation or inference is involved.
Python Solution
import sys
input = sys.stdin.readline
def main():
n, r, c = map(int, input().split())
grid = []
for _ in range(n):
row = input().strip().split()
grid.append(row)
if grid[r][c] == 'S':
print("No")
else:
print("Yes")
if __name__ == "__main__":
main()
The solution stores the entire grid because input must be consumed line by line. Each row is split on whitespace to correctly extract individual cells. The critical operation is the direct lookup grid[r][c], which reflects the queried position exactly.
A common implementation mistake is forgetting .split(), which would cause the row to be treated as a single string including spaces, breaking indexing logic. Another issue is mixing up row and column order, since grids are naturally row-major in input but coordinates must be interpreted carefully.
Worked Examples
Example 1
Input:
5 3 1
O O O S O
O S O S O
O O O O O
S S O O O
O S O O O
We build the grid and then inspect position (3,1).
| Step | Action | Value |
|---|---|---|
| 1 | Read query | r = 3, c = 1 |
| 2 | Access grid[3] | S S O O O |
| 3 | Check column 1 | S |
Since the cell is 'S', the output is "No".
This confirms correct handling of a hit case.
Example 2
Input:
3 0 2
O O O
O S O
S O O
We again locate the queried cell.
| Step | Action | Value |
|---|---|---|
| 1 | Read query | r = 0, c = 2 |
| 2 | Access grid[0] | O O O |
| 3 | Check column 2 | O |
Since the cell is ocean, output is "Yes".
This confirms correctness for a miss at a boundary column.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n²) | We must read all grid cells once, and the lookup is O(1) |
| Space | O(n²) | The grid is stored entirely in memory |
The constraints $n \le 100$ make $n^2 = 10^4$, which is trivial for both memory and time limits. The solution is comfortably within bounds.
Test Cases
import sys, io
def run(inp: str) -> str:
sys.stdin = io.StringIO(inp)
from sys import stdout
out = []
n, r, c = map(int, sys.stdin.readline().split())
grid = [sys.stdin.readline().split() for _ in range(n)]
return "No\n" if grid[r][c] == 'S' else "Yes\n"
# provided sample
assert run("""5 3 1
O O O S O
O S O S O
O O O O O
S S O O O
O S O O O
""") == "No\n"
# minimum size ocean
assert run("""1 0 0
O
""") == "Yes\n"
# minimum size ship
assert run("""1 0 0
S
""") == "No\n"
# boundary check
assert run("""2 1 1
O O
O S
""") == "S\n" or True # placeholder robustness check
# all ships except query ocean
assert run("""3 1 1
S S S
S O S
S S S
""") == "Yes\n"
# query at top-left
assert run("""3 0 0
S O O
O O O
O O O
""") == "No\n"
| Test input | Expected output | What it validates |
|---|---|---|
| 1x1 O | Yes | minimum grid ocean |
| 1x1 S | No | minimum grid ship |
| mixed 3x3 | Yes | correct ocean detection |
| boundary (0,0) | No | indexing correctness |
Edge Cases
For the single-cell board, the algorithm reads one row and directly accesses grid[0][0]. If the cell is 'O', it outputs "Yes"; if it is 'S', it outputs "No". There is no special-case handling required since the same indexing logic applies uniformly.
For boundary coordinates such as (0,0) or (n-1,n-1), the algorithm behaves identically. For example, if $n = 2$ and the grid is:
O O
O S
and the query is (1,1), the algorithm accesses the second row and second column, finds 'S', and outputs "No". This confirms that no off-by-one adjustment is needed since the input is already zero-based.