Pagini recente » Cod sursa (job #1818999) | Cod sursa (job #1833975) | Cod sursa (job #1913433) | Cod sursa (job #2847115) | Cod sursa (job #2637385)
#include <fstream>
#include <queue>
#include <functional>
#include <algorithm>
#include <bitset>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
int v[1005][1005];
int n, m;
void border() {
for (int i = 0; i <= n + 1; i++) {
v[i][0] = -10;
v[i][m + 1] = -10;
}
for (int i = 0; i <= m + 1; i++) {
v[0][i] = -10;
v[n + 1][i] = -10;
}
}
int main() {
queue<pair<int, int> > q;
pair<int, int> enter, exit;
char c;
in >> n >> m;
border();
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) {
in >> c;
if (c == '*')
v[i][j] = -1;
else if (c == 'D')
v[i][j] = -2, q.push(make_pair(i, j));
else if (c == 'I')
enter = make_pair(i, j);
else if (c == 'O')
exit = make_pair(i, j);
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second, val=max(0,v[x][y]);
for (int i = 0; i < 4; i++) {
if ((v[x + dx[i]][y + dy[i]] == 0 || v[x + dx[i]][y + dy[i]] > val + 1))
v[x + dx[i]][y + dy[i]] = val + 1, q.push(make_pair(x + dx[i], y + dy[i]));
}
q.pop();
}
function<int(int, int)> to_v = [&](int x, int y) {
if (x == 0 || y == 0)
return 0;
return (x - 1) * m + y;
};
function<bool(int)> cango = [&](int g) {
int x, y;
queue<pair<int, int> > q;
bitset<1010025> b;
b[0] = 1;
q.push(enter);
while (!q.empty()) {
if (q.front() == exit)
return 1;
x = q.front().first;
y = q.front().second;
for (int i = 0; i < 4; i++) {
if (!b[to_v(x + dx[i], y + dy[i])] && v[x + dx[i]][y + dy[i]] >= g) {
q.push(make_pair(x + dx[i], y + dy[i]));
b[to_v(x + dx[i], y + dy[i])] = 1;
}
}
q.pop();
}
return 0;
};
int pos = 0;
for (int pas = 1 << 12; pas >= 1; pas /= 2) {
if (cango(pos + pas))
pos += pas;
}
out << pos;
return 0;
}