F - Spotlights

原创 2018年04月17日 19:20:52

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.


题意:寻找 0 的 方向上 是否有1;
思路:因为按 0 寻找 是否有1,时间超限。所以可以逆向思维,按1在四个方向搜索0。

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int  tu[1010][1010];
int n,m;
static int step=0;
void Sea(int x,int y)
{
    int i;
   for(i=x-1;i>=0;i--)      //向上搜索
    {
        if(tu[i][y])      
              break;
        step++;
    }
    for(i=x+1;i<n;i++)  //向下
    {
        if(tu[i][y])
              break;
              step++;
    }
    for(i=y+1;i<m;i++)    //左
    {
        if(tu[x][i])
              break;
        step++;
    }
    for(i=y-1;i>=0;i--)   //右
    {
        if(tu[x][i])
              break;
    step++;
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        scanf("%d",&tu[i][j]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
             if(tu[i][j])      //按1搜索
              Sea(i,j);
    printf("%d\n",step);
    return 0;
}


CodeForces 738B - Spotlights(前缀和)

B. Spotlights time limit per test1 second memory limit per test256 megabytes inputstandard input ...
  • Snakeflute
  • Snakeflute
  • 2016-11-21 15:17:40
  • 212

F - Spotlights CodeForces - 738B

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which act...
  • Biuasm
  • Biuasm
  • 2018-04-10 23:02:27
  • 18

Codeforces 729B Spotlights(简单思维题)

B. Spotlights time limit per test 1 second memory limit per test 256 megabytes inpu...
  • zwj1452267376
  • zwj1452267376
  • 2016-11-21 02:41:34
  • 493

【Codeforces 729B Spotlights】

B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard ...
  • WYK1823376647
  • WYK1823376647
  • 2016-11-20 22:25:45
  • 369

Spotlights

Spotlights 一个spotlight是一个directional light和point light的组合。在world space中有一个坐标位置,但是光线只能照射到某个特定方向。另外...
  • chenjinxian_3D
  • chenjinxian_3D
  • 2016-07-08 17:50:20
  • 488

Codeforces Round #380 B Spotlights -dp

Codeforces Round #380 B Spotlights -dp Theater stage is a rectangular field of size n × m. The dir...
  • ly59782
  • ly59782
  • 2016-11-20 23:46:55
  • 245

B. Spotlights

B. Spotlights time limit per test 1 second memory limit per test 256 megabytes inpu...
  • qq_35252878
  • qq_35252878
  • 2016-11-21 23:59:25
  • 91

Codeforce 729B. Spotlights

B. Spotlights time limit per test 1 second memory limit per test 256 megabytes inpu...
  • DTL66
  • DTL66
  • 2016-11-21 22:28:09
  • 210

codeforces B. Spotlights

#include using namespace std; int map1[1005][1005]; vectorrow[1005]; vectorcol[1005]; int main() { ...
  • ScorpioN___
  • ScorpioN___
  • 2016-11-20 22:19:26
  • 83
收藏助手
不良信息举报
您举报文章:F - Spotlights
举报原因:
原因补充:

(最多只允许输入30个字)