Cod sursa(job #1478179)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 28 august 2015 02:47:48
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.93 kb
#include <iostream>
#include <fstream>
#include <deque>
#include <bitset>
#include <cstring>

using namespace std;

ifstream fin("barbar.in");
ofstream fout("barbar.out");

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

bitset < NMax > viz[NMax];
deque < int > qx, qy;

int X, Y;
int v[NMax][NMax];

void lee(){
    int x, y, nx, ny;
    while(!qx.empty()){
        x = qx.front();
        y = qy.front();
        qx.pop_front();
        qy.pop_front();
        for(int i = 0; i < 4; i++){
            nx = x + dx[i];
            ny = y + dy[i];
            if(v[nx][ny] == 0){
                v[nx][ny] = v[x][y] + 1;
                qx.push_back(nx);
                qy.push_back(ny);
            }
        }
    }
}

void solve(int lim, bool &is){
    memset(viz, 0, sizeof(viz));
    viz[qx.front()][qy.front()] = 1;
    int x, y, nx, ny;
    while(!qx.empty()){
        x = qx.front();
        y = qy.front();
        qx.pop_front();
        qy.pop_front();
        for(int i = 0; i < 4; i++){
            nx = x + dx[i];
            ny = y + dy[i];
            if(viz[nx][ny] == 0 && v[nx][ny] > lim){
                if(nx == X && ny == Y){
                    is = true;
                    qx.clear();
                    qy.clear();
                    return;
                }
                viz[nx][ny] = 1;
                qx.push_back(nx);
                qy.push_back(ny);
            }
        }
    }
}

int main(){
    string read;
    int n, m, x, y;
    fin >> n >> m;
    for(int i = 1; i <= n; i++){ // READ
        fin >> read;
        read = " " + read;
        for(int j = 1; j <= m; j++){
            if(read[j] == '*'){
                v[i][j] = -1;
            } else {
                if(read[j] == 'D'){
                    v[i][j] = 1;
                    qx.push_back(i);
                    qy.push_back(j);
                } else {
                    if(read[j] == 'I'){
                        x = i;
                        y = j;
                    } else {
                        if(read[j] == 'O'){
                            X = i;
                            Y = j;
                        }
                    }
                }
            }
        }
    }
    for(int i = 0; i <= n + 1; i++){ //BORD
        v[i][0] = v[i][m + 1] = -1;
    }
    for(int j = 0; j <= m + 1; j++){ //BORD
        v[0][j] = v[n + 1][j] = -1;
    }
    lee();
    int lo = 0, hi = n * m, mid, ans;
    bool is;
    ans = 0;
    while(lo <= hi){
        mid = (lo + hi) >> 1;
        is = false;
        qx.push_back(x);
        qy.push_back(y);
        solve(mid, is);
        if(is == true && v[x][y] > mid){
            lo = mid + 1;
            ans = 1;
        } else {
            hi = mid - 1;
        }
    }
    if(ans){
        fout << hi;
    } else {
        fout << -1;
    }
    return 0;
}