can someone help me to know what is wrong in my solution of kickstart 2021 a , 2nd question?

Revision en1, by non--stop, 2022-01-09 18:06:54

question

#include <iostream>
#include<math.h>

using namespace std;

int arr[1001][1001];
int dp[1001][1001][4];

int count (int x, int y) {
  return max(min(x,y/2)-1,0)+ max(min(x/2,y)-1,0);
}

int fun(int r, int c) {
  int ans = 0;
  for(int i=1;i<=r;i++){
    for(int j=1;j<=c;j++){
      for(int k=0;k<4;k++){
        dp[i][j][k] = 0;
      }
    }
  }
  
  for(int i=1;i<=r;i++){
    for(int j=1;j<=c;j++){
      if(!arr[i][j]) continue;
      dp[i][j][3] = dp[i][j-1][3] + 1;// for left
      dp[i][j][0] = dp[i-1][j][0] + 1;// for top
    }
  }

  for(int i=r;i>=1;i--){
    for(int j=c;j>=1;j--){
      if(!arr[i][j]) continue;
      dp[i][j][1] = dp[i][j+1][1] + 1;// for right
      dp[i][j][2] = dp[i+1][j][2] + 1;// for bottom
    }
  }

  for(int i=1;i<=r;i++){
    for(int j=1;j<=c;j++){
      if(!arr[i][j]) continue;
      // all 4 directions
      ans += count(dp[i][j][0],dp[i][j][1]);// top,right
      ans += count(dp[i][j][1],dp[i][j][2]);//right,bottom
      ans += count(dp[i][j][2],dp[i][j][3]);// bottom,left
      ans += count(dp[i][j][3],dp[i][j][0]);// left, top
    }
  }
  return ans;
}
int main() {
  int  t;
  cin>> t;
  int r,c;
  for(int k=1;k<=t;k++) {
      cin>>r>>c;
      for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
          cin>>arr[i][j];
        }
      }
    cout<<"Case #"<<k<<": "<<fun(r,c)<<"\n";
  }
 return 0;
}  

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English non--stop 2022-01-09 18:06:54 1653 Initial revision (published)