Advertisement
mickypinata

PROG-T1058: Longest

Nov 22nd, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1000;
  5.  
  6. bool board[N + 10][N + 10];
  7. int mov[N + 10][N + 10][4];
  8. int Q, row, col;
  9.  
  10. int main(){
  11.  
  12.     scanf("%d", &Q);
  13.     for(int q = 1; q <= Q; ++q){
  14.         scanf("%d %d", &row, &col);
  15.         for(int i = 1; i <= row; ++i){
  16.             for(int j = 1; j <= col; ++j){
  17.                 char tmpChar;
  18.                 scanf(" %c", &tmpChar);
  19.                 if(tmpChar == '1'){
  20.                     board[i][j] = true;
  21.                 } else {
  22.                     board[i][j] = false;
  23.                 }
  24.             }
  25.         }
  26.         for(int i = 1; i <= row; ++i){
  27.             int cnt = 0;
  28.             for(int j = 1; j <= col; ++j){
  29.                 if(!board[i][j]){
  30.                     cnt = -1;
  31.                 }
  32.                 mov[i][j][0] = cnt;
  33.                 ++cnt;
  34.             }
  35.             cnt = 0;
  36.             for(int j = col; j >= 1; --j){
  37.                 if(!board[i][j]){
  38.                     cnt = -1;
  39.                 }
  40.                 mov[i][j][1] = cnt;
  41.                 ++cnt;
  42.             }
  43.         }
  44.         for(int i = 1; i <= col; ++i){
  45.             int cnt = 0;
  46.             for(int j = 1; j <= row; ++j){
  47.                 if(!board[j][i]){
  48.                     cnt = -1;
  49.                 }
  50.                 mov[j][i][2] = cnt;
  51.                 ++cnt;
  52.             }
  53.             cnt = 0;
  54.             for(int j = row; j >= 1; --j){
  55.                 if(!board[j][i]){
  56.                     cnt = -1;
  57.                 }
  58.                 mov[j][i][3] = cnt;
  59.                 ++cnt;
  60.             }
  61.         }
  62.  
  63.         int mx = 0;
  64.         for(int i = 1; i <= row; ++i){
  65.             for(int j = 1; j <= col; ++j){
  66.                 if(board[i][j]){
  67.                     mx = max(mx, max(mov[i][j][0], mov[i][j][1]) + max(mov[i][j][2], mov[i][j][3]));
  68.                 }
  69.             }
  70.         }
  71.         cout << mx + 1 << "\n";
  72.     }
  73.  
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement