Pagini recente » Cod sursa (job #2053616) | Cod sursa (job #3211453) | Cod sursa (job #43312) | Cod sursa (job #613205) | Cod sursa (job #2112167)
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int r, c;
char a[1001][1001];
int mindist[1001][1001];
#define fi first
#define se second
pair <int, int> s, d;
queue <pair <int, int> > q;
pair <int, int> q1[1000001];
int st, dr;
int viz[1001][1001], cat;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
inline bool ver(int dist) {
st = 1, dr = 1;
q1[st] = s;
cat++;
while(st <= dr) {
pair <int, int> nod = q1[st];
st++;
int x, y;
x = nod.fi, y = nod.se;
for(int i = 0;i < 4;i++) {
int nx, ny;
nx = x + dx[i], ny = y + dy[i];
if(a[nx][ny] == 1 && mindist[nx][ny] >= dist && viz[nx][ny] < cat) {
q1[++dr] = {nx, ny};
viz[nx][ny] = cat;
if(nx == d.fi && ny == d.se) {
return 1;
}
}
}
}
return 0;
}
int main() {
freopen("barbar.in", "r", stdin);
freopen("barbar.out", "w", stdout);
scanf("%d%d", &r, &c);
fgetc(stdin);
for(int i = 1;i <= r;i++) {
for(int j = 1;j <= c;j++) {
char c;
mindist[i][j] = INF;
c = fgetc(stdin);
if(c == '.')
a[i][j] = 1;
else if(c == 'D')
a[i][j] = 2, q.push({i, j}), mindist[i][j] = 0;
else if(c == 'I')
a[i][j] = 1, s = make_pair(i, j);
else if(c == 'O')
a[i][j] = 1, d = make_pair(i, j);
}
fgetc(stdin);
}
while(!q.empty()) {
pair <int, int> nod = q.front();
q.pop();
int x, y;x = nod.fi, y = nod.se;
for(int i = 0;i < 4;i++) {
int nx, ny; nx = x + dx[i], ny = y + dy[i];
if(a[nx][ny]) {
if(mindist[nx][ny] > mindist[x][y] + 1) {
mindist[nx][ny] = mindist[x][y] + 1;
q.push({nx, ny});
}
}
}
}
int r = 0, pas = 1 << 20;
while(pas) {
if(ver(r + pas))
r += pas;
pas >>= 1;
}
printf("%d", r);
return 0;
}