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 ...
随机推荐
- java io流之BufferReader&BufferedWriter
BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高 ...
- python——请求服务器(http请求和https请求)
一.http请求 1.http请求方式:get和post get一般用于获取/查询资源信息,在浏览器中直接输入url+请求参数点击enter之后连接成功服务器就能获取到的内容,post请求一般用于更新 ...
- C++常用输出 cout、cerr、clog
三者在C++中都是标准IO库中提供的输出工具: cout:写到标准输出的ostream对象: cerr:输出到标准错误的ostream对象,常用于程序错误信息: clog:也是输出标准错误流(这点儿和 ...
- 使用NSScanner,将十六进制字符串转十进制
例子: NSString * numStr = [@"0x110" substringFromIndex:2]; unsigned long long result = 0; N ...
- 07-JAVA继承与接口
动手动脑: 源程序: public class FuGai { public static void main(String[] args) { // TODO Auto-generated meth ...
- export LD_LIBRARY_PATH=/opt/gtk/lib:$LD_LIBRARY_PATH
如题,临时修改程序运行时动态库的搜索路径,平时经常会用到,记录之!
- 引用模板中的类型时,切记要加上typename声明!!
如题,发现实际操作中太容易忘记了,导致一些莫名其妙的编译错误,故在此记录!
- python模拟登陆知乎并爬取数据
一些废话 看了一眼上一篇日志的时间 已然是5个月前的事情了 不禁感叹光阴荏苒其实就是我懒 几周前心血来潮想到用爬虫爬些东西 于是先后先重写了以前写过的求绩点代码 爬了草榴贴图,妹子图网,后来想爬婚恋网 ...
- Android.mk 基本应用
如果是在android源码里面编译我们自己的应用,就需要这个android.mk文件,这个文件就告诉android系统应用如何来编译这个应用以及这个应用它所依赖哪些文件等等信息.我对android.m ...
- 未添加document.ready产生的BUG
今天在框架里使用superslide插件时,在javascript部分 <script type="text/javascript"> jQuery(".yj ...