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.
题意:
给你一个n*m的图,里面填满了0,1;
问你每一个0,他的上下左右4个方向上(可以不相邻)有没有1
解题思路
打code force的时候暴力枚举 2重循环加4个while循环没过。。。
然后陷入了二分查找的怪圈,后来突然想到 二分查找函数只适用于排好序的序列啊!!!!
比赛结束后灵光一现想到dp。。。。。
dp[i][j] 表示第 i 行 到第j个元素为止(加上他本身),之前有几个1
dp2[i][j] 表示第 i 列 到第j个元素为止(加上他本身),之前有几个1
那么每一行
dp[i][j] = dp[i][j-1]+1,g[i][j] ==1.
dp[i][j] = dp[i][j-1] , g[i][j] ==0.
列也是一样的
dp数组处理好了,怎么使用呢?
对于每个 == 0 的g[i][j]
左边有1, dp[i][j]>0;
右边有1,dp[i][m-1] - dp[i][j]>0;
上边有1,dp2[j][i]>0;
下边有1,dp2[j][n-1]-dp2[j][i]>0;
最后输出ans就可以啦
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
int a;
int n,m;
int ans = 0;
int g[1010][1010];///按行存这个图
int g2[1010][1010];///按列存(其实可以不用开这个数组存,直接在处理列的地方处理就好了)
int dp[1010][1010];///第i行加上他本身,之前有几个1
int dp2[1010][1010];///第i列加上他本身,之前有几个1
int main()
{
scanf("%d%d",&n,&m);
///按行存
for(int i =0 ;i<n;++i){
for(int j = 0;j<m;++j){
scanf("%d",&a);
g[i][j]=a;
}
}
///按列存(可省)
for(int j=0;j<m;++j){
for(int i=0;i<n;++i){
g2[j][i]=g[i][j];
}
}
///处理行
for(int i = 0 ;i<n;++i){///每行第一个元素就是他自身
if(g[i][0]==1) dp[i][0]=1;
else
dp[i][0] = 0;
}
for(int i = 0;i<n;++i){
for(int j = 1;j<m;++j){///第二个元素开始与前一个有关
if(g[i][j]==1) dp[i][j]=dp[i][j-1]+1;
else dp[i][j] = dp[i][j-1];
}
}
///处理列
for(int i = 0 ;i<m;++i){
if(g2[i][0]==1) dp2[i][0]=1;
else
dp2[i][0] = 0;
}
for(int i = 0;i<m;++i){
for(int j = 1;j<n;++j){
if(g2[i][j]==1) dp2[i][j]=dp2[i][j-1]+1;
else dp2[i][j] = dp2[i][j-1];
}
}
///处理结果
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(g[i][j]==1) continue;
///左边有没有1
if(dp[i][j]>0) ans++;
///右边有没有1
if(dp[i][m-1]-dp[i][j]>0) ans++;
///上边有没有1
if(dp2[j][i]>0) ans++;
///下边有没有1
if(dp2[j][n-1]-dp2[j][i]>0) ans++;
}
}
printf("%d",ans);
return 0;
}