F - Spotlights

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;
}


【讨论】if(function f())console.log(f);

04-12

[code=javascript]if(function f())console.log(f);[/code]rn最开始我以为会出现function f(),结果却是f is not defined,同样情况,适用于while,switch,forrnJS中没有块级作用域(花括号内),而是使用了函数作用域(函数体内),这是权威指南里的讲的rn所以我的理解,if()是没有自己的作用域的,但是按照这个例子来看,if是有自己的作用域的,或者说[color=#FF0000]()有自己的作用域[/color],就是()内(不知道这么说准不准确,虽然好像没有什么实际用处,而且if(function f())这种写法貌似也没有什么实际意义)rn按照这个理解测试了一下for:rn[code=javascript]var i = 0;for(function f()i++;;i<5;f())console.log(i)[/code]rn没想到结果仍然是f is not defined,不是很理解rnstackoverflow上找到了[url=http://stackoverflow.com/questions/14218779/function-names-defined-as-parameters-to-a-function-call-arent-hoisted-why-not][/url]rn我的英文很差,所以只能大概理解里面的回答,跟函数声明和函数表达式有关系,另外跟()也是有关系的rn但是,我仍然不能理解for循环的情况,毕竟如果是正常情况下:rn[code=javascript]function f();f()[/code]rn是没有问题的,那为什么加到for循环中出现找不到f呢?思考了一会想到for的运算流程rn[code=javascript]for ([initialization]; [condition]; [expression])rn statement[/code]rn进入for后是initialization-->condition-->statement-->expression-->condition-->statement...rn也就是说是先进入了statement之后才会运算expression,而在上面的测试代码中,第一次进入statement时就已经找不到函数f了,因为在()外面,所以再运算expression时会提示f is not definedrn好吧,至此疑问解开,虽然不知道理解的对不对,但是勉强说服自己了[img=http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/2.gif][/img]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试