zoukankan      html  css  js  c++  java
  • Spotlights

    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.

    分析:预处理前缀和,模拟;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn][maxn],b[maxn][maxn];
    ll ans;
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)rep(j,1,m)scanf("%d",&a[i][j]);
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(i,1,n)
        {
            rep(j,1,m)
            {
                b[i][j]+=b[i][j-1];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(i,1,n)
        {
            for(j=m;j>=1;j--)
            {
                b[i][j]+=b[i][j+1];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(j,1,m)
        {
            for(i=n;i>=1;i--)
            {
                b[i][j]+=b[i+1][j];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(j,1,m)
        {
            rep(i,1,n)
            {
                b[i][j]+=b[i-1][j];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        printf("%lld
    ",ans);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    fetch的优点
    gitignore不起作用
    css动画和js动画区别
    工业家居气象空气环境质量监测仪记录数据甲醛PM2.5二氧化碳大气压温湿度
    摆脱淘宝、京东、拼多多内部引流消费规则,自建网站利用其完成支付
    语音朗读模块TTS文本变量实时转语音朗读科大讯飞XFS5152CE芯片AI
    PCB altium designer AD10 AD20 导出DWG CAD文件 过孔问题
    【Creator3】如何在3D场景中实现炫酷传送门,和简单的小地图功能,RenderTexture技术应用
    B站视频:【Creator3】好玩的编队代码 魔性排列停不下来 附源码及出处
    B站视频:《四图猜词》 Part3 | CocosCreator游戏开发教程
  • 原文地址:https://www.cnblogs.com/dyzll/p/6095586.html
Copyright © 2011-2022 走看看