TITLE: Battleship Permutations
QUESTION [14 upvotes]: Using the game of Battleship as an example, is there a general solution for determining the number of arrangements of a given set of 1xN rectangles on a X by Y grid?
Example: In Battleship, each player has a 10x10 grid on which they must place each of the following rectangular 1xN ships which may not overlap or be placed diagonally:

1x5-square "carrier"
1x4-square "battleship"
1x3-square "submarine"
1x3-square "cruiser"
1x2-square "destroyer"

A) How would one calculate the number of arrangements for this example that use each "ship" exactly once?
B) What is the general solution for a grid of height Y and width X, and a given set of 1xN "ships"?

REPLY [3 votes]: I've recently done some coding to try and solve this problem, and I've arrived at the answer
30,093,975,536
where we consider the two length 3 ships as distinct. I can't say that I am certain of the result [edit: having received confirmation by a second party, I believe that I can say that I am certain now], and you can find my (python) code at
http://forums.xkcd.com/viewtopic.php?f=17&t=97981&p=3186720#p3186509
If someone can find a clear logical error in it, or has comments on the algorithm I use, I'd be keen to talk specifics.
@Lemming: Regarding the table found at
http://www.haskell.org/haskellwiki/Battleship_game_combinatorics
the second result there is wrong. If we consider placing 2 ships of length 2, there are 8 ways to put the first in a corner which blocks 4 moves for the second ship, 28 ways to place it along a side which blocks 5, 32 ways to place it perpendicular to a side which blocks 6 moves, and 112 ways to place the first ship in the middle of the board, which block 7 moves for the second ship ie there are (8*176 + 28 * 175 + 32 * 174 + 112 * 173)/2 = 15626 ways to place 2 indistinguishable 2s, and the table at the haskell wiki says there are 13952.
Edit: I believe I've determined where the disagreement in numbers comes from. If one assumes that ships can be placed in any way so long as they don't overlap anywhere, then you get the figures that I reported. But, if you assume that ships cannot be adjacent, neither horizontally, vertically or diagonally, then the numbers that arise are those reported by Lemmings.
Nobody is wrong, people are just using different sets of rules.