codeforces 372B B. Counting Rectangles is Fun(暴力)

题目链接:

codeforces 372B


题目大意:

给出一个01矩阵,每次询问给出一个范围,问这个范围当中的全0矩阵的个数。


题目分析:

  • 首先做一个预处理,处理出sum[i][j][k][t]也就是在以(i,j)为左上角的矩阵中以(k,t)为右下角能够得到的阶梯的最大面积。(因为阶梯状能够保证每个点都可以作为左上角得到一个矩形)
  • 暴力求取即可
  • 然后每次查询,枚举右下角,然后求和即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 47

using namespace std;

int n,m,q;
char s[MAX][MAX];
int l[MAX][MAX];
int sum[MAX][MAX][MAX][MAX];

void init ( )
{
    memset ( l , 0 , sizeof ( l ) );
    for ( int i = 1 ; i <= n ; i++ )
        for ( int j = 1 ; j <= m ; j++ )
        {
            if ( s[i][j] == '1' ) continue;
            if ( s[i][j-1] == '0' ) 
                l[i][j] = l[i][j-1] + 1;
            else l[i][j] = 1;
        }
    for ( int i = 1 ; i <= n ; i++ )
        for ( int j = 1 ; j <= m ; j++ )
            for ( int k = i ; k<= n ;k++ )
                for ( int t = j ; t <= m ;t++ )
                {
                    int temp,ss;
                    temp = 1<<29;
                    ss = 0;
                    for ( int x = k ; x >= i ; x-- )
                    {
                        int ll = min ( t-j+1 , l[x][t] );
                        temp = min ( ll , temp );
                        ss += temp;
                    }
                    sum[i][j][k][t] = ss;
                }
}

int main ( )
{
    while ( ~scanf ( "%d%d%d" , &n , &m , &q ) )
    {
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%s" , s[i]+1 );
        init ( );
        int a,b,c,d;
        while ( q-- )
        {
            scanf ( "%d%d%d%d" , &a , &b , &c , &d );
            int ans = 0;
            for ( int i = a ; i <= c ; i++ )
                for ( int j = b ; j <= d ; j++ )
                    ans += sum[a][b][i][j];
            printf ( "%d\n" , ans );
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值