B. Spotlights
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.

题目大意:0 1组成的矩阵,对每个0,检查它的上下左右四个方向是否有1,每一个有1的方向都会使答案+1.

做法:从前往后扫一遍,扫描的时候如果a[i][j]为1,则记录该行该列有1,之后再遇到0的时候看这一行这一列是否有过1,如果有就答案++,相当于考虑了上和左的情况,再从后往前扫一遍,同理,相当于考虑了下和右的情况。

代码:

 #include <cstdio>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
#define N 1005
#define inf 1e18+5
typedef long long ll;
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
int i,j,k,m,n,t,cc,ans;
int a[N][N],r[N],c[N],r2[N],c2[N];
bool vis[N][N];
char s;
int main()
{
while(scanf("%d %d",&n,&m)!=EOF){
ans=;
// memset(vis,0,sizeof(vis));
memset(r,,sizeof(r));
memset(c,,sizeof(c));
memset(r2,,sizeof(r2));
memset(c2,,sizeof(c2));
memset(a,,sizeof(a));
for(i=;i<n;i++){
for(j=;j<m;j++){
scanf("%d",&a[i][j]);
if(a[i][j]==) {
r[i]=;
c[j]=;
}
if(r[i]==){
if(a[i][j]==){ ans++;
}
}
if(c[j]==){
if(a[i][j]==){
ans++;
}
}
}
}
for(i=n-;i>=;i--){
for(j=m-;j>=;j--){
if(a[i][j]==) {
r2[i]=;
c2[j]=;
}
if(r2[i]==){
if(a[i][j]==){ ans++;
}
}
if(c2[j]==){
if(a[i][j]==){
ans++;
}
}
}
}
printf("%d\n",ans);
} return ;
}

Codeforces #380 div2 B(729B) Spotlights的更多相关文章

  1. Codeforces Round #380 (Div. 2)/729B Spotlights 水题

    Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which act ...

  2. Codeforces #380 div2 E(729E) Subordinates

    E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. Codeforces #380 div2 D(729D) Sea Battle

    D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  6. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  7. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  8. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  9. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

随机推荐

  1. C语言file相关函数学习

    1.errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); 注:fopen_s能过创建文件,但无法创建目录 打 ...

  2. javax/javaee-api/ Maven依赖

    <dependency>    <groupId>javax</groupId>    <artifactId>javaee-api</artif ...

  3. ACM算法模板

    旧版模板下载链接: here 新版的模板目前不提供电子版,正在抽时间做一些修正以及添加一些新内容. 新模板如有需要纸质版的,可以自付打印费进行打印.购买链接:https://weidian.com/i ...

  4. 第一次到IT公司上班!

    今日是自个的首次正式到IT公司进行作业,感触也是别有一番兴趣!如今就让自个回味下第一天的作业经历吧! 我上班的公司叫西安西科软件技术有限公司,第一天上班的缘故,早上起得很早,差不多六点半还没到就起床洗 ...

  5. POJ1236 network of schools

    100个学校,有单向网络连接,从而分享软件. 问题一:几个学校得到软件就可使所有学校都得到? 问题二:再加几条单向网络可以使得"任意学校得到软件,就可使得所有学校都有软件"? -- ...

  6. VoLTE 注册流程

    1.开关按钮位置:   设置--> 更多--> 移动网络--> 增强型4G LTE模式 2.该设置开关使用了SwitchPreference控件,addEnhanced4GLteSw ...

  7. database link远程链接数据库

    --授权创建.删除dblink GRANT CREATE [PUBLIC] DATABASE LINK,DROP [PUBLIC] DATABASE LINK TO canco; --查看数据库GLO ...

  8. silverlight控件阴影效果示例

    <ScrollViewer MaxHeight="400" VerticalScrollBarVisibility="Auto" HorizontalSc ...

  9. Python3利用BeautifulSoup4抓取站点小说全文的代码

    再写一个用BeautifulSoup抓站的工具,体会BeautifulSoup的强大. 根据小说索引页获取小说全部章节内容并在本地整合为小说全文.不过不是智能的,不同的站点对代码需要做相应的修改. # ...

  10. Linux下SVN客户端安装及使用

    转载自:http://www.linuxidc.com/Linux/2015-01/111748.htm 不想自己写了,这个写的挺全的,我就按这个步骤走的,呵呵 非常感谢作者 环境说明: 系统版本:C ...