Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1.     #include <bits/stdc++.h>
  2.     #include <ext/pb_ds/assoc_container.hpp>
  3.     #include <ext/pb_ds/tree_policy.hpp>
  4.      
  5.     using namespace std;
  6.     using namespace __gnu_pbds;
  7.      
  8.     template <typename T>
  9.     using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10.      
  11.     typedef long long ll;
  12.     typedef pair<ll, ll> pll;
  13.     typedef pair<int, int> pii;
  14.     typedef complex<double> point;
  15.      
  16.     #define X real()
  17.     #define Y imag()
  18.     #define angle(a) (atan2((a).imag(), (a).real()))
  19.     #define vec(a, b) ((b) - (a))
  20.     #define same(p1, p2) (dp(vec(p1, p2), vec(p1, p2)) < EPS)
  21.     #define dp(a, b) ((conj(a) * (b)).real())
  22.     #define cp(a, b) ((conj(a) * (b)).imag())
  23.     #define length(a) (hypot((a).imag(), (a).real()))
  24.     #define normalize(a) (a) / length(a)
  25.     #define rotateO(p, ang) ((p)*exp(point(0, ang)))
  26.     #define rotateA(p, ang, about) (rotateO(vec(about, p), ang) + about)
  27.     #define reflectO(v, m) (conj((v) / (m)) * (m))
  28.      
  29.     const ll N = 1005;
  30.     const int MOD = 1e5;
  31.     const double EPS = 1e-5;
  32.     const double PI = acos(-1.0);
  33.      
  34.     int dx[] = {-1, 0, 0, 1};
  35.     int dy[] = {0, -1, 1, 0};
  36.      
  37.     ll n, m, mat[N][N], dist[N][N];
  38.      
  39.     bool valid(ll x, ll y)
  40.     {
  41.         return x >= 0 && x < n && y >= 0 && y < m && mat[x][y] == 1;
  42.     }
  43.      
  44.     int main()
  45.     {
  46.         scanf("%lld %lld", &n, &m);
  47.        
  48.         queue<pll> Q;
  49.      
  50.         memset(dist, -1, sizeof(dist));
  51.      
  52.         for(int i = 0; i < n; i++)
  53.             for(int j = 0; j < m; j++)
  54.             {
  55.                 scanf("%lld", &mat[i][j]);
  56.                 if(mat[i][j] == 2)
  57.                 {
  58.                     Q.push({i, j});
  59.                     dist[i][j] = 0;
  60.                 }
  61.             }
  62.      
  63.         while(!Q.empty())
  64.         {
  65.             ll x = Q.front().first;
  66.             ll y = Q.front().second;
  67.             Q.pop();
  68.            
  69.             for(int i = 0; i < 4; i++)
  70.             {
  71.                 ll nx = x + dx[i];
  72.                 ll ny = y + dy[i];
  73.                 if(valid(nx, ny) && (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1))
  74.                 {
  75.                     dist[nx][ny] = dist[x][y] + 1;
  76.                     Q.push({nx, ny});
  77.                 }
  78.             }
  79.         }
  80.         ll maxx = 0;
  81.         for(int i = 0; i < n; i++)
  82.          for(int j = 0; j < m; j++)
  83.             if(mat[i][j] == 1)
  84.             {
  85.                 if(dist[i][j] == -1)
  86.                 {
  87.                     printf("-1\n");
  88.                     return 0;
  89.                 }
  90.                 else
  91.                     maxx = max(maxx, dist[i][j]);
  92.             }
  93.         printf("%lld\n", maxx);
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement