Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cmath>
- #include <deque>
- using namespace std;
- ifstream cin("ccski.in");
- ofstream cout("ccski.out");
- #define INF 2000000001
- #define N_MAX 501
- int ml[]={1,0,-1,0};
- int mc[]={0,1,0,-1};
- int lee[N_MAX][N_MAX],v[N_MAX][N_MAX];
- bool mat[N_MAX][N_MAX];
- int main()
- {
- int n,m,i,j,l,c,cntd,cntc,max1,cs;
- cin>>n>>m;
- deque<pair<int,int>> mers;
- deque<int> cnt;
- for(i=1;i<=n;i++){
- for(j=1;j<=m;j++){
- cin>>v[i][j];
- lee[i][j]=INF;
- }
- }
- for(i=1;i<=n;i++){
- for(j=1;j<=m;j++){
- cin>>mat[i][j];
- if(mat[i][j]==1){
- cs++;
- mers.push_back({i,j});
- cnt.push_back(0);
- }
- }
- }
- while(!mers.empty()){
- cntd=cnt.front();
- pair <int,int> poz;
- poz=mers.front();
- mers.pop_front();
- cnt.pop_front();
- for(i=0;i<4;i++){
- l=poz.first+ml[i];
- c=poz.second+mc[i];
- if(l>0 && c>0 && l<=n && c<=m){
- cntc=max(cntd,abs(v[l][c]-v[poz.first][poz.second]));
- if(cntc<lee[l][c] && lee[l][c]<INF){
- lee[l][c]=cntc;
- mers.push_front({l,c});
- cnt.push_front(cntc);
- }else if(cntc<lee[l][c]){
- lee[l][c]=cntc;
- mers.push_back({l,c});
- cnt.push_back(cntc);
- }
- }
- }
- }
- max1=0;
- for(i=1;i<=n;i++){
- for(j=1;j<=m;j++){
- if(mat[i][j]==1 && lee[i][j]>max1){
- max1=lee[i][j];
- }
- }
- }
- if(cs==1)
- cout<<"0";
- else
- cout<<max1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement