题意:
剧场舞台是尺寸为n×m的矩形场。导演给你舞台的计划,谁的演员会遵循。对于每个单元格,在计划中说明在该单元格中是否有一个actor。
你会在舞台上放置一个聚光灯在一个良好的位置。聚光灯将投射光在四个方向之一(如果你从上面看舞台) - 左,右,上或下。因此,聚光灯的位置是它被放置的单元格和它发光的方向。
如果两个条件成立,则位置是好的:
1在单元格中没有放置聚光灯的演员;
2在聚光灯投射的方向上至少有一个演员。
计算放置聚光灯的良好位置的数量。如果位置单元或投影方向不同,则聚光的两个位置被认为是不同的。
思路:
从头往后扫的时候计算左边和上边的数量,再一次倒着扫计算下边和右边的数量即可。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int pic[maxn][maxn];
bool bookRow[maxn], bookCol[maxn];
int main(void)
{
int row, col;
while(cin >> row >> col)
{
memset(bookRow, 0, sizeof(bookRow));
memset(bookCol, 0, sizeof(bookCol));
int ans = 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
{
scanf("%d", &pic[i][j]);
if(pic[i][j]) bookRow[i] = bookCol[j] = 1;
else
{
if(bookRow[i]) ans++;
if(bookCol[j]) ans++;
}
}
memset(bookRow, 0, sizeof(bookRow));
memset(bookCol, 0, sizeof(bookCol));
for(int i = row-1; i >= 0; i--)
for(int j = col-1; j >= 0; j--)
{
if(pic[i][j]) bookRow[i] = bookCol[j] = 1;
else
{
if(bookRow[i]) ans++;
if(bookCol[j]) ans++;
}
}
printf("%d\n", ans);
}
return 0;
}