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.

【题目链接】:http://codeforces.com/contest/738/problem/B

【题解】



设f[i][j][4]表示从这个点往4个方向是不是有表演者(这个点是empty的情况下);

然后用记忆化搜索搞一波;

具体点;

从一个为empty的点开始往4个方向搜actor;如果搜到了actor,或者在途中遇到另外一个empty的点f[i][j][当前方向]为true;则也可以返回true;否则返回false;

统计答案就好;

直接用int即可不用LL;



【完整代码】

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1010;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0}; int n,m,ans = 0;
int f[MAXN][MAXN][4];
bool a[MAXN][MAXN]; int can(int x,int y,int p)
{
if (x > n || y>m)
return 0;
if (x < 1 || y<1)
return 0;
if (f[x][y][p]!=-1)
return f[x][y][p];
if (a[x][y])
return 1;
f[x][y][p] = can(x+dx[p],y+dy[p],p);
return f[x][y][p];
} int main()
{
// freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&m);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
scanf("%d",&a[i][j]);
memset(f,255,sizeof(f));
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (!a[i][j])
{
for (int k = 0;k <= 3;k++)
if (can(i,j,k))
ans++;
}
printf("%d\n",ans);
return 0;
}

【47.76%】【Round #380B】Spotlights的更多相关文章

  1. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  2. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  3. POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. 【UOJ#76】【UR #6】懒癌(动态规划)

    [UOJ#76][UR #6]懒癌(动态规划) 题面 UOJ 题解 神....神仙题. 先考虑如果是完全图怎么做... 因为是完全图,所以是对称的,所以我们只考虑一个有懒癌的人的心路历程. 如果只有一 ...

  5. 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)

    cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...

  6. 【BestCoder】【Round#41】

    枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...

  7. 【Mail.Ru Cup 2018 Round 2 A】 Metro

    [链接] 我是链接,点我呀:) [题意] [题解] 1:一直往右走的情况. 2:中间某个地方中转 (不会出现超过1次的转弯. (如果超过了和1次是等价的 [代码] #include <bits/ ...

  8. 【Mail.Ru Cup 2018 Round 2 B】 Alice and Hairdresser

    [链接] 我是链接,点我呀:) [题意] [题解] 因为只会增加. 所以. 一开始暴力算出来初始答案 每次改变一个点的话. 就只需要看看和他相邻的数字的值就好. 看看他们是不是大于l 分情况增加.减少 ...

  9. 【Mail.Ru Cup 2018 Round 2 C】 Lucky Days

    [链接] 我是链接,点我呀:) [题意] [题解] 题解的作者: manish_joshi 对于任意一个k 因为那条直线(关于x,y的方程可以看出一条直线)的斜率>= 所以肯定会经过第一象限. ...

随机推荐

  1. idle-实现清屏

    最近在学习python的时候,需要用到ubuntu的python idle.这个工具可以测试python语法.但是呢,在使用的过程中遇到了一个问题.就是随着你的输入,你会发现这个输入会停留在这个界面的 ...

  2. Scott Hanselman的问题-3

    .Net程序员面试 中级篇 (回答Scott Hanselman的问题)   继<.Net 程序员面试 C# 语言篇 (回答Scott Hanselman的问题)>跟<.Net程序员 ...

  3. HTML5多维度数据分析

    详情:http://echarts.baidu.com/index.html

  4. JS错误记录 - dom操作 - 排序

    本次练习错误总结: 1. for循环要套到按钮的onclick里面,否则onclick点击事件无法依次执行. 2. var n1, var n2 这两个变量是arr.sort排序使用的,所以应该放在s ...

  5. 微信小程序从零开始开发步骤(一)搭建开发环境

    从零到有写一个小程序系列专题,很早以前就想写来分享,但由于项目一直在进展,没有过多的时间研究技术,现在可以继续分享了. 1:注册 用没有注册过微信公众平台的邮箱注册一个微信公众号, 申请帐号 ,网址: ...

  6. 【Educational Codeforces Round 36 D】 Almost Acyclic Graph

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包 ...

  7. hdoj-1289-A Bug&#39;s Life【种类并查集】

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. JavaScript全讲-架构原则解析

    因为近期一直在忙,非常久没有更新,见谅. 上篇我们讲完JavaScript函数式编程的特性,今天我们就来聊聊JavaScript中的架构. 提到JavaScript架构.非常多人会认为不可思议,由于架 ...

  9. 带你走进EJB--EJB和Spring对比(转)

    http://blog.csdn.net/jnqqls/article/details/17723417 通过对EJB系列的总结和学习我们已经对EJB有了基本的了解,但是为了更进一步的去深入学习EJB ...

  10. asp.net 前后台数据交互方式(转)

    https://blog.csdn.net/luckyrass/article/details/38758007 一.前台直接输出后台传递的数据 后台代码: // .aspx.cs public st ...