Submit | All submissions | Best solutions | Back to list |
BSHIP - Battleships |
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.
Added by: | SourSpinach |
Date: | 2013-05-06 |
Time limit: | 2s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
hide comments
|
|||||
2024-10-30 07:14:16
for debugging: count matrix for M=8, N=10, K=8: 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 1 2 3 3 3 3 3 3 2 1 count matrix for M=19, N=23, K=11: 1 2 3 4 5 6 7 8 9 10 11 11 11 10 9 8 7 6 5 4 3 2 1 2 4 6 8 10 12 14 16 18 20 22 22 22 20 18 16 14 12 10 8 6 4 2 3 6 9 12 15 18 21 24 27 30 33 33 33 30 27 24 21 18 15 12 9 6 3 4 8 12 16 20 24 28 32 36 40 44 44 44 40 36 32 28 24 20 16 12 8 4 5 10 15 20 25 30 35 40 45 50 55 55 55 50 45 40 35 30 25 20 15 10 5 6 12 18 24 30 36 42 48 54 60 66 66 66 60 54 48 42 36 30 24 18 12 6 7 14 21 28 35 42 49 56 63 70 77 77 77 70 63 56 49 42 35 28 21 14 7 8 16 24 32 40 48 56 64 72 80 88 88 88 80 72 64 56 48 40 32 24 16 8 9 18 27 36 45 54 63 72 81 90 99 99 99 90 81 72 63 54 45 36 27 18 9 9 18 27 36 45 54 63 72 81 90 99 99 99 90 81 72 63 54 45 36 27 18 9 9 18 27 36 45 54 63 72 81 90 99 99 99 90 81 72 63 54 45 36 27 18 9 8 16 24 32 40 48 56 64 72 80 88 88 88 80 72 64 56 48 40 32 24 16 8 7 14 21 28 35 42 49 56 63 70 77 77 77 70 63 56 49 42 35 28 21 14 7 6 12 18 24 30 36 42 48 54 60 66 66 66 60 54 48 42 36 30 24 18 12 6 5 10 15 20 25 30 35 40 45 50 55 55 55 50 45 40 35 30 25 20 15 10 5 4 8 12 16 20 24 28 32 36 40 44 44 44 40 36 32 28 24 20 16 12 8 4 3 6 9 12 15 18 21 24 27 30 33 33 33 30 27 24 21 18 15 12 9 6 3 2 4 6 8 10 12 14 16 18 20 22 22 22 20 18 16 14 12 10 8 6 4 2 1 2 3 4 5 6 7 8 9 10 11 11 11 10 9 8 7 6 5 4 3 2 1 |
|||||
2015-01-03 19:18:46 swordfish12
simple and straight forward ---- be careful with cin/cout... |
|||||
2014-12-23 19:14:49 Sudarshan K
Learnt tonnes of stuff. Time,Space optimizations. Hats off _/\_. Tip: Use scanf/printf. For some reason setprecision and cout gave WA :) |
|||||
2013-07-14 09:39:34 007: Name stolen
@Jacob Plachta : plz can u check my subbmission id :- 9654115(link http://www.spoj.com/files/src/9654115/) and plz can u tell me my mistake will be very grateful to u..... thank you Last edit: 2013-07-14 09:39:59 |
|||||
2013-06-28 08:58:46 SilentKiller
I am continuously getting SIGSEGV error . Can anyone help? Edit: Got AC but still dnt know why I was getting SIGSEGV @problemsetter: Please look into the code and tell me what was wrong :) RE: String variables are initialized with length 0, and for a string variable s, accessing s[x] (with x >= s.length()) does indeed yield a runtime error. Last edit: 2013-07-05 13:21:06 |
|||||
2013-06-05 18:02:05 Muh. Aunorafiq Musa
finally :D Last edit: 2013-06-06 12:06:00 |
|||||
2013-05-20 07:37:25 Aradhya
@Tjandra:: bc how you solved this one in 1.6 M o.O |
|||||
2013-05-19 04:35:14 Federico Lebrón
Two pass, O(MN) memory solution runs in 0.04s. One pass, O(1) memory solution runs in 0.09s. You can't explain that. |
|||||
2013-05-18 04:06:25 Hardik Rakholiya
@Jacob Plachta: Sir i have resolved that problem with NaN. And i have also checked by running it on ideone.com. It gives right answer on ideone.com for small strings of 'X' and '.'. I havent checked with large strings. I am rounding off using compiler itself where 0.5555555... is rounded off like .555556. I am asking much but please help. RE: You're really close, but on just the largest case, your answer is very far off. Last edit: 2013-05-18 06:31:22 |