Cod sursa(job #1537799)

Utilizator OpportunityVlad Negura Opportunity Data 28 noiembrie 2015 02:16:01
Problema Barbar Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <fstream>
#include <queue>
#include <stdio.h>
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
using namespace std;

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

ifstream fi("barbar.in");
ofstream fo("barbar.out");

int n,m,a[1001][1001],isDragon[1001][1001],high,low,viz[1001][1001];
char ch;
pair<int,int> start,finish;
deque< pair<int,int> > q;

inline bool inside(int x, int y) {
    return (x >= 0 && x < n && y >= 0 && y < m);
}

void clear() {
    while (!q.empty()) q.pop_front();
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            viz[i][j] = 0;
        }
    }
}

bool bfs(int bound) {
    clear();
    q.pb(mp(start.fs, start.sc));
    viz[start.fs][start.sc] = 1;
    while (!q.empty()) {
        pair<int,int> aux = q.front();
        q.pop_front();
        if (aux.fs == finish.fs && aux.sc == finish.sc) {
            return true;
        }
        for (int i = 0; i < 4; ++i) {
            int nx = aux.fs + dx[i];
            int ny = aux.sc + dy[i];
            if (inside(nx, ny) && a[nx][ny] >= bound && !viz[nx][ny]) {
                q.pb(mp(nx,ny));
                viz[nx][ny] = 1;
            }
        }
    }

    return false;
}

int bsearch(int low, int high) {
    int mid = (high+low)/2;
    if (low == high) {
        if (bfs(low)) {
            return low;
        } else {
            return low-1;
        }
    }
    if (bfs(mid)) {
        return bsearch(mid+1, high);
    } else {
        return bsearch(low, mid-1);
    }
}

int main() {

    File *fi = fopen("barbar.in", "r");

    fi >> n >> m;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            fscanf(fi,"%c",&ch);
            switch (ch) {
                case 'I':
                    start.fs = i;
                    start.sc = j;
                    break;
                case 'O':
                    finish.fs = i;
                    finish.sc = j;
                    break;
                case 'D':
                    q.pb(mp(i,j));
                    isDragon[i][j] = 1;
                    break;
                case '*':
                    a[i][j] = -1;
                    break;
                default:
                    break;
            }
        }
    }

    while (!q.empty()) {
        pair<int,int> aux = q.front();
        q.pop_front();
        for (int i=0; i<4; i++) {
            int nx = aux.fs + dx[i];
            int ny = aux.sc + dy[i];
            if (inside(nx, ny) && !isDragon[nx][ny] && a[nx][ny] == 0) {
                a[nx][ny] = a[aux.fs][aux.sc]+1;
                q.pb(mp(nx, ny));
                high = max(high,a[nx][ny]);
            }
        }
    }

    if (!bfs(1)) {
        fo << -1;
    } else {
        fo << bsearch(low, high);
    }

    return 0;
}