Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "MAXR"
- #include <iostream>
- #include <cstdio>
- #include <queue>
- using namespace std;
- using ll = long long;
- using ld = long double;
- template <class T>
- void readInt(T &x)
- {
- x = 0;
- register int c;
- while ((c = getchar()) && (c > '9' || c < '0'))
- ;
- for (; c >= '0' && c <= '9'; c = getchar())
- x = x * 10 + c - '0';
- }
- constexpr int N = 5e3 + 5;
- constexpr int Inf = 1e9 + 7;
- int x[] = {0, 0, -1, 1},
- y[] = {-1, 1, 0, 0};
- int m, n;
- int a[N][N], d[N][N];
- void Read()
- {
- // cin >> m >> n;
- readInt(m), readInt(n);
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- // cin >> a[i][j];
- readInt(a[i][j]);
- }
- void Solve()
- {
- fill_n(&d[0][0], N * N, Inf);
- queue<pair<int, int>> q;
- for (int i = 0; i <= m + 1; ++i)
- for (int j = 0; j <= n + 1; ++j)
- if (a[i][j] == 0)
- {
- d[i][j] = 0;
- q.emplace(i, j);
- }
- int ans(0);
- while (!q.empty())
- {
- pair<int, int> c = q.front();
- q.pop();
- ans = max(ans, d[c.first][c.second] - 1);
- for (int i = 0; i < 4; ++i)
- {
- int u = c.first + x[i],
- v = c.second + y[i];
- if (u > 0 && u <= m && v > 0 && v <= n && d[u][v] == Inf)
- {
- d[u][v] = d[c.first][c.second] + 1;
- q.emplace(u, v);
- }
- }
- }
- cout << ans;
- }
- int32_t main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement