time limit per test1 second
memory limit per test256 megabytes inputstandard input outputstandard output 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.Input
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.
Output
Print one integer — the number of good positions for placing the spotlight.Examples
input 2 4 0 1 0 0 1 0 1 0 output 9 input 4 4 0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0 output 20 Note 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.【题目链接】:
【题解】
设f[i][j][4]表示从这个点往4个方向是不是有表演者(这个点是empty的情况下); 然后用记忆化搜索搞一波; 具体点; 从一个为empty的点开始往4个方向搜actor;如果搜到了actor,或者在途中遇到另外一个empty的点f[i][j][当前方向]为true;则也可以返回true;否则返回false; 统计答案就好; 直接用int即可不用LL; 【完整代码】#includeusing namespace std;const int MAXN = 1010;const int dx[4] = { 0,0,1,-1};const int dy[4] = { 1,-1,0,0};int n,m,ans = 0;int f[MAXN][MAXN][4];bool a[MAXN][MAXN];int can(int x,int y,int p){ if (x > n || y>m) return 0; if (x < 1 || y<1) return 0; if (f[x][y][p]!=-1) return f[x][y][p]; if (a[x][y]) return 1; f[x][y][p] = can(x+dx[p],y+dy[p],p); return f[x][y][p];}int main(){ // freopen("F:\\rush.txt","r",stdin); scanf("%d%d",&n,&m); for (int i = 1;i <= n;i++) for (int j = 1;j <= m;j++) scanf("%d",&a[i][j]); memset(f,255,sizeof(f)); for (int i = 1;i <= n;i++) for (int j = 1;j <= m;j++) if (!a[i][j]) { for (int k = 0;k <= 3;k++) if (can(i,j,k)) ans++; } printf("%d\n",ans); return 0;}