计算机学院大学生程序设计竞赛(2015’12) 1006 01 Matrix(dp)

01 Matrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 605    Accepted Submission(s): 127
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<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[i][j]代表着点(i,j)右下方的最长可行矩阵的长度
    
    
L[i][j]代表着点(i,j)右方的最长可行矩阵的长度
    
    
R[i][j]代表着点(i,j)下方的最长可行矩阵的长度

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<set>
#include<stack>
#include<map>
#include<algorithm>
#include<list>
#include<cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;

int dp[1010][1010],L[1010][1010],R[1010][1010],Hash[1010];
char str[1010][1010];

int main()
{
    int T,n,m,k;
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T-- && scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n+1; i++)
            for(int j=1; j<=n+1; j++)
                dp[i][j] = L[i][j] = R[i][j] =  Hash[i] = 0;
        for(int i=1; i<=n; i++)
            scanf("%s",str[i]+1);
        for(int i=n; i>=1; i--)
        {
            for(int j=n; j>=1; j--)
            {
                if(str[i][j] == '0')
                    dp[i][j] = L[i][j] = R[i][j] = 0;
                else
                {
                    L[i][j] = L[i+1][j] + 1;
                    R[i][j] = R[i][j+1] + 1;
                    dp[i][j] = min(min(L[i][j],R[i][j]),dp[i+1][j+1]+1);
                    Hash[dp[i][j]]++;
                }
            }
        }
        for(int i=n; i>=1; i--)
            Hash[i] += Hash[i+1];
        while(m--)
        {
            scanf("%d",&k);
            printf("%d\n",Hash[k]);
        }
    }

    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值