DCEPC14H - Watchers On The Wall

no tags 

John Snow has been recently appointed commander of the nights watch, And as his first task he needs to defend castle black against the wildlings army again.

Mance Rayder, commander of the wildlings has assembled a fierce army consisting of Giants, Thenns, Hornfoots, Ice-river clans, cave people each having different fighting abilities

John has built various towers in the area. From each tower a cannon can be fired in all or some of the four direction (N, W, E, S) which destroys all enemies in its way until it is blocked by a wall or a tower. John has only one shot before the sunrise so he want to cause maximum damage.

Your task is to help john assign directions (possibly all / none)  to shoot cannons for each tower such that total damage is maximized. However no two cannons should cross paths without any wall or tower in between.

John can also choose not to shoot any cannons from any tower.

All cannons are fired simultaneously.

Input

A grid of size N each position in grid can be:

'#' wall,

'T' tower,

‘0’ denoting empty space,

integer(1-9) denoting the ability of enemy.

First line is N size of the grid.

next n lines specifies the grid.

Output

Out put a single integer- maximum possible damage in one shot.

Constraints

N <= 500

number of cannons <= 200

Example

Input:
4
T000
1#T3
292T
9000

Output:
17
Input:
4
T000
1003
292T
9000

Output:
16

hide comments
dce coders: 2015-05-25 07:24:40

@quartus it's simply means that there will be at most 200 'T' in any given grid.

dce coders: 2015-05-25 07:23:25

@shubhankaryash Tower at (1,1) shoots down to get 1+2+9
Tower at (2,3) shoots down to get 2
Tower at (3,4) shoots up to get 3
So answer is 1+2+9+2+3=17

shubhankaryash: 2015-05-21 09:56:30

could you plz explain how the first case has 17 as output? Does the question mean that only 1 tower can make a shot or evdry tower can a shot??

quartus: 2015-05-20 03:09:25

@dce_coders: When the specs say at most 200 "cannons," what does this count, exactly? Are there exactly 4 cannons per tower (one in each of the cardinal directions) or only 1 cannon per tower (that can be fired possibly in multiple directions) or does the number 200 reflect something else, such as the number of vantage points not adjacent to exterior or wall? For example, for the T in the upper left corner in the two examples: does that tower have 4 cannons, 2, or 1?

dce coders: 2015-05-02 02:12:32

@Myrthan a tower may not shoot at all.
So answer is 2+9+2+3

Myrthan: 2015-05-02 00:15:21

Can someone explain why in second case we have 16?
T000
1003
292T
9000
I see 15 is max.


Added by:dce coders
Date:2015-04-26
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:C CSHARP C++ 4.3.2 CPP CPP14 C99 GOSU JAVA PAS-GPC PAS-FPC PYTHON PYPY PYTHON3 PY_NBC