Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- ll n,m;
- ll hit[105][105];
- ll wat[105][105];
- bool vis1[105][105];
- bool vis2[105][105];
- vector<ll>vokhit;
- ll cka,cpa;
- void dfs(int r,int c)
- {
- if(r>n || r<1 || c<1 || c>m)
- {
- wat[cka][cpa]=0;
- return;
- }
- if(vis1[r][c])
- return;
- if(r==1 || r==n || c==1 || c==m)
- {
- wat[cka][cpa]=0;
- return;
- }
- vis1[r][c]=1;
- ll loki = hit[r][c+1];
- ll poki = hit[r][c];
- if(loki<=poki)
- {
- dfs(r,c+1);
- }
- if(hit[r+1][c]<=hit[r][c])
- {
- dfs(r+1,c);
- }
- if(hit[r-1][c]<=hit[r][c])
- {
- dfs(r-1,c);
- }
- if(hit[r][c-1]<=hit[r][c])
- {
- dfs(r,c-1);
- }
- }
- int pok(int r,int c)
- {
- if(r==1 || r==n || c==1 || c==m)
- return 1;
- return 0;
- }
- ll dfs2(int r,int c)
- {
- if(vis2[r][c])
- return INF;
- if(r==1 || r==n || c==1 || c==m)
- {
- return hit[r][c];
- }
- if(wat[r][c])
- {
- vokhit.pb(hit[r][c]);
- }
- else
- {
- return hit[r][c];
- }
- vis2[r][c]=1;
- ll mni = INF;
- mni = min(mni,dfs2(r+1,c));
- mni = min(mni,dfs2(r-1,c));
- mni = min(mni,dfs2(r,c+1));
- mni = min(mni,dfs2(r,c-1));
- return mni;
- }
- int main()
- {
- //freopen("out.txt","w",stdout);
- int tc;
- sfi(tc);
- while(tc--)
- {
- scanf("%lld %lld",&n,&m);
- loop(i,1,n)
- {
- loop(j,1,m)
- {
- wat[i][j]=1;
- }
- }
- loop(r,1,n)
- {
- loop(c,1,m)
- {
- cin>>(hit[r][c]);
- }
- }
- ms(vis1,false);
- loop(r,1,n)
- {
- loop(c,1,m)
- {
- ms(vis1,false);
- cka = r,cpa =c;
- dfs(r,c);
- }
- }
- ll ans = 0;
- ms(vis2,false);
- loop(r,1,n)
- {
- loop(c,1,m)
- {
- if(wat[r][c] && !vis2[r][c])
- {
- cka = r,cpa =c;
- ll wall = dfs2(r,c);
- for(int ix=0; ix<SZ(vokhit); ix++)
- {
- if(vokhit[ix]<wall)
- ans+=(wall-vokhit[ix]);
- }
- vokhit.clear();
- }
- }
- }
- pf("%lld\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement