Pagini recente » Cod sursa (job #1240377) | Cod sursa (job #1767986) | Cod sursa (job #1778335) | Cod sursa (job #3316132) | Cod sursa (job #3321915)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
#if 0
#define int ll
#define uint ull
#endif
/**
* Problem: Barbar
* URL: https://infoarena.ro/problema/barbar
* TL: 200 ms
* ML: 64 MB
*
* Good Luck!
*/
int dirl[4] = {-1, 1, 0, 0};
int dirc[4] = {0, 0, -1, 1};
void tc() {
int n, m; cin >> n >> m;
vector<vector<bool>> mat(n + 1, vector<bool>(m + 1));
vector<vector<int>> minn(n + 1, vector<int>(m + 1, INT32_MAX));
vector<vector<int>> maxx(n + 1, vector<int>(m + 1));
queue<pair<int, int>> q;
pair<int, int> ii, oo;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
char c; cin >> c;
if (c == '*') mat[i][j] = true;
else if (c == 'D') {
minn[i][j] = 0;
q.push({i, j});
}
else if (c == 'I') ii = {i, j};
else if (c == 'O') oo = {i, j};
}
}
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int dir = 0; dir < 4; dir ++) {
int xx = x + dirl[dir], yy = y + dirc[dir];
if (1 <= xx && xx <= n && 1 <= yy && yy <= m && !mat[xx][yy] && minn[xx][yy] > minn[x][y] + 1) {
minn[xx][yy] = minn[x][y] + 1;
q.push({xx, yy});
}
}
}
maxx[ii.first][ii.second] = minn[ii.first][ii.second];
q.push(ii);
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int dir = 0; dir < 4; dir ++) {
int xx = x + dirl[dir], yy = y + dirc[dir];
if (1 <= xx && xx <= n && 1 <= yy && yy <= m && !mat[xx][yy] && maxx[xx][yy] < min(maxx[x][y], minn[xx][yy])) {
maxx[xx][yy] = min(maxx[x][y], minn[xx][yy]);
q.push({xx, yy});
}
}
}
cout << maxx[oo.first][oo.second] << '\n';
}
#define MTC 0
#define FIO 1
#define FN "barbar"
signed main() {
#if FIO == 1 && !defined(LOCAL)
(void)freopen(FN ".in", "r", stdin);
(void)freopen(FN ".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#if MTC == 1
signed tt; cin >> tt;
while (tt --) tc();
#else
tc();
#endif
return 0;
}