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. 移动端meta几个值的设置以及含义

    <!-- 为移动设备添加 viewport --> <meta name="viewport" content="width=device-width, ...

  2. css+ js 实现圆环时钟

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  3. ElasticSearch、Kibana Web管理

    ElasticSearch的Web管理 http://localhost:9200/ http://localhost:9200/cluster/health?pretty http://localh ...

  4. 在物理 Data Guard 中对异构主备系统的支持 (文档 ID 1602437.1)

    Data Guard中主数据库与物理备用数据库(Redo Apply)之间可以有什么差别?本说明针对重做应用和 Oracle Data Guard 12 发行版 1 进行了更新.它适用于 Oracle ...

  5. C# 正整数和非零正整数校验

    /// <summary> /// 1. 校验正整数(包含0) /// </summary> public static bool isInterger(string str) ...

  6. Windows学习总结(1)——win10系统最新快捷键汇总

    Win10新增功能快捷键大全: 贴靠窗口:Win + 左/右 >  Win + 上/下 > 窗口可以变为 1/4 大小放置在屏幕 4 个角落. 切换窗口:Alt + Tab(不是新的,但任 ...

  7. UVA 10306 e-Coins(全然背包: 二维限制条件)

    UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...

  8. LeetCode OJ Basic Calculator II

    Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...

  9. nls 字符编码文件对应的国家语言

    原文 http://ftp.twaren.net/cpatch/faq/tech/tech_nlsnt.txt * updated by Kii Ali, 12-11-2001 ftp://ftp.n ...

  10. C#打印日志的小技巧(转)

    https://www.cnblogs.com/jqg-aliang/p/5234206.html 打印日志的函数 开发中输出日志必不可少,在C#中输出多个不同类型参数的时候,需要连接符累加输出,很是 ...