Pagini recente » Cod sursa (job #366025) | Cod sursa (job #2447854) | Statisticile problemei Vip | Cod sursa (job #231357) | Cod sursa (job #1729426)
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int n, m, stx, sty, sfx, sfy;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int a[1002][1002], b[1002][1002];
pair<int,int> aux, work;
queue<pair<int,int>> q;
void read() {
char c;
fin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
fin >> c;
if (c == 'I') {
a[i][j] = 1 << 30;
stx = i;
sty = j;
}
else if (c == 'O') {
a[i][j] = 1 << 30;
sfx = i;
sfy = j;
}
else if (c == 'D') {
a[i][j] = 0;
q.push(make_pair(i, j));
}
else if (c == '*') {
a[i][j] = -4;
}
else
a[i][j] = 1 << 30;
}
}
for (int i=0; i<=n+1; i++)
a[i][0] = a[i][m+1] = -4;
for (int j=0; j<=m+1; j++)
a[0][j] = a[n+1][j] = -4;
}
void dragons() {
while (!q.empty())
{
auto x = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int newx = x.first + dx[i];
int newy = x.second + dy[i];
if (a[newx][newy] > a[x.first][x.second] + 1)
{
a[newx][newy] = a[x.first][x.second] + 1;
q.push(make_pair(newx, newy));
}
}
}
}
void copying() {
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
b[i][j] = a[i][j] >= 0 ? 1 << 30 : a[i][j];
for (int i=0; i<=n+1; i++)
b[i][0] = b[i][m+1] = -4;
for (int j=0; j<=m+1; j++)
b[0][j] = b[n+1][j] = -4;
}
int drum(int k) {
copying();
q = queue<pair<int,int>>();
aux.first = stx;
aux.second = sty;
b[stx][sty] = 0;
if (a[stx][sty] < k)
return 0;
q.push(aux);
while (!q.empty()) {
aux = q.front();
if (sfx == aux.first && sfy == aux.second) {
return 1;
}
q.pop();
for (int i=0; i<4; i++) {
work.first = aux.first + dx[i];
work.second = aux.second + dy[i];
if (a[work.first][work.second] >= k && b[aux.first][aux.second] + 1 < b[work.first][work.second]) {
q.push(work);
b[work.first][work.second] = b[aux.first][aux.second] + 1;
}
}
}
return 0;
}
int caut (int st, int dr) {
int mij, ras=-1;
while (st < dr) {
mij = (st + dr) / 2;
if (drum(mij) == 1) {
st = mij + 1;
ras = mij;
}
else
dr = mij;
}
return ras;
}
int main() {
int big;
read();
big = 2 * max(n, m) - 2;
dragons();
fout << caut(0, big);
return 0;
}