Ships
Time limit1sMemory limit128 MB
Given partial knowledge of seven non-overlapping tetromino ships on a grid, decide if all 28 ship squares can be uncovered with at most one miss against every consistent arrangement.
- Level
Hard8 of 10
- Topics
- Backtracking, Brute force, Implementation
- Solved
- No attempts yet
Problem
Two players hide ships on a grid and take turns guessing where the other player's ships are. In this version your opponent has placed exactly seven ships on a rectangular grid, one ship for each of the shapes below.
xx xx xx x x x
xx xx xx xxx xxx xxx xxxx
Every shape covers four squares, so the seven ships together cover 28 squares. A ship may be rotated, but it may not be mirrored. Every ship lies completely inside the rectangle and no two ships overlap, although a ship may touch another ship or the border.
The game is already under way and some squares have been uncovered. You are given a grid describing what you know right now. Each square holds one of three characters:
xif a ship covers the squareoif no ship covers the square.if you have not uncovered the square yet
From here you uncover . squares one at a time, and you may pick the next square after seeing the result of the previous one. Uncovering a ship square is a hit, uncovering an empty square is a miss. Decide whether you can uncover all 28 ship squares while taking at most one miss, against every arrangement that agrees with what you already know. An order that works only when you guess luckily does not count.
If your current knowledge already pins down the 28 ship squares, no miss is needed. Otherwise you may spend the single miss, and once you know its result the ship squares must be pinned down, whichever arrangement turns out to be the real one.
Every grid in the input agrees with at least one arrangement.
Input
The input holds several game situations. Each one starts with a line containing two integers and , the width and the height of the grid, with .
Each of the next lines holds a string of characters, each of them x, o or ..
Blank lines may appear between games. The input ends with a game where and , and that game is not processed.
Output
For each game print the line Game #k, where is the position of the game in the input counting from 1, then a line holding yes. if you can uncover all ship squares while taking at most one miss, or no. otherwise.
Print one empty line between consecutive games. Do not print an empty line after the last game.