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. MySQL出现Access denied for user 'root'@'%' to database 'netai_test'问题

    访问数据库时报错信息 Access denied for user 'root'@'%' to database 'netai_test' 原因:这是由于创建数据库后没有对用户授权,使用户可以访问数据 ...

  2. 【Android端 APP 启动时长获取】启动时长获取方案及具体实施

    一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分 ...

  3. 十二 .ocBlock

    NSProxy是一个不继承NSObject的根类(尽管它遵守NSObject协议),调用[[NSProxy alloc]init]将会产生一个运行异常. 所幸,oc引入被称为“块代码”的第一级函数的概 ...

  4. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  5. SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0

    1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度

  6. JavaWeb 学习0010-今日问题 2016-12-3

    2016-12-3 1. 今天要做的第一个问题就是,怎么把网页变得好看点: addStudent/listStudent页面都有改动 其中 list页面还有了<c:if 的语句这是还没练习过得知 ...

  7. setNeedsDisplay和setNeedsLayout

    1,UIView的setNeedsDisplay和setNeedsLayout方法 首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到  UI ...

  8. 大批量GPS坐标转百度坐标

    一. 百度地图API大批量转换时有数量限制,一个一个转.  用到的方法接口    /**      源坐标 格式:经度,纬度;经度,纬度… 最多支持100个;      源坐标类型:默认为1,即GPS ...

  9. ZKM混淆工具

    原创文章,尊重劳动,转载请标明出处 ZKM 介绍 一般使用步骤 直接使用 ZKM 脚本 使用 GUI 工具混淆,同时生成 ZKM 脚本 参考 ZKM 介绍 zkm 是一款付费的代码混淆工具. 一般使用 ...

  10. mysql Statement violates GTID consistency 的坑

    今天项目迁移,重新换了一个数据库版本,然后问题来了,原本运行正常的程序迁移过来之后就是不能正常运行,后台报错如下: update tbl_user_info set -- 强制下架 mv_count ...