F - Spotlights CodeForces - 738B

本文介绍了一个算法,用于计算在一个给定的矩形舞台上,能够放置舞台灯光的好位置的数量。通过预处理上下左右方向上的演员分布情况,算法可以高效地计算出满足条件的位置总数。

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.

You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.

A position is good if two conditions hold:

  • there is no actor in the cell the spotlight is placed to;
  • there is at least one actor in the direction the spotlight projects.

Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

Input

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.

The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Output

Print one integer — the number of good positions for placing the spotlight.

Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note

In the first example the following positions are good:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.

题意:就是看a[i][j]==0是它的4个方位有几个1;

代码:

#include<stdio.h>
#include<string.h>
int n,m,a[1200][1200],sum[1200][1200][4];
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        int ans=0;
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)//因为我们输入时从上至下、从左至右,所以先找我们输入的这个数的上方和左边有没有1;
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
                sum[i][j][0]=sum[i-1][j][0]|a[i][j];//如果它的上面一个的上方有1或自己是1,就标记它的上方是1;
                sum[i][j][1]=sum[i][j-1][1]|a[i][j];//如果它的左边一个的左边有1或自己是1,就标记它的左边是1;
            }
        }
        for(int i=n;i>0;i--)
        {
            for(int j=m;j>0;j--)
            {
                sum[i][j][2]=sum[i][j+1][2]|a[i][j];//同上
                sum[i][j][3]=sum[i+1][j][3]|a[i][j];//同上
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==0)
                    ans+=sum[i][j][0]+sum[i][j][1]+sum[i][j][2]+sum[i][j][3];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值