Imagine the following scenario: you are playing battleships with a friend but decide to cheat. Rather than moving a ship after he shoots where your ship used to be, you decide not to place any ships at all. You tell him all his shots are misses, until it is impossible to place ships in such a way.
You have to write a function, or a full program, that somehow takes 3 arguments: the field size, a list of amounts of ship sizes, and a list of shots.
Battlefield
One of the given parameters is the board size. The battlefield is a square of cells, and the given parameter is simply one side of the square.
For example, the following is a board of size 5.
Coordinates on the field are specified as a 2-component string: a letter followed by a number. You can rely on the letters being in some particular case.
Letter specifies the column, number specifies the row of the cell (1-indexed). For example in the above picture, the highlighted cell is denoted by "D2"
.
Since there are only 26 letters, the field can't be larger than 26x26.
Ships
The ships are straight lines of 1 or more blocks. The amount of ships is specified in a list, where the first element is the amount of 1-cell ships, second - of 2-cell ships and so on.
For instance, the list [4,1,2,0,1]
would create the following shipset:
When placed on the battlefield, ships cannot intersect, or even touch each other. Not even with corners. They can however touch the edges of the field.
Below you can see an example of valid ship placement:
You can assume that for a given shipset, there always exists a placement on an empty board of given size.
Output
If such placements of ships exist, you have to output any of them.
The program has to output a newline-separated matrix of ascii characters of either of 3 types - one to denote blank cell, one - a ship piece, and one - a cell marked as "missed". No other characters should be output.
For example,
ZZ@Z
\@@Z
@\\Z
\Z\\
(In this example, I defined @
to be blank cell, \
to be a "missed" cell, and Z
to be ship piece)
If no such placement exists, the program/function should return without outputting anything.
Input
If you decide to make a fullblown program, it's up to specify you how the lists are input, some might go via arguments, some via stdin.
This is code-golf, lowest amount of characters wins.
An example non-golfed optimized solution can be found here
Compile with -std=c99
, first argument is the size of the board, the other arguments are ship sizes. A newline-separated list of shots is given on stdin. Example:
./a 4 1 1 1 <<< $'A2\nA4\nB3\nC3\nC4\D4'
26x26
? I sketched a solution based on regexps and recursion, and it gets extremely slow = unusable for fields more than6x6
. Either I do something very stupid, or lack of answers means that others have no success too. \$\endgroup\$10x10
with a4,3,2,1
shipset \$\endgroup\$