sukesh2000

KickStart A - Rabbit House

Mar 21st, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. /**************************
  2. *                         *
  3. *    Author: Sukesh       *
  4. *                         *
  5. ***************************/
  6. #include<bits/stdc++.h>
  7. #define FIO                 ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
  8. #define int                 long long int
  9. #define float               long double
  10. #define re(arr, n, m)       arr.resize(n, vll (m,0))
  11. #define inp1d(arr)          for(auto &it: arr) cin>>it
  12. #define inp1ds(arr, sum)    for(auto &it: arr){ cin>>it; sum+=it; }
  13. #define inp1dma(arr, m)     for(auto &it: arr){ cin>>it; m = max(m,it); }
  14. #define inp1dmi(arr, m)     for(auto &it: arr){ cin>>it; m = min(m,it); }
  15. #define inp2d(arr)          for(auto &it1: arr) for(auto &it2: it1) cin>>it2
  16. #define o1d(arr)            for(auto it: arr) cout<<it<<" ";
  17. #define o2d(arr)            for(auto it1: arr) { for(auto it2: it1) cout<<it2<<" "; cout<<endl; }
  18. #define fo(i,s,e)           for(int i=s;i<e;i++)
  19. #define fe(i,s,e)           for(int i=s;i<=e;i++)
  20. #define fi(i,e,s)           for(int i=e;i>=s;i--)
  21. #define vec(x)              vector<x>
  22. #define mp(x,y)             map<x, y>
  23. #define pr(x,y)             pair<x,y>
  24. #define set(x)              set<x>
  25. #define lb                  lower_bound
  26. #define ub                  upper_bound
  27. #define eb                  emplace_back
  28. #define all(arr)            arr.begin(),arr.end()
  29. #define S(arr)              arr.size()
  30. #define nl                  cout<<"\n"
  31. #define fr                  first
  32. #define se                  second
  33. #define contains(arr,x)     arr.find(x) != arr.end()
  34. #define INF                 (int)1e9
  35.  
  36. using namespace std;
  37. template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
  38. template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
  39.  
  40. int di[] = {0,-1,0,1};
  41. int dj[] = {1,0,-1,0};
  42. vec(vec(int)) matrix;
  43.  
  44. bool isValid(int i, int j, int height){
  45.     int n = S(matrix), m = S(matrix[0]);
  46.     if(i<0 || j<0 || i>=n || j>=m)
  47.         return 0;
  48.     if(matrix[i][j] >= height-1)
  49.         return 0;
  50.  
  51.     return 1;
  52. }
  53.  
  54. int BFS(int i, int j){
  55.     queue<pr(int,pr(int, int))> q;
  56.     q.push({matrix[i][j], {i,j}});
  57.  
  58.     int ans = 0;
  59.     while(!q.empty()){
  60.         auto curr = q.front();
  61.         q.pop();
  62.         int height = curr.first;
  63.         int x = curr.second.first, y = curr.second.second;
  64.  
  65.         fo(k,0,4){
  66.             int nx = x + di[k], ny = y + dj[k];
  67.             if(isValid(nx, ny, height)){
  68.                 int diff = height - 1 - matrix[nx][ny];
  69.                 ans += diff;
  70.                 matrix[nx][ny] += diff;
  71.                 q.push({matrix[nx][ny], {nx, ny}});
  72.             }
  73.         }
  74.     }
  75.  
  76.     return ans;
  77. }
  78.  
  79.  
  80.  
  81. int solve(){
  82.     FIO;
  83.     int r, c; cin >> r >> c;
  84.     matrix.clear(), matrix.resize(r, vec(int) (c));
  85.  
  86.     int maxel = -1, maxi = 0, maxj = 0;
  87.     fo(i,0,r){
  88.         fo(j,0,c){
  89.             cin >> matrix[i][j];
  90.             if(matrix[i][j] > maxel){
  91.                 maxi = i, maxj = j;
  92.                 maxel = matrix[i][j];
  93.             }
  94.         }
  95.     }
  96.  
  97.     int ans = BFS(maxi,maxj);
  98.     fo(i,0,r){
  99.         fo(j,0,c){
  100.             ans += BFS(i,j);
  101.         }
  102.     }
  103.    
  104.     return ans;
  105. }
  106.  
  107. signed main(){
  108.     FIO;
  109.    
  110.     // #ifndef ONLINE_JUDGE
  111.     // freopen("in.txt","r",stdin);
  112.     // freopen("out.txt","w",stdout);
  113.     // #endif
  114.  
  115.     int t=1;
  116.     cin>>t;
  117.     int n = t;
  118.     while(t--){
  119.         int res = solve();
  120.         cout << "Case #" << n-t << ": " << res; nl;
  121.     }
  122.    
  123.     #ifndef ONLINE_JUDGE
  124.         cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
  125.     #endif
  126.     return 0;
  127. }
Add Comment
Please, Sign In to add comment