SuitNdtie

Longest

Apr 12th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include<stdio.h>
  2. int max(int a,int b){
  3.     return (a > b ? a : b);
  4. }
  5. int max4(int a,int b,int c,int d){
  6.     return max(max(a,b),max(c,d));
  7. }
  8. int main(){
  9.     int T;
  10.     scanf("%d",&T);
  11.     for(int t = 0 ; t < T ; t ++){
  12.         int n,m;
  13.         scanf("%d %d",&n,&m);
  14.         char board[n][m+1];
  15.         for(int i=0;i<n;i++){
  16.             scanf("%s",board[i]);
  17.         }
  18.         int ngurUp[n][m];
  19.         for(int i=0;i<n;i++)for(int j=0;j<m;j++)ngurUp[i][j] = (board[i][j] == '1' ? i : -1);
  20.         for(int j=0;j<m;j++){
  21.             for(int i=1;i<n;i++){
  22.                 if(ngurUp[i-1][j] != -1 && ngurUp[i][j] != -1){
  23.                     ngurUp[i][j] = ngurUp[i-1][j];
  24.                 }
  25.             }
  26.         }
  27.         int ngurDown[n][m];
  28.         for(int i=0;i<n;i++)for(int j=0;j<m;j++)ngurDown[i][j] = (board[i][j] == '1' ? i : -1);
  29.         for(int j=0;j<m;j++){
  30.             for(int i=n-2;i>=0;i--){
  31.                 if(ngurDown[i+1][j] != -1 && ngurDown[i][j] != -1){
  32.                     ngurDown[i][j] = ngurDown[i+1][j];
  33.                 }
  34.             }
  35.         }
  36.         int ans = -2e9;
  37.         for(int i=0;i<n;i++){
  38.             int temp = 0;
  39.             for(int j=0;j<m;j++){
  40.                 temp++;
  41.                 temp*=board[i][j] - '0';
  42.                 if(temp == 0)continue;
  43.                 ans = max4(ans,temp,temp + (i - ngurUp[i][j]),temp + (ngurDown[i][j] - i));
  44.             }
  45.         }
  46.         for(int i=0;i<n;i++){
  47.             int temp = 0;
  48.             for(int j=m-1;j>=0;j--){
  49.                 temp++;
  50.                 temp*=board[i][j] - '0';
  51.                 if(temp == 0)continue;
  52.                 ans = max4(ans,temp,temp + (i - ngurUp[i][j]),temp + (ngurDown[i][j] - i));
  53.             }
  54.         }
  55.         printf("%d\n",ans);
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment