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的位置,找到有几个方向上含有1;输出含有1的方向的数量;
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int sum[1005][1005][4];//定义三数组,代表每个点的每个方向上是否含有1;0为上方,1为左方,2为右方,3为下方;
int maps[1005][1005];
int main()
{
int m,n;
while(~scanf("%d %d",&n,&m))
{
int i,j,ans=0;
memset(sum,0,sizeof(sum));
memset(maps,0,sizeof(maps));
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&maps[i][j]);
if(maps[i][j]==1||sum[i-1][j][0]==1)//如果它本身是1或者他的上方是1,就使它为1;
sum[i][j][0]=1;
if(maps[i][j]==1||sum[i][j-1][1]==1)//如果它本身是1或者他的左方是1,就使它为1;
sum[i][j][1]=1;
}
}
for(i=n; i>=1; i--)
{
for(j=m; j>=1; j--)
{
if(maps[i][j]==1||sum[i+1][j][2]==1)//如果它本身是1或者他的下方是1,就使它为1;
sum[i][j][2]=1;
if(maps[i][j]==1||sum[i][j+1][3]==1)//如果它本身是1或者他的右方是1,就使它为1;
sum[i][j][3]=1;
}
}
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
if(maps[i][j]==0)
ans=ans+sum[i][j][0]+sum[i][j][1]+sum[i][j][2]+sum[i][j][3];//是每个0点的每个方向相加,有1的方向为1,否则为0;
}
printf("%d\n",ans);
}
}