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的矩形场。导演给你舞台的计划,谁的演员会遵循。对于每个单元格,在计划中说明在该单元格中是否有一个actor。
你会在舞台上放置一个聚光灯在一个良好的位置。聚光灯将投射光在四个方向之一(如果你从上面看舞台) - 左,右,上或下。因此,聚光灯的位置是它被放置的单元格和它发光的方向。
如果两个条件成立,则位置是好的:
1在单元格中没有放置聚光灯的演员;
2在聚光灯投射的方向上至少有一个演员。
计算放置聚光灯的良好位置的数量。如果位置单元或投影方向不同,则聚光的两个位置被认为是不同的。
先正着循环一遍,有1就把这一行这一列都都标记,遇到0,就看行跟列是不是被标记了,被标记了就说明前面或者上面有1,然后倒着来一次,看后面跟右面。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
int matrix[maxn][maxn], book1[maxn], book2[maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d", &matrix[i][j]);
if(matrix[i][j]) book1[i] = 1, book2[j] = 1;
else
{
if(book1[i]) ans++;
if(book2[j]) ans++;
}
}
memset(book1, 0, sizeof(book1));
memset(book2, 0, sizeof(book2));
for(int i = n; i >= 1; i--)
for(int j = m; j >= 1; j--)
{
if(matrix[i][j]) book1[i] = book2[j] = 1;
else
{
if(book1[i]) ans++;
if(book2[j]) ans++;
}
}
printf("%d\n", ans);
return 0;
}
直接用一个二维数组,看每一行的前几列有几个1,每一列前几行有几个1,这样看前面有没有1就是直接看sum的和,看后面就是用sum[n][j] - sum[i][j];
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e3 + 5;
int sum1[maxn][maxn], sum2[maxn][maxn],a[maxn][maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
scanf("%d", &a[i][j]);
sum1[i][j] = sum1[i-1][j] + a[i][j];
sum2[i][j] = sum2[i][j-1] + a[i][j];
}
int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
if(!a[i][j])
{
if(sum1[i][j]) ans++;
if(sum2[i][j]) ans++;
if(sum1[n][j]-sum1[i][j]) ans++;
if(sum2[i][m]-sum2[i][j]) ans++;
}
}
printf("%d\n", ans);
return 0;
}