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;



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 1010;
  4. const int dx[4] = {0,0,1,-1};
  5. const int dy[4] = {1,-1,0,0};
  6. int n,m,ans = 0;
  7. int f[MAXN][MAXN][4];
  8. bool a[MAXN][MAXN];
  9. int can(int x,int y,int p)
  10. {
  11. if (x > n || y>m)
  12. return 0;
  13. if (x < 1 || y<1)
  14. return 0;
  15. if (f[x][y][p]!=-1)
  16. return f[x][y][p];
  17. if (a[x][y])
  18. return 1;
  19. f[x][y][p] = can(x+dx[p],y+dy[p],p);
  20. return f[x][y][p];
  21. }
  22. int main()
  23. {
  24. // freopen("F:\\rush.txt","r",stdin);
  25. scanf("%d%d",&n,&m);
  26. for (int i = 1;i <= n;i++)
  27. for (int j = 1;j <= m;j++)
  28. scanf("%d",&a[i][j]);
  29. memset(f,255,sizeof(f));
  30. for (int i = 1;i <= n;i++)
  31. for (int j = 1;j <= m;j++)
  32. if (!a[i][j])
  33. {
  34. for (int k = 0;k <= 3;k++)
  35. if (can(i,j,k))
  36. ans++;
  37. }
  38. printf("%d\n",ans);
  39. return 0;
  40. }

【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. SQl 行转列,列转行 (PIVOT,UPIVOT)

    1.列转行 表t_pivot 转后效果   方法 1 (常用方法 case when) SELECT     buydate, SUM(CASE WHEN type = '生活' THEN typec ...

  2. Android官方文档翻译——Fragment生命周期

    网上有的博客写得太乱 不如自己翻译官方文档 Lifecycle 生命周期 Though a Fragment's lifecycle is tied to its owning activity, i ...

  3. js --- 递归结构图

    // 用递归 来求 5 的阶乘 // n! = n * (n-1)! // 定义一个函数,用于求 n 的阶乘 function func(n) { ) { ; } // func(n-1) 因为传递的 ...

  4. javafx checkbox

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  5. Kinect 开发 —— 图片浏览

    总体思路 首先运用WPF编写一个简单的支持多点触控的图片浏览程序,这方面您可以参看MSDN上的这篇文章,上面有代码,可能需要FQ才能下载.中文的话,您可以参考Gnie同学关于在WPF上面多点触屏(Mu ...

  6. 自己动手写SSO(单点登录)

    SSO在我们的应用中非常常见,例如我们在OA系统登录了,我们就可以直接进入采购系统,不需要再登录了,这样使我们非常方便.现在网上也有很多实现方法,于是乎我也想写一个看看.我主要用到的是cookie的机 ...

  7. 洛谷 P1256 显示图像

    P1256 显示图像 题目描述 古老的显示屏是由N×M个像素(Pixel)点组成的.一个像素点的位置是根据所在行数和列数决定的.例如P(2,1)表示第2行第1列的像素点.那时候,屏幕只能显示黑与白两种 ...

  8. 细说 iOS 消息推送

    APNS的推送机制 与Android上我们自己实现的推送服务不一样,Apple对设备的控制很严格.消息推送的流程必需要经过APNs: 这里 Provider 是指某个应用的Developer,当然假设 ...

  9. dom4j解析xml文件和字符串

    转自:http://www.cnblogs.com/black-spike/p/9776180.html 最近在工作中,需要调别的接口,接口返回的是一个字符串,而且内容是xml格式的,结果在解析jso ...

  10. Zabbix监控Tomcat,Redis

    一 Tomcat监控 1.1.1 Tomcat 端配置 JMX 编辑catalina.sh文件,配置如下: CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.m ...