01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
It's really a simple problem.
Given a "01" matrix with size by n*n (the matrix size is n*n and only contain "0" or "1" in each grid), please count the number of "1" matrix with size by k*k (the matrix size is k*k and only contain "1" in each grid).
Given a "01" matrix with size by n*n (the matrix size is n*n and only contain "0" or "1" in each grid), please count the number of "1" matrix with size by k*k (the matrix size is k*k and only contain "1" in each grid).
Input
There is an integer T (0 < T <=50) in the first line, indicating the case number.
Each test case begins with two numbers n and m (0<n, m<=1000), specifying the size of matrix and the query number.
Then n lines follow and each line contains n chars ("0" or "1").
Then m lines follow, each lines contains a number k (0<k<=n).
Each test case begins with two numbers n and m (0<n, m<=1000), specifying the size of matrix and the query number.
Then n lines follow and each line contains n chars ("0" or "1").
Then m lines follow, each lines contains a number k (0<k<=n).
Output
For each query, output the number of "1" matrix with size by k*k.
Sample Input
2 2 2 01 00 1 2 3 3 010 111 111 1 2 2
Sample Output
1 0 7 2 2
/*
题解:
如果从左上角向右下角,每个点对应最大矩形的边长是很难考虑清楚地
所以可以从右下角想左上角进行转移(dp是从局部到整体的思想)
Down[i][j]表示这个点的下方有多少个连续的'1'
Right[i][j]表示这个点的右方有多少个连续的'1'
dp[i][j]表示以这个点为左上方顶点的矩形边长最大为多少
状态转移方程:
如果这个点对应的s[i][j]为'0',那么所有的数组均为0
相反,则Down[i][j]=Down[i+1][j]+1,
Right[i][j]=Right[i][j+1],
dp[i][j]讨论一下就可以了
然后知道了所有点的dp[i][j],利用后缀和的便可以求出每个边长有多少个方案了
*/
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
char s[1102][1102];
int Down[1102][1102],Right[1102][1102],dp[1102][1102];
int C[1002];
int main(){
int n,t,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n+10;i++){
for(int j=1;j<=n+10;j++)
dp[i][j]=0,Down[i][j]=0,Right[i][j]=0;
C[i]=0;
}
for(int i=n;i>=1;i--)
for(int j=n;j>=1;j--){
if(s[i][j]=='0'){
Down[i][j]=0;
dp[i][j]=0;
Right[i][j]=0;
}
else{
Right[i][j]=Right[i][j+1]+1;
Down[i][j]=Down[i+1][j]+1;
if(Right[i][j]>=dp[i+1][j+1]+1&&Down[i][j]>=dp[i+1][j+1]+1)
dp[i][j]=dp[i+1][j+1]+1;
else
dp[i][j]=min(Down[i][j],Right[i][j]);
C[dp[i][j]]++;
}
}
for(int i=n;i>=1;i--)
C[i]+=C[i+1];
while(m--){
int k;
scanf("%d",&k);
printf("%d\n",C[k]);
}
}
return 0;
}