Codeforces Round-#380-Div. 2-B-Spotlights

B. Spotlights
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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:

the (1, 1) cell and right direction;
the (1, 1) cell and down direction;
the (1, 3) cell and left direction;
the (1, 3) cell and down direction;
the (1, 4) cell and left direction;
the (2, 2) cell and left direction;
the (2, 2) cell and up direction;
the (2, 2) and right direction;
the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.

题意对于给定的矩阵,1代表聚光灯,0代表演员,每个演员可以收到上下左右的聚光灯照射,但是同一方向只能收到一盏灯光。对于任意一个演员自然可能收到0~4盏灯的照射,问所有演员可以收到灯光照射的总数量

思路:对于每一盏灯往四个方向搜索,遇到演员就让演员的计数加一,直到边界或者遇到另一个聚光灯为止。为了方便搜索,聚光灯被用-1表示

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=1005;
int N,M;
int map[maxn][maxn];
void DFS(int i,int j,int vis_i,int vis_j)
{
    int flag_i=i+vis_i;
    int flag_j=j+vis_j;
    if(flag_i>=1&&flag_i<=N&&flag_j>=1&&flag_j<=M)
    {
        if(map[flag_i][flag_j]!=-1)
        {
            map[flag_i][flag_j]++;
            DFS(i+vis_i,j+vis_j,vis_i,vis_j);
        }
    }
}

int main()
{
    scanf("%d%d",&N,&M);
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
        {
            scanf("%d",&map[i][j]);
            if(map[i][j]==1)
                map[i][j]=-1;//标记为灯光
        }
    }
    int result=0;
    for(int i=1; i<=N; i++)
        for(int j=1; j<=M; j++)
            if(map[i][j]==-1)
            {
                DFS(i,j,0,1);
                DFS(i,j,0,-1);
                DFS(i,j,1,0);
                DFS(i,j,-1,0);
            }
    for(int i=1; i<=N; i++)
    {
        for(int j=1; j<=M; j++)
            if(map[i][j]!=-1)
                result+=map[i][j];
    }
//    for(int i=1; i<=N; i++)
//    {
//        for(int j=1; j<=M; j++)
//            printf("%d ",map[i][j]);
//        printf("\n");
//    }
//    printf("\n");
    printf("%d\n",result);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值