题目链接:
题目大意:
给出一个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 );
}
}
}