2015‘12杭电校赛1006 01 Matrix(二维DP)

题解

我们用d[i][j]表示右下角为(i,j)的最大的全1正方形的size是多少。

那么直接有一个DP转移

if(s[i][j]==’1’)d[i][j]=min(d[i-1][j],min(d[i][j-1],d[i-1][j-1]))+1;

else d[i][j]=0;

然后一样计数++f[d[i][j]]

最后扫描一遍就可以啦。

代码


#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int N=50005;
int n,k,m;
int s[1005];
int v[1005][1005],r[1005][1005],x[1005][1005],a[1005][1005];
char c[1005];
int main()
{
    int i,j;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(s,0,sizeof(s));
        memset(r,0,sizeof(r));
        memset(x,0,sizeof(x));
        for(i=1; i<=n; i++)
        {
            scanf("%s",c);
            for(j=0;j<n;j++)
                a[i][j+1]=c[j]-'0';
        }
        x[n+1][n]=0;
        r[n][n+1]=0;
        v[n+1][n+1]=0;
        for(i=n; i>=1; i--)
        {
            for(j=n; j>=1; j--)
            {
                if(a[i][j])
                {
                    v[i][j]=min(min(v[i+1][j+1],r[i][j+1]),x[i+1][j]);
                    r[i][j]=r[i][j+1]+1;
                    x[i][j]=x[i+1][j]+1;
                    v[i][j]++;
                }
                else
                    v[i][j]=r[i][j]=x[i][j]=0;
                s[v[i][j]]++;
            }
        }
        for(i=n-1; i>=1; i--)
        {
            s[i]+=s[i+1];
        }
        while(m--)
        {
            scanf("%d",&k);
            cout<<s[k]<<endl;
        }
    }
    return 0;
}

题目

01 Matrix

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 666 Accepted Submission(s): 156

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).

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值