Cod sursa(job #1847651)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 14 ianuarie 2017 20:30:05
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.79 kb
#include <fstream>
#include <deque>

using namespace std;

ifstream f("barbar.in");
ofstream g("barbar.out");

struct pct {
    int x, y;
};
pct drg[8005];
int nd, ix, iy, ox, oy;
const int f_mare = 1e8;
char s[1005][1005];
int dist[1005][1005];

int ddr[1005][1005];
int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
int i, j, n, m;

inline void reset() {
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
            if (s[i][j] == 'D' || s[i][j] == '*')
                dist[i][j] = -1;
            else dist[i][j] = f_mare;
}

inline bool inauntru (const int &x, const int &y) {
    return x >= 1 && y >= 1 && x <= n && y <= m;
}

deque <pct> c1;
inline void bfd() {
    int x, y, xx,yy;
    while (c1.empty() == 0) {
        x = c1.front().x;
        y = c1.front().y;
        c1.pop_front();
        for (i = 0; i < 4; i++) {
            xx = x+dx[i];
            yy = y+dy[i];
            if (dist[xx][yy] > dist[x][y]+1 && inauntru(xx, yy)) {
                dist[xx][yy] = dist[x][y]+1;
                c1.push_back({xx, yy});
            }
        }
    }
}

inline bool bf(const int &LIM) {
    int x, y, xx, yy;
    c1.clear();
    c1.push_back({ix, iy});
    dist[ix][iy] = 0;
    if (ddr[ix][iy] < LIM)
        return 0;
    while (c1.empty() == 0) {
        x = c1.front().x;
        y = c1.front().y;
        c1.pop_front();
        for (i = 0; i < 4; i++) {
            xx = x+dx[i];
            yy = y+dy[i];
            if (dist[xx][yy] > dist[x][y]+1 && inauntru(xx, yy) && ddr[xx][yy] >= LIM) {
                dist[xx][yy] = dist[x][y]+1;
                if (xx == ox && yy == oy)
                    return 1;
                c1.push_back({xx, yy});
            }
        }
    }
    return 0;
}

int main() {
    f >> n >> m; f.get();
    c1.clear();
    for (i = 1; i <= n; i++) {
        f.getline(s[i]+1, sizeof(s[i]));
        for (j = 1; j <= m; j++) {
            if (s[i][j] == 'I')
                ix = i, iy = j,dist[i][j] = f_mare;
            if (s[i][j] == 'D')
                c1.push_back({i, j});
            if (s[i][j] == 'O')
                ox = i, oy = j, dist[i][j]=f_mare;
            if (s[i][j] == '*')
                dist[i][j] = -1;
            if (s[i][j] == '.')
                dist[i][j] = f_mare;
        }
    }
    bfd();
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            ddr[i][j] = dist[i][j];
            //g << ddr[i][j] << ' ';
        }
        //g << '\n';
    }
    int p = 1, w;
    while (p <= 1000000)
        p *= 2;
    j = 0;
    bool FAIL = 1;
    while (p) {
        reset();
        if (bf(j+p))
            j += p, FAIL = 0;
        p /= 2;
    }
    if (!FAIL) g << j;
    else       g << -1;

    return 0;
}