zoukankan      html  css  js  c++  java
  • Codeforces #380 div2 B(729B) Spotlights

    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 good positions in this example.

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

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

    代码:

     1 #include <cstdio>
     2 #include <ctime>
     3 #include <cstdlib>
     4 #include <iostream>
     5 #include <cstring>
     6 #include <cmath>
     7 #include <queue>
     8 #include <algorithm>
     9 #define N 1005
    10 #define inf 1e18+5
    11 typedef long long ll;
    12 #define rep(i,n) for(i=0;i<n;i++)
    13 using namespace std;
    14 int i,j,k,m,n,t,cc,ans;
    15 int a[N][N],r[N],c[N],r2[N],c2[N];
    16 bool vis[N][N];
    17 char s;
    18 int  main()
    19 {
    20     while(scanf("%d %d",&n,&m)!=EOF){
    21         ans=0;
    22     //    memset(vis,0,sizeof(vis));
    23         memset(r,0,sizeof(r));
    24         memset(c,0,sizeof(c));
    25         memset(r2,0,sizeof(r2));
    26         memset(c2,0,sizeof(c2));
    27         memset(a,0,sizeof(a));
    28         for(i=0;i<n;i++){
    29             for(j=0;j<m;j++){
    30                 scanf("%d",&a[i][j]);
    31                 if(a[i][j]==1) {
    32                     r[i]=1;
    33                     c[j]=1;
    34                 }
    35                 if(r[i]==1){
    36                     if(a[i][j]==0){
    37                         
    38                         ans++;
    39                     }
    40                 }
    41                 if(c[j]==1){
    42                     if(a[i][j]==0){
    43                         ans++;
    44                     }
    45                 }
    46             }
    47         }
    48         for(i=n-1;i>=0;i--){
    49             for(j=m-1;j>=0;j--){
    50                 if(a[i][j]==1) {
    51                     r2[i]=1;
    52                     c2[j]=1;
    53                 }
    54                 if(r2[i]==1){
    55                     if(a[i][j]==0){
    56                         
    57                         ans++;
    58                     }
    59                 }
    60                 if(c2[j]==1){
    61                     if(a[i][j]==0){
    62                         ans++;
    63                     }
    64                 }
    65             }
    66         }
    67         printf("%d\n",ans);
    68     }
    69 
    70     return 0;
    71 }
  • 相关阅读:
    Maven笔记之面试题合集
    Maven笔记之核心概念及常用命令
    UML中的关系
    RocketMq核心概念
    linux安装rocketMq(包括安装maven,JDK)
    linux安装JDK,配置环境变量
    ASP.NET Core读取appsettings.json配置文件信息
    ASP.NET Core获取客户端IP地址
    ASP.NET Core根据环境切换NLog配置
    ASP.NET Core使用NLog记录日志
  • 原文地址:https://www.cnblogs.com/Crazycatmiao/p/6091371.html
Copyright © 2011-2022 走看看