Codeforces #380 div2 B(729B) Spotlights
1 second
256 megabytes
standard input
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.
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.
Print one integer — the number of good positions for placing the spotlight.
2 4
0 1 0 0
1 0 1 0
9
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
20
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.
题目大意: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的更多相关文章
- 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 ...
- Codeforces #380 div2 E(729E) Subordinates
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 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 ...
- 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 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- loadrunner 编写socket脚本实例(附服务端实现)
一.socket背景知识 这个咱就不废话了,网上一搜一大堆 二.本实例实现的功能 服务端接收客户端发送的字符串,并返回"5678succ"共8个字符 三.服务端实现(java代码) ...
- Android(Intent 学习)
Intent 是一个消息传递对象,Intent可以通过多种方式促进组件之间的通信,基本的三种用例: 启动Acitivity: Activity表示应用中的一个屏幕,通过将Intent传递给startA ...
- C3属性的轮播图(持续更新)
天气好冷,都不想写代码.就先写个没有焦点的轮播图,过两天在补全. 用的是CSS3的属性 过渡transition 和 转换 transfrom:translateX() 只做了轮播和 鼠标进入停止轮播 ...
- cell当中的按钮如何获得cell内容
cell当中的btn添加方法 [cell.btn addTarget:self action:@selector(btnClickedwith:) forControlEvents:UIControl ...
- 2 Add Two Numbers
// Java public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return add(l1, l2, false); } priva ...
- java程序故障排查脚本之——CPU占用高
root@ubuntu-B85M-D3H:~/tmp# cat java_Analy.sh #!/bin/bash T=`ps -mp $1 -o THREAD,tid,time|sort -k 2 ...
- SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0
1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度
- linux shell执行中需要交互输入回车,Yes/NO Y/N
最近写自动安装脚本遇到redis-server安装的时候,需要输入3个回车,对此尝试无果,最后google比较满意的解决办法: shell 脚本需要交互,比如输入回车,输入YES/NO Y/N之类进行 ...
- poj -- 1042 Gone Fishing(枚举+贪心)
题意: John现有h个小时的空闲时间,他打算去钓鱼.钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他 ...
- 用grunt进行前端工程化之路
我们的项目wecash4.0的前端构建考虑过用fis和grunt. 目录: 前期调研:fis vs grunt vs gulp? 一.安装grunt和项目. fis是百度fex研发的构建工具,非常方便 ...