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 ...
随机推荐
- excel将单元格格式由数字转为文本
由于python读取excel数据时,整数总会变成浮点数,例如1会变成1.0,这时可以通过将excel里面的数字转化为文本,再读取出来就不会变成浮点数了.还有一种情况是excel里面的数字是由公式计算 ...
- 新博客地址: kuangbin.org
RT. 买了新域名,原来的kuangbin.net已经废了,数据已经移动到了kuangbin.org
- HDU 1285 确定比赛名次 拓扑排序模板题
http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <cstdio> #include <cstdlib> #inc ...
- man curl_easy_init(原创)
curl_easy_init(3) libcurl 手册 curl_easy_init(3) 名字 curl_easy_init - 开始一个简单的libcurl会话 梗概 #include < ...
- LeetCode OJ 114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- Markdown syntax guide and writing on MWeb
Philosophy Markdown is intended to be as easy-to-read and easy-to-write as is feasible.Readability, ...
- [问题解决]《GPU高性能编程CUDA实战》中第4章Julia实例“显示器驱动已停止响应,并且已恢复”问题的解决方法
以下问题的出现及解决都基于"WIN7+CUDA7.5". 问题描述:当我编译运行<GPU高性能编程CUDA实战>中第4章所给Julia实例代码时,出现了显示器闪动的现象 ...
- Python在Windows下安装第三方库浅谈
在用python编写代码时,往往需要用到第三方库,那么python如何去用第三方库呢,首先我们先来看看是如何安装的,方法可能会很多,但这边只介绍一种,其它请百度或google 比如asyncio,这里 ...
- expense KK [ɪkˋspɛns] DJ [iksˋpens]
https://tw.dictionary.yahoo.com/dictionary?p=expense expense 1 Dr.eye譯典通 KK [ɪkˋspɛns] DJ [iksˋpen ...
- NGUI BUG /各种坑
以下为:NGUI3.6具体使用过程中碰到的 各种BUG /各种坑 备忘 1.UIToggle 使用UIToggle组件实现多个tab标签互斥,如果是在代码中动态的设置显示某个tab标签对应的内容,则需 ...