Pagini recente » Cod sursa (job #978937) | Cod sursa (job #577354) | Cod sursa (job #2242122) | Cod sursa (job #955006) | Cod sursa (job #2467078)
//ALEX ENACHE
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
using namespace std;
//-----------------------------------------------------------------
#include <fstream>
//ifstream cin("input"); ofstream cout("output");
ifstream cin("barbar.in"); ofstream cout("barbar.out");
const int inf = 1e9;
const int MAXN = 1010;
queue < pair < int , int> > q;
int obst[MAXN][MAXN];
int dist[MAXN][MAXN];
int used[MAXN][MAXN];
int dirx[] = { 0 , 0 , 1 , -1 };
int diry[] = { 1 , -1 , 0 , 0 };
int main() {
int n , m;
cin >> n >> m;
char c;
pair < int, int > start, end;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dist[i][j] = inf;
cin >> c;
if (c == '*') {
obst[i][j] = 1;
}
if (c == 'D') {
q.push({ i , j });
dist[i][j] = 0;
}
if (c == 'I') {
start = { i , j };
}
if (c == 'O') {
end = { i , j };
}
}
}
while (!q.empty()) {
pair < int, int > now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = now.first + dirx[i];
int y = now.second + diry[i];
if (x < 1 || x > n || y < 1 || y > m) {
continue;
}
if (obst[x][y]) {
continue;
}
if (dist[x][y] <= dist[now.first][now.second] + 1) {
continue;
}
dist[x][y] = dist[now.first][now.second] + 1;
q.push({ x , y });
}
}
int st = 0, dr = n * m;
int ans = 0;
while (st <= dr) {
int mij = st + dr;
mij /= 2;
used[start.first][start.second] = 1;
q.push(start);
while (!q.empty()) {
pair < int, int > now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = now.first + dirx[i];
int y = now.second + diry[i];
if (x < 1 || x > n || y < 1 || y > m) {
continue;
}
if (obst[x][y]) {
continue;
}
if (dist[x][y] < mij) {
continue;
}
if (used[x][y]) {
continue;
}
used[x][y] = 1;
q.push({ x , y });
}
}
if (used[end.first][end.second]) {
st = mij + 1;
ans = mij;
}
else {
dr = mij - 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
used[i][j] = 0;
}
}
}
cout << ans;
return 0;
}