01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 625 Accepted Submission(s): 134
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
题意:给定一个N*N的0-1矩阵和Q次查询,每次查询给出一个k,问你全是1的k*k矩阵有多少个。
#include <iostream>
#include<stdio.h>
#include <cstring>
using namespace std;
int a[1001][1001] ,b[1000];
char s[1001][1001];
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
int ma=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0; i<n; i++)
scanf("%s",s[i]);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i+1][j+1]=s[i][j]-'0';
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(a[i][j]==1)
{
int mi=a[i-1][j-1];
if(mi>a[i-1][j])mi=a[i-1][j];
if(mi>a[i][j-1])mi=a[i][j-1];
mi++;
a[i][j]=mi;
b[mi]++;
ma=max(mi,ma);
}
}
for(int i=ma; i>1; i--)
{
b[i-1]+=b[i];
}
int x;
for(int i=0; i<m; i++)
{
cin>>x;
cout<<b[x]<<endl;
}
}
return 0;
}