BSHIP - Battleships

no tags 

You and a friend are playing the classic game of Battleships. You each have a grid consisting of $M$ rows of $N$ cells each ($1 \leq M,N \leq 2000$). Each cell is either empty or contains a player’s ship (in this version of the game, all ships are the size of one cell). The goal of the game is to destroy all of the opponent’s ships by hitting individual cells.

You and your friend have bet tons of Internet Points on this game. Unfortunately, your friend is completely owning you. So desperate times call for desperate measures.

You know for a fact that you can distract your friend for a brief moment by telling him that a famous programmer is behind him, but this trick will only work exactly once (programmers are so predictable). While he isn’t looking, you’ll have time to snatch up some of his ships with one hand. Your hand can cover a square of exactly $S$x$S$ cells ($1 \leq S \leq min\{M,N\}$), and you can gather all the ships within such a square at once.

Of course, your friend is no fool, so he’s got his grid well concealed. As such, you don’t know anything about it except its size, so when the time comes, you’ll just choose a random square of size $S$x$S$ that’s completely within the grid.

As usual, these bets attract large crowds. One of the bystanders who can see your opponent’s grid knows your plan, and is curious as to the expected number of ships that you will grab (in other words, the average number of ships out of all the possible snatches you could make). Nerdy though he is, he can’t calculate it in his head, so he runs over to a computer and codes up a program...

Input

Line 1: 3 integers, $M$, $N$, and $S$.

Next $M$ lines: $N$ characters each, representing your opponent’s grid – an ‘X’ represents a ship, while a ‘.’ represents an empty cell.

Output

A single number – the expected number of ships that you’ll grab, rounded to 6 decimal places.

Example

Input:
3 4 2
XX.X
XX..
.X..

Output:
2.000000

Explanation of Sample

There are 6 possible areas you could pick, yielding this many ships each:

4 2 1
3 2 0

This is a total of 12 ships, for an average of exactly 2 per grab.


hide comments
arko: 2013-05-10 15:25:20

if the test case is:
1 6 1
XXXX..
should the answer be 0.666666
or 0.666667??

RE: It should be 0.666667, since you're rounding off to 6 decimal places.

Last edit: 2013-05-10 16:24:10
Mitch Schwartz: 2013-05-07 05:49:34

Outputting 2 instead of 2.000000 is fine according to my test.

Master Wad7a: 2013-05-06 22:53:12

Output 2 not ac
Output 2.000000 ac

RE: Right, I guess SPOJ's standard 10^-6 precision judge actually requires at least 6 decimal places... The problem statement's been updated.

Last edit: 2013-05-06 22:54:39
Hasan Jaddouh: 2013-05-06 22:53:12

too easy :)

RE: Check out http://www.spoj.com/problems/ABSHIP/ for more fun

Last edit: 2013-05-06 10:13:04

Added by:SourSpinach
Date:2013-05-06
Time limit:2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64