Codeforces Beta Round #95 (Div. 2)

F. Present to Mom
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

How many stars are there in the sky? A young programmer Polycarpus can't get this question out of his head! He took a photo of the starry sky using his digital camera and now he analyzes the resulting monochrome digital picture. The picture is represented by a rectangular matrix consisting of n lines each containing m characters. A character equals '1', if the corresponding photo pixel is white and '0', if it is black.

Polycarpus thinks that he has found a star on the photo if he finds a white pixel surrounded by four side-neighboring pixels that are also white:

 1 
111
 1 
a star on the photo

Polycarpus whats to cut out a rectangular area from the photo and give his mom as a present. This area should contain no less than k stars. The stars can intersect, have shared white pixels on the photo. The boy will cut out the rectangular area so that its borders will be parallel to the sides of the photo and the cuts will go straight between the pixel borders.

Now Polycarpus keeps wondering how many ways there are to cut an area out of the photo so that it met the conditions given above. Help Polycarpus find this number.

<p< p=""><p< p="">
Input
<p< p="">

The first line of the input data contains three integers n, m and k (1 ≤ n, m ≤ 500;1 ≤ k ≤ nm). Then follow n lines, containing the description of the given photo as a sequence of lines. Each line contains m characters '0' or '1'.

<p< p=""><p< p="">
Output
<p< p="">

Print the required number of areas on the given photo.

<p< p=""><p< p="">
Sample test(s)
<p< p=""><p< p="">
input
4 6 2
111000
111100
011011
000111
output
6
input
5 5 4
11111
11111
11111
11111
11111
output
9
<p< p=""><p< p="">
Note
<p< p="">

We'll number the rows and columns below starting from 1, the coordinates (p, q) will denote a cell in row p, column q.

In the first sample Polycarpus should cut out any area containing a rectangle whose opposite corners lie in cells (1, 1) and (3, 4). Only rectangles with opposite corners in (1, 1) and (x, y), where x ≥ 3 and y ≥ 4 fit the conditions.

In the second sample any rectangle whose each side is no less than four, will do. The possible rectangle sizes are 4 × 44 × 55 × 4 and 5 × 5. Such figures can be cut in 4 ways, 2 ways, 2 ways and 1 way correspondingly.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.



#include<cstdio>
#include<cstring>

#define N 505
int R,C,K;
int flag[N][N];
int map[N][N];
__int64 sum[N][N];

int main()
{
    scanf("%d%d%d",&R,&C,&K);
    for ( int i = 0; i <= R+2; i++ )
    for ( int j = 0; j <= C+2; j++ )
        flag[i][j] = map[i][j] = sum[i][j] = 0;

    char str[N];
    for ( int i = 1; i <= R; i++ )
    {
        scanf("%s",str);
        for ( int j = 0; str[j]; j++ )
            flag[i][j+1] = str[j] - '0';
    }

    for ( int i = 1; i <= R; i++ )
    {
        for ( int j = 1; j <= C; j++ )
        {
            map[i][j] = flag[i][j] && flag[i-1][j] && flag[i+1][j] && flag[i][j-1] && flag[i][j+1];
            sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + map[i-1][j-1];
        }
    }

    __int64 res = 0;
    int last_bot, last_col;
    int top, bot, lt, rt;
    int mark;
    for ( top = 1, last_bot = 3; top <= R - 2; top++ )
    {
        mark = 0;
        for ( bot = last_bot; bot <= R; bot++ )
        {
            for ( lt = 1, last_col = 3; lt <= C - 2; lt++ )
            {
                for ( rt = last_col ; rt <= C; rt++ )
                    if ( sum[bot][rt] - sum[bot][lt+1] - sum[top+1][rt] + sum[top+1][lt+1] >= K )
                    {
                        res += C - rt + 1; break;
                        mark++;
                    }
                last_col = rt;
            }

            if ( mark == 1 )
                last_bot = bot;
        }
    }
    printf("%I64d\n",res);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值