Spotlights

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.


题意:给了一个n*m的舞台,舞台上0代表空位,1代表演员.要求我们放置聚光灯.聚光灯不能放置在演员的位置,所以他得放在0的位置上,并且使得这个聚光灯能照到演员.如果有一个方向满足这个条件,那就算一种照射方式.

分析:按照题目意思,就是求每个0的四周有1的方向的和,那么一开始我就想遍历一下,然后顺理成章的就T在第七个样例.

于是得换种方法.

再分析:刚刚我们是先找0,然后再找有多少个1的方向,那么如果反一反呢(因为1比较少).也就是说先找1,然后再找四周有多少个零.这样试了试,(不用试了)第一个样例都不过不了,说明这个思路还不对,还要处理一下.

那么来看第一个样例,如果找1的周围0那么输出是10.多了一个,那么多了哪一个呢???仔细一点可以发现,左下角的0的右边边方向多了一个.那么这个是和(2,3)右边那个0重复的.

所以for循环要处理一下,一旦在找零的过程中遇到了1,那么就返回.


思路成了,要注意一下,别有多余的头文件,别用cin和cout.不然你会在第七个样例T到绝望...

代码:

#include<stdio.h>
#define M 1000+10
int a[M][M],n,m,ans;;
void Search(int x,int y)
{
    for(int i=x+1;i<n;i++)
    {
        if(a[i][y])
            break;
        ans++;
    }
    for(int i=x-1;i>=0;i--)
    {
        if(a[i][y])
            break;
        ans++;
    }
    for(int j=y+1;j<m;j++)
    {
        if(a[x][j])
            break;
        ans++;
    }
    for(int j=y-1;j>=0;j--)
    {
        if(a[x][j])
            break;
        ans++;
    }

}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            scanf("%d",&a[i][j]);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j])
            Search(i,j);
        }
    }
    printf("%d\n",ans);
}

ps:2018年4月12日 17:08:30

这题比赛的时候一直T就没怎么多想了,回头一看学长的代码,才发现只差一点点转弯的思路,和一点小处理.

当初没做出来真可惜.

因为渴望知识而学习,非名利.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值