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.


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.


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.


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,


(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.


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 , 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'

  • \$\begingroup\$ 26x26? I sketched a solution based on regexps and recursion, and it gets extremely slow = unusable for fields more than 6x6. Either I do something very stupid, or lack of answers means that others have no success too. \$\endgroup\$ Commented Mar 6, 2014 at 10:11
  • \$\begingroup\$ I just wrote an implementation in C, it computes instantly for at least 10x10 with a 4,3,2,1 shipset \$\endgroup\$
    – mniip
    Commented Mar 6, 2014 at 14:10
  • \$\begingroup\$ @mniip: Do you have any specific way of dealing with hard cases (big board, many ships, failing due to position of shots)? Or is it just (a bit smart) brute force? \$\endgroup\$ Commented Mar 6, 2014 at 19:56
  • \$\begingroup\$ It has a few optimizations, f.e tries to place small ships first, and excludes swapping ships of equal size from the brute force. It's kinda slow when there are lots of ships on a small and almost empty board. \$\endgroup\$
    – mniip
    Commented Mar 7, 2014 at 7:37
  • \$\begingroup\$ @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ I've added an example solution link. \$\endgroup\$
    – mniip
    Commented Mar 7, 2014 at 7:53

8 Answers 8


SWI-Prolog, 536 4411 bytes

1 UNIX line ending, final new line not counted

The version with all optimizations removed (441 bytes):

e([H|R],I,O):-J#=I+1,e(R,J,P),l(H,Q),Q ins I,append(P,Q,O).

Since the code is changed to minimize the number of bytes, it will no longer accept a list of shots that have duplicates.

The version with basic optimization, fully golfed (536 → 506 bytes)

e([H|R],I,O):-J#=I+1,e(R,J,P),l(H,Q),Q ins I,append(P,Q,O).

I implement some basic checks (count number of ship blocks necessary) to make the code exits faster for clearly impossible cases. The code is also immune to duplicate in the list of shots so far.

Below is the (somewhat) readable version, with detailed comments:


% Shorthand for maplist, which works like map in high order function

% Creating a square matrix A which is L x L

% Shorthand for length, with order of parameters reversed

% Unification A[I] = E

% Unification A[X][Y]=E

% Mark positions that have been shot

% Place all ships on the board

% Place a length S ship horizontal/vertical forward on the board
    l(L,A), % Get length
    X#>0,X#=<L,Y#>0,Y#=<L, % Constraint X and Y coordinates
    transpose(A,T), % Transpose to work on columns
    (placeship_h(S,(X,Y),A) ; placeship_h(S,(Y,X),T)).

% Place ship horizontal, forward at (X,Y)
    idx2(A,E,(X,Y)),var(E),E=s, % Make sure position is unassigned, then mark
    L2#=L-1,Y2#=Y+1, % Do this for all blocks of the ship

% Expand the list of ships
% e.g. [2,3,1] --> [3,2,2,2,1,1]

    I2#=I+1,shipexpand(R,I2,O2), % process the rest
    l(H,O1),O1 ins I, % duplicate current ship size H times
    append(O2,O1,O). % larger ship size goes in front

% Print the result

    board(L,A), % create board
    sort(S,SS), % remove duplicates
    markshot(A,SS), % mark positions that have been shot
    shipexpand(H,HH),!, % make a list of ships
    l(SC,SS),sumlist(HH,BC),L*L-SC>=BC, % check to speed-up, can be removed
    placeships(HH,A),!, % place ships
    pboard(A). % print result

Query format:

game(Board_Size, Ships_List, Shots_List).

Sample query (using the sample in the question):

?- game(4,[1,1,1],[(2,1),(3,2),(3,3),(4,1),(4,3),(4,4)]).

?- game(4,[2,2,0,1],[(2,1),(3,2),(3,3),(4,1),(4,3),(4,4)]).
  • \$\begingroup\$ Ah darn beat me by a few dozen characters! Not sure if I can compress mine any more but I'll keep trying... nice use of Prolog! \$\endgroup\$
    – Claudiu
    Commented Mar 6, 2014 at 19:48
  • \$\begingroup\$ @Claudiu: My solution still have a "buffer" of 20 or so characters. I left those checking code in there for performance reason, but they can be removed without affecting the correctness ;) I don't bother if other answers get below 500, though. \$\endgroup\$ Commented Mar 6, 2014 at 19:52

Matlab - 536 chars

Updated: Much smaller output formatting, some loop whitespace removed.

Updated: Added golfed version

Updated: Added commented version, reduced golfed version by ~100 chars

% Battleship puzzle solver.
% n: size of the map (ex. 4 -> 4x4)
% sh: list of shots (ex. ['A2';'C4'])
% sp: ships of each size (ex. [2,0,1] -> 2x1 and 1x3)
function bs(n,sh,sp)

  % sp holds a vector of ship counts, where the index of each element is
  % the size of the ship. s will hold a vector of ship sizes, with order
  % not mattering. This is easier to work with using recursion, because
  % we can remove elements with Matlab's array subselection syntax, rather
  % than decrement elements and check if they're zero.
  % Tricks:
  %   Since sp never contains a -1, find(1+sp) is the same as 1:length(sp)
  %   but is three characters shorter.
  for i=find(1+sp)

  % m will hold the map. It will be +2 in each direction, so that later we
  % can find neighbors of edge-spaces without checking bounds. For now,
  % each element is either '0' or '1' for a space or missed shot,
  % respectively. We convert the shots as provided by the user (ex. 'A2')
  % to marks on the map.
  % Tricks:
  %   It takes one shorter character to subtract 47 than 'A' to determine
  %   the indices into the map.
  for h=sh'

  % Solve the puzzle. q will either be the empty array or a solution map.

  % If a solution was found, output it, showing the original shots and the
  % ship placement. If no solution was found, print a sad face.
  % Tricks:
  %   q is 0 on failure, which is treated like 'false'. q is a matrix on
  %   success which is NOT treated like 'true', so we have to check for
  %   failure first, then handle success in the 'else' block.
  %   q contains the "fake shots" that surround each placed ship (see the
  %   pl function). We don't want to output those, so we just copy the ship
  %   markings into the original map.
  if ~q ':('

% Depth-first search for a solution.
% m: map (see main code above)
% s: vector of ship sizes to place in the map
% Returns q: square matrix of integers, updated with all requested ship
% sizes, or 0 if no solution is possible.
function q = pp(m,s)

  % If we have no ships to process, we're all done recursing and the
  % current map is a valid solution. Rather than handle this in an 'else'
  % block, we can assume it's the case and overwrite q if not, saving 4
  % characters.

  % If we have any ships to place...
  % Tricks:
  %   Since we are only interested in positive values in s, we can use
  %   sum(s) in place of isempty(s), saving 4 characters.
  if sum(s)

    % Try to place the first ship in the list into the map, both vertically
    % (first call to p) and vertically (second call to p). We can process
    % any ship in the list, but the first can be removed from the list
    % with the fewest characters. r will hold a cell-array of options for
    % this ship.

    % Recurse for each option until we find a solution.
    % Tricks:
    %   Start with q, our return variable, set to 0 (indicating no solution
    %   was found. On each loop we'll only bother to recurse if q is still
    %   0. This relieves the need for if/else to check whether to continue
    %   the loop, or any final q=0 if the loop exits without success.
    %   Sadly, there's no way around the length(r) call. Matlab doesn't
    %   provide syntax for iterating over cell-arrays.
    for i=1:length(r)
      if ~q q=pp(r{i},s(2:end));end

% Place a single ship into a map.
% m: map (see main code above)
% s: ship size to place
% t: if the map has been transposed prior to this call
% Returns r: cell-array of possible maps including this ship
function r=p(m,s,t)
  % Start with an empty cell-array and pre-compute the size of the map,
  % which we'll need to use a few times.

  % For each row (omitting the first and last 'buffer' rows)...
  for i=2:z(2)-1

  % For each starting column in this row where enough consecutive 0s
  % appear to fit this ship...
  for j=strfind(m(i,2:end-1),(1:s)*0)

    % Copy the map so we can modify it without overwriting the variable
    % for the next loop.

    % For each location on the map that is part of this optional
    % placement...
    for l=0:s-1
      % Let's leave this is an exercise for the reader ;)
      n([(l+j)*z(1)+i,z(1),1]*[ones(1,9);kron(v,[1 1 1]);[v v v]])=1;

    % Mark each location that is part of this optional placement with
    % a '2'.

    % Since our results are going into a cell-array it won't be
    % convenient for the caller to undo any transpositions they might
    % have done. If the t flag is set, transpose this map back before
    % adding it to the cell-array.
    if t n=n';end

Here is the golfed version.

function bs(n,sh,sp)
s=[];for i=find(1+sp)
m=zeros(n+2);for h=sh'
q=pp(m,s);if ~q ':('
function q = pp(m,s)
q=m;if sum(s)
r=[p(m,s(1),0),p(m',s(1),1)];q=0;for i=1:length(r)if ~q q=pp(r{i},s(2:end));end
function r=p(m,s,t)
r={};z=size(m);for i=2:z(2)-1
for j=strfind(m(i,2:end-1),(1:s)*0)n=m;for l=0:s-1
v=-1:1;n([(l+j)*z(1)+i,z(1),1]*[ones(1,9);kron(v,[1 1 1]);[v v v]])=1;end
n(i,1+j:j+s)=2;if t n=n';end

Here's some samples.



>> bs(4,['A1';'B4';'C2'],[3,1])

>> bs(4,['A1';'B4';'C2'],[3,2])

The big line with 'kron' (near the bottom of the un-golfed code) is my favorite part of this. It writes a '1' to all locations in the map that neighbor a given position. Can you see how it works? It uses the kronecker tensor product, matrix multiplication, and indexes the map as linear array...


Python, 464 chars

M=sum(1<<int(m[1:])*C-C+ord(m[0])-65for m in M)
def P(L,H,S):
 if len(L)==0:
  for y in R:print''.join('.#!'[(S>>y*C+x&1)+2*(M>>y*C+x&1)]for x in R)
  return 1
 for s in[2**L[0]-1,sum(1<<C*j for j in range(L[0]))]:
  for i in range(C*C):
   if H&s==s and P(L[1:],H&~(s|s<<1|s>>1|s<<B|s>>B|s<<C|s>>C|s<<C+1|s>>C+1),S|s):return 1
 return 0
P(sum(([l+1]*k for l,k in enumerate(L)),[])[::-1],sum((2**B-1)<<B*j+j for j in R)&~M,0)

Input (stdin):

7, [4,1,2,0,1], ['A1','B2','C3']



Works using integers holding bitmaps of various features:

M = bitmap of misses
H = bitmap of holes where ships can still go
S = bitmap of ships already placed
L = list of ship sizes not yet placed
B = dimension of board
C = bitmap row length
  • \$\begingroup\$ If you don't mind, do you do any optimization, or is it just straight brute force? \$\endgroup\$ Commented Mar 7, 2014 at 9:33
  • \$\begingroup\$ There's one optimization: the [::-1] which makes it try the longest ship first. It also backtracks as soon as it finds a ship it can't place. \$\endgroup\$ Commented Mar 7, 2014 at 17:46
  • \$\begingroup\$ You can use a single tab instead of 2 or 3 spaces on lines 7, 8, 11, and 12, reducing the byte count to 458. See here. \$\endgroup\$
    – mbomb007
    Commented Jun 12, 2015 at 21:29

Python, 562 characters, -8 with ugly output, +4? for bash invocation

import sys;a=sys.argv
S=I(a[1]);f=[[0]*(S+2)for _ in R(S+2)]
for i,n in enumerate(C):X=[i+1]*I(n)+X
for q in Q:f[I(q[1:])][ord(q[0])-64]=1
V=lambda r,c:(all(f[r+Q][c+W]<2for Q in D for W in D),0,0)[f[r][c]]
def F(X):
 if not X:print"\n".join(" ".join(" .@"[c]for c in r[1:-1])for r in f[1:-1])
 for r in A:
    for c in A:
     for h in(0,1):
        def P(m):
         for i in R(x):f[(r+i,r)[h]][(c,c+i)[h]]=m
        if(r+x,c+x)[h]<S+2and all(V((r+i,r)[h],(c,c+i)[h])for i in R(x)):P(2);F(X[1:]);P(0)

Note: the indent levels are space, tab, tab + space, tab + tab, and tab + tab + space. This saves a few characters from using spaces only.

Usage and example:

Takes input from command line arguments. Outputs a blank as a space, a shot as a ., and a @ as part of a ship:

$ python bships_golf.py "7" "4 0 2 0 1" \
         "A1 C3 B5 E4 G6 G7 A3 A4 A5 A6 C1 C3 C5 C7 B6 B7 D1 D2 G3" 2>X
. @ . . @ @ @
  @   .
. @ . @   @ .
.     @ .
. . . @   @
. .   @     .
@ . . @   @ .

When unsolvable, prints nothing:

$ python bships_golf.py "3" "2" "A1 A3 B1 C1 C3" 2>X
. . .
@   @
.   .
$ python bships_golf.py "3" "2" "A1 A2 A3 B1 C1 C3" 2>X

The 2>X is to suppress an error message since the program exits by throwing an exception. Feel free to add a +4 penalty if deemed fair. Otherwise I'd have to do a try: ... except:0 to suppress it, which would take more characters anyway.

I can also print the output as numbers (0, 1, and 2) to shave off 8 characters, but I value aesthetics.


I represent the board as a list of lists of integers with size 2 greater than the input, to avoid having to do bounds checking. 0 represents an empty space, 1 a shot, and 2 a ship. I run through the shots list Q and mark all the shots. I convert the list of ships to an explicit list X of ships, e.g. [4, 0, 2, 0, 1] becomes [5, 3, 3, 1, 1, 1, 1]. Then it's a simple backtracking algorithm: in order of descending size order, try to place a ship, and then try to place the rest of the ships. If it doesn't work, try the next slot. As soon as it succeeds, the ship list X is null, and accessing X[0] throws an exception which quits the program. The rest is just heavy golfing (was initially 1615 characters).


GolfScript, 236 characters

n%([~](:N):M;[.,,]zip{~[)]*~}%-1%:S;{(65-\~(M*+}%:H;{{M*+}+N,/}N,%H-S[]]]{(~\.{(:L;{{M*+{+}+L,%}+N)L-,/}N,%{.{.M/\M%M*+}%}%{3$&,L=},{:K[{..M+\M-}%{..)\(}%3$\- 1$3$K+]}%\;\;\;\+.}{;:R;;;0}if}do{{{M*+.H?)'!'*\R?)'#'*'.'++1<}+N,/n}N,%}R!!*

The input is given on STDIN. First line contains size and number of ships, each following line coordinates of a single shot.


4 1 1 1


I thought that also this challenge should have at least one GolfScript answer. In the end it became very ungolfscriptish due to the used algorithm which favours performance over shortness.

Annotated code:

n%               # Split the input into lines
([~]             # The first line is evaluated to an array [N S1 S2 S3 ...]
(:N              # This happy smiley removes the first item and assigns it to variable N
):M;             # While this sad smiley increases N by one and assigns it to M

[.,,]zip         # For the rest of numbers in the first line create the array [[0 S1] [1 S2] [2 S3] ...]
{~[)]*~}%        # Each element [i Si] is converted into the list (i+1 i+1 ... i+1) with Si items. 
-1%:S;           # Reverse (i.e. largest ship first) and assign the list to variable S.
                 # The result is a list of ship lengths, e.g. for input 3 0 2 we have S = [3 3 1 1 1].
{                # On the stack remains a list of coordinates
  (65-           # Convert the letter from A,B,... into numeric value 0,1,...
  \~(            # The number value is decreased by one
  M*+            # Both are combined to a single index (M*row+col)
}%:H;            # The list of shots is then assigned to variable H

                 # The algorithm is recursive backtracking implemented using a stack of tuples [A S R] where
                 #   - A is the list of open squares
                 #   - S is a list of ships to be placed
                 #   - R is the list of positions where ships were placed                     

    {{           # initial A is the full space of possible coordinates
      M*+        #   combine row and column values into a single index
    }+N,/}N,%    # do the N*N loop
    H-           # minus all places where a shot was done already
    S            # initial S is the original list
    []           # initial R is the empty list (no ships placed yet)
]                # The starting point is pushed on the stack 

{                # Start of the loop running on the stack
  (~\            # Pop from the stack and extract items in order A R S

  .{             #   If S is non-empty

    (:L;         #     Take first item in S (longest ship) and asign its length to L

                 #     This lengthy expression just calculates all possible ship placements on a single board
                 #     (could be shortened by 3 chars if we don't respect board size but I find it clearer this way)

    {3$&,L=},    #     This step is just a filter on those placements. The overlap (&) with the list of open squares (3$) 
                 #     must be of size L, i.e. all coordinates must be free

                 #     Now we have possible placements. For each one generate the appropriate tuple (A* S* R*) for recursion
      :K         #     Assign the possible new ship placement to temporary variable K
                 #       For each coordinate add the square one row above and below (first loop)
                 #       and afterwards for the resulting list also all squares left and right (second loop)
        3$\-     #       Remove all these squares from the list of available squares A in order to get the new A*
        1$       #       Reduced list of ships S* (note that the first item of S was already remove above)
        3$K+     #       Last item in tuple is R* = R + K, i.e. the ship's placements are added to the result

    \;\;\;       #     Discard the original values A R S
    \+           #     Push the newly generated tuples on the stack
    .            #     Loop until the stack is empty

  }{             #   else

    ;:R;;;       #     Assign the solution to the variable R and remove all the rest from the stack. 
    0            #     Push a zero in order to break the loop

  }if            #   End if

}do              # End of the loop

{                # The output block starts here
    .H?)         #   Is the current square in the list of shots?
    '!'*         #     then take a number of '!' otherwise an empty string
    \R?)         #   Is the current square in the list of ships?
    '#'*         #     then take a number of '#' otherwise an empty string
    '.'++        #   Join both strings and append a '.'
    1<           #   Take the first item of the resulting string, i.e. altogether this is a simple if-else-structure
  }+N,/n}N,%     # Do a N*N loop
R!!*             # Run this block only if R was assigned something during the backtracking. 
                 # (!! is double-negation which converts any non-zero R into a one)
                 # Note: since the empty list from the algorithm is still on the stack if R wasn't assigned
                 # anything the operator !! works on the code block (which yields 1) which is then multiplied
                 # with the empty list.

Perl, 455 447 437 436 422 418

$n=($N=<>+0)+1;@S=map{(++$-)x$_}<>=~/\d+/g;$_=<>;$f=('~'x$N.$/)x$N;substr($f,$n*$1-$n-65+ord$&,1)=x while/\w(\d+)/g;sub f{for my$i(0..$N*$n-1){for(0..@_-2){my($f,@b)=@_;$m=($s=splice@b,$_,1)-1;pos=pos($u=$_=$f)=$i;for(s/\G(~.{$N}){$m}~/$&&("\0"."\377"x$N)x$s|(o."\0"x$N)x$m.o/se?$_:(),$u=~s/\G~{$s}/o x$s/se?$u:()){for$k(0,$N-1..$n){s/(?<=o.{$k})~|~(?=.{$k}o)/!/sg}return$:if$:=@b?f($_,@b):$_}}}}$_=f$f,@S;y/!/~/;print


substr($f,$n*$1-$n-65+ord$&,1)=x while/\w(\d+)/g;
sub f{
    for my$i(0..$N*$n-1){
              $u=~s/\G~{$s}/o x$s/se?$u:()){

I expect it can be golfed further (e.g. with eval<> for pre-formatted input, as I see it's OK (?)), and also some other things (50 $ sigils? no, they'll stay).

Speed, as I said earlier, can be an issue (from instantaneous (see example below) to very-very long), depending on where the solution is on recursion tree, but let it be the question of obsolescence of hardware used. I'll do optimized version later, with recursion gone and 2-3 other obvious tricks.

It's run like this, 3 lines being fed through STDIN:

$ perl bf.pl
4 1 2 0 1
A1 B2 C3

~ is the ocean (artistic solution, isn't it), o's and xs are ships and shots. First 5 lines get the input and prepare our 'battlefield'. $N is size, @S is unrolled array of ships (e.g. 1 1 1 1 2 3 3 5 as above), $f is a string representing battlefield (rows with newlines concatenated). Next is recursive subroutine, which expects current battlefield state and list of remaining ships. It checks left to right, top to bottom and tries to place each ship in each position, both horizontally and vertically (see? Ripe to optimize, but that'll be later). Horizontal ship is obvious regexp replacement, vertical a bit trickier -- bitwise string manipulation to replace in 'column'. On success (H, V or both), new inaccessible positions are marked with !, and it forks to next iteration. And so on.

Edit: OK, here's 594 bytes (when un-indented) version that actually tries to be useful (i.e. fast) - optimized to the best of my ability while still implementing the same techniques - recursion (albeit done 'manually') and regexps. It maintains a 'stack' - @A - array of states worth to investigate. A 'state' is 4 variables: current battlefield string, index from where to start trying to place ships, reference to array of remaining ships, and index of next ship to try. Initially there's a single 'state' - start of empty string, all ships. On match (H or V, see above), current state is pushed to investigate later, updated state (with a ship placed and inaccessible positions marked) is pushed and block is restarted. When end of battlefield string is reached without success, next available state from @A is investigated (if any).

Other optimizations are: not restarting from the very beginning of the string, trying to place large ships first, not checking ship of same size if previous one failed in the same position, + maybe something else (like no $& variable etc.).

$S=[reverse map{(++$-)x$_}<>=~/\d+/g];
substr($f,$N*$2-$N+$2-66+ord$1,1)=x while/(\w)(\d+)/g;
    last if!@$S;
            next if$j and$$S[$j]==$$S[$j-1];
            push@m,$_ if s/\G(?:~.{$N}){$n}~/
            push@m,$_ if s/\G~{$s}/o x$s/se;
                redo O
        redo if++$i-length$f
    redo if@A


perl bf+.pl
5 4 3 2 1
A1 B2 C3 A10 B9 C10 J1 I2 H3 I9 J10 A5 C5 E5 F6 G7

OTOH, it still takes forever for impossible case 6 5 4 3 2 1 in example above. Maybe practical version should exit immediately if total ship tonnage exceeds battlefield capacity.


C# solution

  public static class ShipSolution {
    private static int[][] cloneField(int[][] field) {

      int[][] place = new int[field.Length][];

      for (int i = 0; i < field.Length; ++i) {
        place[i] = new int[field.Length];

        for (int j = 0; j < field.Length; ++j)
          place[i][j] = field[i][j];

      return place;


    private static void copyField(int[][] source, int[][] target) {
      for (int i = 0; i < source.Length; ++i)
        for (int j = 0; j < source.Length; ++j)
          target[i][j] = source[i][j];

    // Check if placement a legal one
    private static Boolean isPlacement(int[][] field, int x, int y, int length, Boolean isHorizontal) {
      if (x < 0)
        return false;
      else if (y < 0)
        return false;
      else if (x >= field.Length)
        return false;
      else if (y >= field.Length)
        return false;

      if (isHorizontal) {
        if ((x + length - 1) >= field.Length)
          return false;

        for (int i = 0; i < length; ++i)
          if (field[x + i][y] != 0)
            return false;
      else {
        if ((y + length - 1) >= field.Length)
          return false;

        for (int i = 0; i < length; ++i)
          if (field[x][y + i] != 0)
            return false;

      return true;

    //  When ship (legally) placed it should be marked at the field
    //  2 - ship itself
    //  3 - blocked area where no other ship could be placed
    private static void markPlacement(int[][] field, int x, int y, int length, Boolean isHorizontal) {
      if (isHorizontal) {
        for (int i = 0; i < length; ++i)
          field[x + i][y] = 2;

        for (int i = x - 1; i < x + length + 1; ++i) {
          if ((i < 0) || (i >= field.Length))

          for (int j = y - 1; j <= y + 1; ++j)
            if ((j >= 0) && (j < field.Length))
              if (field[i][j] == 0)
                field[i][j] = 3;
      else {
        for (int i = 0; i < length; ++i)
          field[x][y + i] = 2;

        for (int i = x - 1; i <= x + 1; ++i) {
          if ((i < 0) || (i >= field.Length))

          for (int j = y - 1; j < y + length + 1; ++j)
            if ((j >= 0) && (j < field.Length))
              if (field[i][j] == 0)
                field[i][j] = 3;

    // Ship placement itteration
    private static Boolean placeShips(int[][] field, int[] ships) {
      int[] vessels = new int[ships.Length];

      for (int i = 0; i < ships.Length; ++i)
        vessels[i] = ships[i];
      for (int i = ships.Length - 1; i >= 0; --i) {
        if (ships[i] <= 0)

        int length = i + 1;

        vessels[i] = vessels[i] - 1;

        // Possible placements
        for (int x = 0; x < field.Length; ++x)
          for (int y = 0; y < field.Length; ++y) {
            if (isPlacement(field, x, y, length, true)) {
              int[][] newField = cloneField(field);

              // Mark
              markPlacement(newField, x, y, length, true);

              if (placeShips(newField, vessels)) {
                copyField(newField, field);

                return true;

            if (length > 1)
              if (isPlacement(field, x, y, length, false)) {
                int[][] newField = cloneField(field);

                // Mark
                markPlacement(newField, x, y, length, false);

                if (placeShips(newField, vessels)) {
                  copyField(newField, field);

                  return true;

        return false; // <- the ship can't be placed legally

      return true; //    <- there're no more ships to be placed

    /// <summary>
    /// Solve ship puzzle
    /// </summary>
    /// <param name="size">size of the board</param>
    /// <param name="ships">ships to be placed</param>
    /// <param name="misses">misses in (line, column) format; both line and column are zero-based</param>
    /// <returns>Empty string if there is no solution; otherwise possible ship placement where
    ///   . - Blank place
    ///   * - "miss"
    ///   X - Ship
    /// </returns>
    public static String Solve(int size, int[] ships, String[] misses) {
      int[][] field = new int[size][];

      for (int i = 0; i < size; ++i)
        field[i] = new int[size];

      if (!Object.ReferenceEquals(null, misses))
        foreach (String item in misses) {
          String miss = item.Trim().ToUpperInvariant();

          int x = int.Parse(miss.Substring(1)) - 1;
          int y = miss[0] - 'A';

          field[x][y] = 1;

      if (!placeShips(field, ships))
        return "";

      StringBuilder Sb = new StringBuilder();

      foreach (int[] line in field) {
        if (Sb.Length > 0)

        foreach (int item in line) {
          if (item == 1)
          else if (item == 2)

      return Sb.ToString();


  int size = 4;
  int[] ships = new int[] { 1, 1, 1 };
  String[] misses = new String[] {"C1", "C2", "B2", "A3", "A1", "B3", "D1"};

  // *X**
  // .**X
  // **.X
  // XX.X
  Console.Out.Write(ShipSolution.Solve(size, ships, misses));
  • \$\begingroup\$ While this is a great and fast solution, it doesn't seem to handle unsolvable tasks correctly. For example, size=1 ships={1} moves={"A1"}. \$\endgroup\$
    – mniip
    Commented Mar 6, 2014 at 14:29
  • \$\begingroup\$ I'm sorry, I've missed the condition when the next ship can't be placed legally. I've edited the solution. \$\endgroup\$ Commented Mar 6, 2014 at 14:43
  • 6
    \$\begingroup\$ Because the question is a code-golf, please try to keep your character count as low as possible (by removing whitespace for example), and include the character count in your code. \$\endgroup\$
    – ProgramFOX
    Commented Mar 6, 2014 at 14:50
  • \$\begingroup\$ Character count as it stands now is 5399. \$\endgroup\$
    – intx13
    Commented Mar 6, 2014 at 17:03

Java, 1178

Yeah much too long ._.

import java.util.*;class S{public static void main(String[]a){new S();}int a;int[][]f;List<L>l;Stack<Integer>b;S(){Scanner s=new Scanner(System.in);a=s.nextInt();f=new int[a][a];for(int[]x:f)Arrays.fill(x,95);s.next(";");b=new Stack();for(int i=1;s.hasNextInt();i++){b.addAll(Collections.nCopies(s.nextInt(),i));}s.next(";");while(s.findInLine("([A-Z])")!=null)f[s.match().group(1).charAt(0)-65][s.nextInt()-1]=79;l=new ArrayList();for(int i=0;i<a;i++){l.add(new L(i){int g(int c){return g(c,i);}void s(int c,int v){f[c][i]=v;}});l.add(new L(i){int g(int r){return g(i,r);}void s(int r,int v){f[i][r]=v;}});}if(s()){String o="";for(int r=0;r<a;r++){for(int c=0;c<a;c++)o+=(char)f[c][r];o+='\n';}System.out.print(o);}}boolean s(){if(b.empty())return true;int s=b.pop();Integer n=95;for(L c:l){int f=0;for(int x=0;x<a;x++){if(n.equals(c.g(x)))f++;else f=0;if(f>=s){for(int i=0;i<s;i++)c.s(x-i,35);if(s())return true;for(int i=0;i<s;i++)c.s(x-i,n);}}}b.push(s);return false;}class L{int i;L(int v){i=v;}void s(int i,int v){}int g(int i){return 0;}int g(int c,int r){int v=0;for(int x=-1;x<2;x++)for(int y=-1;y<2;y++)try{v|=f[c+x][r+y];}catch(Exception e){}return v&(f[c][r]|32);}}}


import java.util.*;

class S {
    public static void main(String[] a) {
        new S();

     * Number of rows/columns
    int a;

     * A reference to the full field
    int[][] f;

     * List of Ls representing all columns/rows
    List<L> l;

     * Stack of all unplaced ships, represented by their length as Integer
    Stack<Integer> b;

    S() {
        // Read input from STDIN:
        Scanner s = new Scanner(System.in);
        a = s.nextInt();
        f = new int[a][a];
        // initialize field to all '_'
        for(int[] x: f)
            Arrays.fill(x, 95);
        // ; as separator
        b = new Stack();
        // Several int to represent ships
        for (int i = 1; s.hasNextInt(); i++) {
            // nCopies for easier handling (empty Stack => everything placed)
            b.addAll(Collections.nCopies(s.nextInt(), i));
        // ; as separator
        // find an uppercase letter on this line
        while (s.findInLine("([A-Z])") != null) {
            // s.match.group(1) contains the matched letter
            // s.nextInt() to get the number following the letter
            f[s.match().group(1).charAt(0) - 65][s.nextInt() - 1] = 79;
        // Loop is left when line ends or no uppercase letter is following the current position

        // Now create a List of Lists representing single columns and rows of our field
        l = new ArrayList();
        for (int i = 0; i < a; i++) {
            l.add(new L(i) {
                int g(int c) {
                    return g(c, i);

                void s(int c, int v) {
                    f[c][i] = v;
            l.add(new L(i) {
                int g(int r) {
                    return g(i, r);

                void s(int r, int v) {
                    f[i][r] = v;
        // try to place all ships
        if (s()) {
            // print solution
            String o = "";
            for (int r = 0; r < a; r++) {
                for (int c = 0; c < a; c++) {
                    o += (char) f[c][r];
                o += '\n';

     * Try to place all ships
     * @return {@code true}, if a solution is found
    boolean s() {
        if (b.empty()) {
            // no more ships
            return true;
        // take a ship from the stack
        int s = b.pop();
        // 95 is the Ascii-code of _
        Integer n = 95;
        // go over all columns and rows
        for (L c : l) {
            // f counts the number of consecutive free fields
            int f = 0;
            // move through this column/row
            for (int x = 0; x < a; x++) {
                // Is current field free?
                if (n.equals(c.g(x))) {
                } else {
                    f = 0;
                // Did we encounter enough free fields to place our ship?
                if (f >= s) {
                    // enter ship
                    for (int i = 0; i < s; i++) {
                        c.s(x - i, 35);
                    // try to place remaining ships
                    if (s()) {
                        return true;
                    // placing remaining ships has failed ; remove ship
                    for (int i = 0; i < s; i++) {
                        c.s(x - i, n);
        // we have found no place for our ship so lets push it back
        return false;

     * List representing a single row or column of the field.
     * "Abstract"
    class L {
         * Index of row/column. Stored here as loop-variables can not be final. Used only {@link #g(int)} and {@link #s(int, int)}
        int i;

        L(int v) {
            i = v;

         * Set char representing the state at the i-th position in this row/column.
         * "Abstract"
        void s(int i, int v) {

         * Get char representing the state at the i-th position in this row/column.
         * "Abstract"
         * @return {@code '_'} if this and all adjacent field contain no {@code '#'}
        int g(int i) {
            return 0;

         * Get char representing the state at the position in c-th column and r-th row
         * @return {@code '_'} if this and all adjacent field contain no {@code '#'}
        int g(int c, int r) {
            // v stores the result
            int v = 0;
            // or all adjacent fields
            for (int x = -1; x < 2; x++) {
                for (int y = -1; y < 2; y++) {
                    // ungolfed we should use something like
                    // v |= 0 > c + x || 0 > r + y || c + x >= a || r + y >= a ? 0 : f[c + x][r + y];
                    // but his will do (and is shorter)
                    try {
                        v |= f[c + x][r + y];
                    } catch (Exception e) {
            // we use '_' (95), 'O' (79), '#' (35). Only 35 contains the 32-bit
            // So we only need the 32-bit of the or-ed-value + the bits of the value directly in this field
            return v & (f[c][r] | 32);



6 ; 3 2 1 ; A1 A3 B2 C3 D4 E5 F6 B6 E3 D3 F4




  • O missed shot
  • # ship-part
  • _ shoot here next ;-)

See on ideone.com

Input-handling expects the spaces around numbers / ; and won't work otherwise.

I am placing big ships first as they have fewer places to go. If you want to switch to small-first you can replace pop() by remove(0) and push(s) by add(0,s) even replacing only one of the two should still result in a valid program.

If you allow ships touching each other the g(int,int) function can be vastly simplified or removed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.