D. White Lines
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of nn rows and nncolumns of square cells. The rows are numbered from 11 to nn, from top to bottom, and the columns are numbered from 11 to nn, from left to right. The position of a cell at row rr and column cc is represented as (r,c)(r,c). There are only two colors for the cells in cfpaint — black and white.
There is a tool named eraser in cfpaint. The eraser has an integer size kk (1≤k≤n1≤k≤n). To use the eraser, Gildong needs to click on a cell (i,j)(i,j) where 1≤i,j≤n−k+11≤i,j≤n−k+1. When a cell (i,j)(i,j) is clicked, all of the cells (i′,j′)(i′,j′) where i≤i′≤i+k−1i≤i′≤i+k−1 and j≤j′≤j+k−1j≤j′≤j+k−1 become white. In other words, a square with side equal to kk cells and top left corner at (i,j)(i,j) is colored white.
A white line is a row or a column without any black cells.
Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.
Input
The first line contains two integers nn and kk (1≤k≤n≤20001≤k≤n≤2000) — the number of rows and columns, and the size of the eraser.
The next nn lines contain nn characters each without spaces. The jj-th character in the ii-th line represents the cell at (i,j)(i,j). Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.
Output
Print one integer: the maximum number of white lines after using the eraser exactly once.
Examples
input
Copy
4 2 BWWW WBBW WBBW WWWB
output
Copy
4
input
Copy
3 1 BWB WWB BWB
output
Copy
2
input
Copy
5 3 BWBBB BWBBB BBBBB BBBBB WBBBW
output
Copy
2
input
Copy
2 2 BW WB
output
Copy
4
input
Copy
2 1 WW WW
output
Copy
4
Note
In the first example, Gildong can click the cell (2,2)(2,2), then the working screen becomes:
BWWW WWWW WWWW WWWB
Then there are four white lines — the 22-nd and 33-rd row, and the 22-nd and 33-rd column.
In the second example, clicking the cell (2,3)(2,3) makes the 22-nd row a white line.
In the third example, both the 22-nd column and 55-th row become white lines by clicking the cell (3,2)(3,2).
题意:
n*n的方格,檫除k*k的方格,问最后最多可以剩多少条白线?白线必须保证一行或一列都为W。
分析:
疯狂预处理,sumR[i][j]:表示第i行(1~j)列有多少个B,sumC[i][j]:表示第j列(1~i)行有多少个B,
R[i][j]表示第i行消除(j~j+k)列是否会产生新的白线行, C[i][j]表示第j列消除(i~i+k)行是否会产生新的白线列
ansR[i][j]表示第i行消除前(1~j)列会产生多少白线列, ansC[i][j]表示第j行消除(1~j)列产生多少白线行。
#include <bits/stdc++.h>
using namespace std;
const int N=2005;
int sumR[N][N],sumC[N][N];
int ansR[N][N],ansC[N][N];
int R[N][N],C[N][N];
char ch[N];int n,k;
void out(int a[][N])
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1; i<=n; i++)
{
scanf("%s",ch+1);
for(int j=1; j<=n; j++)
{
sumR[i][j]=sumR[i][j-1]+(ch[j]=='B');
sumC[i][j]=sumC[i-1][j]+(ch[j]=='B');
}
}
int pre_sum=0;
for(int i=1; i<=n; i++)
{
if(sumR[i][n]==0)
pre_sum++;
}
for(int i=1; i<=n; i++)
{
if(sumC[n][i]==0)
pre_sum++;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n-k+1; j++)
{
if(sumR[i][n]!=0)
if(sumR[i][n]==sumR[i][j+k-1]-sumR[i][j-1])
{
R[i][j]=1;
}
}
}
for(int j=1; j<=n; j++)
{
for(int i=1; i<=n-k+1; i++)
{
if(sumC[n][j]!=0)
if(sumC[n][j]==sumC[i+k-1][j]-sumC[i-1][j])
{
C[i][j]=1;
}
}
}
/* out(ansR);
out(ansC); */
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
ansR[i][j]=ansR[i][j-1]+C[i][j];
ansC[i][j]=ansC[i-1][j]+R[i][j];
}
}
/*out(ansR);
out(ansC); */
int ans=0;
for(int i=1; i<=n-k+1; i++)
for(int j=1; j<=n-k+1; j++)
{
ans=max(ans,ansR[i][j+k-1]-ansR[i][j-1]+ansC[i+k-1][j]-ansC[i-1][j]) ;
}
printf("%d",ans+pre_sum);
}