F - Spotlights
CodeForces - 738BTheater 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.
InputThe 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.
OutputPrint one integer — the number of good positions for placing the spotlight.
Examples2 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的位置上,并且使得这个聚光灯能照到演员.如果有一个方向满足这个条件,那就算一种照射方式.
分析:按照题目意思,就是求每个0的四周有1的方向的和,那么一开始我就想遍历一下,然后顺理成章的就T在第七个样例.
于是得换种方法.
再分析:刚刚我们是先找0,然后再找有多少个1的方向,那么如果反一反呢(因为1比较少).也就是说先找1,然后再找四周有多少个零.这样试了试,(不用试了)第一个样例都不过不了,说明这个思路还不对,还要处理一下.
那么来看第一个样例,如果找1的周围0那么输出是10.多了一个,那么多了哪一个呢???仔细一点可以发现,左下角的0的右边边方向多了一个.那么这个是和(2,3)右边那个0重复的.
所以for循环要处理一下,一旦在找零的过程中遇到了1,那么就返回.
思路成了,要注意一下,别有多余的头文件,别用cin和cout.不然你会在第七个样例T到绝望...
代码:
#include<stdio.h>
#define M 1000+10
int a[M][M],n,m,ans;;
void Search(int x,int y)
{
for(int i=x+1;i<n;i++)
{
if(a[i][y])
break;
ans++;
}
for(int i=x-1;i>=0;i--)
{
if(a[i][y])
break;
ans++;
}
for(int j=y+1;j<m;j++)
{
if(a[x][j])
break;
ans++;
}
for(int j=y-1;j>=0;j--)
{
if(a[x][j])
break;
ans++;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j])
Search(i,j);
}
}
printf("%d\n",ans);
}
ps:2018年4月12日 17:08:30
这题比赛的时候一直T就没怎么多想了,回头一看学长的代码,才发现只差一点点转弯的思路,和一点小处理.
当初没做出来真可惜.
因为渴望知识而学习,非名利.