Advertisement
Patrickmeme

CCsky

Jun 11th, 2023
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <fstream>
  2. #include <cmath>
  3. #include <deque>
  4. using namespace std;
  5.  
  6. ifstream cin("ccski.in");
  7. ofstream cout("ccski.out");
  8.  
  9. #define INF 2000000001
  10. #define N_MAX 501
  11.  
  12.  
  13. int ml[]={1,0,-1,0};
  14. int mc[]={0,1,0,-1};
  15.  
  16. int lee[N_MAX][N_MAX],v[N_MAX][N_MAX];
  17. bool mat[N_MAX][N_MAX];
  18.  
  19. int main()
  20. {
  21.     int n,m,i,j,l,c,cntd,cntc,max1,cs;
  22.     cin>>n>>m;
  23.     deque<pair<int,int>> mers;
  24.     deque<int> cnt;
  25.     for(i=1;i<=n;i++){
  26.         for(j=1;j<=m;j++){
  27.             cin>>v[i][j];
  28.             lee[i][j]=INF;
  29.         }
  30.     }
  31.     for(i=1;i<=n;i++){
  32.         for(j=1;j<=m;j++){
  33.             cin>>mat[i][j];
  34.             if(mat[i][j]==1){
  35.                 cs++;
  36.                 mers.push_back({i,j});
  37.                 cnt.push_back(0);
  38.             }
  39.         }
  40.     }
  41.     while(!mers.empty()){
  42.         cntd=cnt.front();
  43.         pair <int,int> poz;
  44.         poz=mers.front();
  45.         mers.pop_front();
  46.         cnt.pop_front();
  47.         for(i=0;i<4;i++){
  48.             l=poz.first+ml[i];
  49.             c=poz.second+mc[i];
  50.             if(l>0 && c>0 && l<=n && c<=m){
  51.                 cntc=max(cntd,abs(v[l][c]-v[poz.first][poz.second]));
  52.                 if(cntc<lee[l][c] && lee[l][c]<INF){
  53.                     lee[l][c]=cntc;
  54.                     mers.push_front({l,c});
  55.                     cnt.push_front(cntc);
  56.                 }else if(cntc<lee[l][c]){
  57.                     lee[l][c]=cntc;
  58.                     mers.push_back({l,c});
  59.                     cnt.push_back(cntc);
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     max1=0;
  65.     for(i=1;i<=n;i++){
  66.         for(j=1;j<=m;j++){
  67.             if(mat[i][j]==1 && lee[i][j]>max1){
  68.                 max1=lee[i][j];
  69.             }
  70.         }
  71.     }
  72.     if(cs==1)
  73.         cout<<"0";
  74.     else
  75.         cout<<max1;
  76.     return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement