Star Wars

시간 제한1초메모리 제한2048 MB

요약
흰색, 검은색, 빈 칸으로 이루어진 n x m 보드에서 흰색 말 하나를 골라 위쪽으로만 이동하며 최대로 잡을 수 있는 검은 말의 수를 구한다.
난이도

보통10점 중 5점

유형
동적 계획법, DFS
정답자
아직 제출이 없습니다

문제

Amirreza is playing the Star Wars game. This game is played on an n×mn \times m board where each cell is either empty (‘.’), contains a white piece (‘W’) or a black piece (‘B’). At start of the game, Amirreza should choose exactly one white piece to play with. Afterwards he can move this piece multiple times to knock out as many black pieces as possible. Suppose the white piece is currently in cell (i,j)(i, j) of the board; In one move, this piece can go up-left (i−1,j−1)(i - 1, j - 1), up (i−1,j)(i - 1, j) or up-right (i−1,j+1)(i - 1, j + 1), provided that cell exists on the board and it does not contains another white piece. If the cell contains a black piece, it will be knocked out. Help Amireza figure out the maximum number of black pieces he can knock out.

입력

The first line contains two integers nn and mm (1≤n,m≤501 \le n, m \le 50), the number of rows and columns in the board, respectively. This is followed by nn lines, each containing mm characters. The jjth character of the (i+1)(i + 1)th line represents cell (i,j)(i, j). Each character is ‘W’, ‘B’, or ‘.’, denoting a white piece, a black piece, or an empty cell, respectively.

출력

Print a single integer, the maximum number of black pieces Amirreza can knock out.

예제1

  1. 예제 1

    입력
    8 10
    .W...BB...
    W..B.WB...
    .B.WB...W.
    .B..B.....
    ..W...BB..
    B.B..B.W.W
    .WB.W...B.
    ..W..BW.B.
    
    예상 출력
    5