Problem:


Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13



Sample input:
4 6
#..#..
.....#
....#.
#.#...


Sample output:
8

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)


def calc(x, isTrans):
    if isTrans:
        y = np.copy(x.T)
    else:
        y = np.copy(x)

    for i in range(len(y)):
        xxx = np.copy(y[i])
        tmp_l = np.arange(len(xxx))
        tmp_l[xxx > 0] = 0
        np.maximum.accumulate(tmp_l, out=tmp_l)
        left = tmp_l

        xxx_reverse = xxx[::-1]
        tmp_r = np.arange(len(xxx_reverse))
        tmp_r[xxx_reverse > 0] = 0
        np.maximum.accumulate(tmp_r, out=tmp_r)
        right = len(xxx)-tmp_r[::-1]-1

        y[i] = right-left-1
        y[i][xxx == 0] = 0

    if isTrans:
        return np.transpose(y)
    else:
        return y


if __name__ == "__main__":
    h, w = map(int, readline().split())
    room = np.zeros((h+2, w+2), dtype=np.int)
    for i in range(1, h+1):
        room[i][1:-1] = [0 if i == "#" else 1 for i in input()]

    a = calc(room, True)
    b = calc(room, False)
    ans = (a + b).max() - 1
    print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
C = [list(input()) for _ in range(H)]
dp = [[0] * W for _ in range(H)]

# x方向
for y in range(H):
    prev_x = -1
    for x in range(W):
        if C[y][x] == '#':
            for i in range(x - prev_x - 1):
                dp[y][i + prev_x + 1] = x - prev_x - 1
            prev_x = x
        if x == W - 1:
            for i in range(x - prev_x):
                dp[y][i + prev_x + 1] = x - prev_x
             

ans = 0
# y方向
for x in range(W):
    prev_y = -1
    for y in range(H):
        if C[y][x] == '#':
            for i in range(y - prev_y - 1):
                dp[i + prev_y + 1][x] += y - prev_y - 1
                ans = max(ans, dp[i + prev_y + 1][x] - 1) # 自分自身を2回カウントしているため-1
            prev_y = y
        if y == H - 1:
            for i in range(y - prev_y):
                dp[i + prev_y + 1][x] += y - prev_y
                ans = max(ans, dp[i + prev_y + 1][x] - 1) # 自分自身を2回カウントしているため-1


print(ans)

Submission status: Accepted

Solution code:
import numpy as np

H, W = map(int, input().split())
S = []
for h in range(H):
  S.append(list(map(lambda s:0 if s=='#' else 1, input())))
S = np.array(S)
 
l,r,u,d = [np.zeros([H,W], dtype=int) for _ in range(4)]

for w in range(W):
  l[:,w] = (l[:,w-1]+1)*S[:,w]
  r[:,-w-1] = (r[:,-w]+1)*S[:,-w-1]

for h in range(H):
  u[h] = (u[h-1]+1)*S[h]
  d[-h-1] = (d[-h]+1)*S[-h-1]
  
ans = np.max((l+r+u+d-3)*S)

print(ans)

Submission status: Accepted

Solution code:
def add(dp, n, l):
    t = 0
    for v in range(1, len(l)):
        y = l[v]
        x = l[v-1]
        while t != y:
            dp[n][t] += y - 1 - x
            t += 1
        t += 1


H,W = [int(_) for _ in input().split()]
s = []
b = []
c = []
for _ in range(H):
    s.append(input())
for i in range(H):
    a = []
    a.append(0)
    for j in range(W):
        if s[i][j] == '#':
            a.append(j+1)
    a.append(W+1)
    b.append(a)
for j in range(W):
    a = []
    a.append(0)
    for i in range(H):
        if s[i][j] == '#':
            a.append(i+1)
    a.append(H+1)
    c.append(a)
dp = [[0 for _ in range(W+2)] for _ in range(H+2)]
dp2 = [[0 for _ in range(H+2)] for _ in range(W+2)]
for i in range(len(b)):
    add(dp, i+1, b[i])
for i in range(W+2):
    for j in range(H+2):
        dp2[i][j] = dp[j][i]
for i in range(len(c)):
    add(dp2, i+1, c[i])
ans = 0
for i in range(1, W+1):
    for j in range(1, H+1):
        ans = max(ans, dp2[i][j])
print(ans-1)

Submission status: Accepted

Solution code:
def add(n, l):
    global dp
    t = 0
    for v in range(1, len(l)):
        y = l[v]
        x = l[v-1]
        while t != y:
            dp[n][t] += y - 1 - x
            t += 1
        t += 1

def add2(n, l):
    global dp2
    t = 0
    for v in range(1, len(l)):
        y = l[v]
        x = l[v-1]
        while t != y:
            dp2[n][t] += y - 1 - x
            t += 1
        t += 1


H,W = [int(_) for _ in input().split()]
s = []
b = []
c = []
for _ in range(H):
    s.append(input())
for i in range(H):
    a = []
    a.append(0)
    for j in range(W):
        if s[i][j] == '#':
            a.append(j+1)
    a.append(W+1)
    b.append(a)
for j in range(W):
    a = []
    a.append(0)
    for i in range(H):
        if s[i][j] == '#':
            a.append(i+1)
    a.append(H+1)
    c.append(a)
dp = [[0 for _ in range(W+2)] for _ in range(H+2)]
dp2 = [[0 for _ in range(H+2)] for _ in range(W+2)]
for i in range(len(b)):
    add(i+1, b[i])
for i in range(W+2):
    for j in range(H+2):
        dp2[i][j] = dp[j][i]
for i in range(len(c)):
    add2(i+1, c[i])
for i in range(H+2):
    for j in range(W+2):
        dp[i][j] = dp2[j][i]
ans = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        ans = max(ans, dp[i][j])
print(ans-1)

Submission status: Accepted

Solution code:
#d

def main():
    h,w = map(int,input().split())
    s=[]
    for i in range(h):
        s.append(list(input()) )
    
    l=[[0]*w for _ in range(h)]
    for i in range(h):
        count=0
        for j in range(w):
            if s[i][j] ==".":
                count+=1
            else:
                count=0
            l[i][j]=count
            
    r=[[0]*w for _ in range(h)]
    for i in range(h-1,-1,-1):
        count=0
        for j in range(w-1,-1,-1):
            if s[i][j] ==".":
                count+=1
            else:
                count=0
            r[i][j]=count
                    
    d=[[0]*w for _ in range(h)]
    for j in range(w-1,-1,-1):
        count=0
        for i in range(h-1,-1,-1):
            if s[i][j] ==".":
                count+=1
            else:
                count=0
            d[i][j]=count
                    
    u=[[0]*w for _ in range(h)]
    for j in range(w):
        count=0
        for i in range(h):
            if s[i][j] ==".":
                count+=1
            else:
                count=0
            u[i][j]=count
    
    ans=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            ans[i][j] = l[i][j]+ r[i][j]+ u[i][j]+ d[i][j]-3
    print( max( max(ans[i]) for i in range(h)) )

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np 
H, W = map(int, input().split())
S = (np.array([list(input()) for _ in range(H)]) == '.')
 

up = np.zeros((H, W), dtype=int)
down = np.zeros((H, W), dtype=int)
left = np.zeros((H, W), dtype=int)
right = np.zeros((H, W), dtype=int)
 
for i in range(H):
    up[i] = (up[i-1] + 1) * S[i]
    down[-i-1] = (down[-i] + 1) * S[-i-1]
    
for i in range(W):
    left[:, i] = (left[:, i-1] + 1) * S[:, i]
    right[:, -i-1] = (right[:, -i] + 1) * S[:, -i-1]

print(np.max(up+down+left+right) - 3)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
L, R, U, D = [[[0]*W for _ in range(H)] for i in range(4)]

for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            L[h][w] = L[h][w-1]+1
            U[h][w] = U[h-1][w]+1
        if S[h][W-1-w] == '.':
            R[h][-w-1] = R[h][-w]+1
        if S[H-1-h][w] == '.':
            D[-h-1][w] = D[-h][w]+1

ans = 0
for i in range(H):
    for j in range(W):
        light = L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3
        ans = max(ans, light)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)]) == '.'
 

up = np.zeros((H, W), dtype=int)
down = np.zeros((H, W), dtype=int)
left = np.zeros((H, W), dtype=int)
right = np.zeros((H, W), dtype=int)
 
for i in range(H):
    up[i] = (up[i-1] + 1) * S[i]
    down[-i-1] = (down[-i] + 1) * S[-i-1]
    
for i in range(W):
    left[:, i] = (left[:, i-1] + 1) * S[:, i]
    right[:, -i-1] = (right[:, -i] + 1) * S[:, -i-1]

print(np.max(up+down+left+right) - 3)