Cod sursa(job #1025716)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 10 noiembrie 2013 14:43:41
Problema Barbar Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.47 kb
#include <fstream>
#include <queue>
using namespace std;

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

const int NMAX = (1<<10);
const int INF = (1<<20);

string Map[NMAX];
int Dist[NMAX][NMAX];
bool Uz[NMAX][NMAX];
queue<pair<int,int> > Q;

int N, M, IX, IY, OX, OY;

bool isInMap(int x,int y){

    if(x < 0 || y < 0 || x >= N || y >= M)
        return false;
    return true;
}

void fillDist(){
    for(int i = 0; i < N; i++){
        for(int j = 0 ; j < M; j++){
            Dist[i][j] = INF;
        }
    }
}

void calcMinDist(){
    int x, y;
    while(!Q.empty()){
        x = Q.front().first;
        y = Q.front().second;
        Q.pop();

        if(isInMap(x+1,y)){
            if(Dist[x][y] + 1 < Dist[x+1][y]){
                Q.push(make_pair(x+1,y));
                Dist[x+1][y] = Dist[x][y] +1;
            }
        }

        if(isInMap(x-1,y)){
            if(Dist[x][y] + 1 < Dist[x-1][y]){
                Q.push(make_pair(x-1,y));
                Dist[x-1][y] = Dist[x][y] +1;
            }
        }

        if(isInMap(x,y+1)){
            if(Dist[x][y] + 1 < Dist[x][y+1]){
                Q.push(make_pair(x,y+1));
                Dist[x][y+1] = Dist[x][y] +1;
            }
        }

        if(isInMap(x,y-1)){
            if(Dist[x][y] + 1 < Dist[x][y-1]){
                Q.push(make_pair(x,y-1));
                Dist[x][y-1] = Dist[x][y] +1;
            }
        }
    }
}

void resetUz(){
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            Uz[i][j] = false;
}

bool findPath(int minDist){
    int x, y;

    resetUz();

    while(!Q.empty())Q.pop();

    Q.push(make_pair(IX,IY));

    while(!Q.empty()){
        x = Q.front().first;
        y = Q.front().second;

        Q.pop();

        if(x == OX && y == OY){
            return true;
        }

        if(isInMap(x+1,y)){
            if(Uz[x+1][y] == false && Dist[x+1][y] >= minDist)
                Q.push(make_pair(x+1,y));
                Uz[x+1][y] = 1;

        }

        if(isInMap(x-1,y)){
            if(Uz[x-1][y] == false && Dist[x-1][y] >= minDist)
                Q.push(make_pair(x-1,y));
                Uz[x-1][y] = 1;
        }

        if(isInMap(x,y+1)){
            if(Uz[x][y+1] == false && Dist[x][y+1] >= minDist)
                Q.push(make_pair(x,y+1));
                Uz[x][y+1] = 1;
        }

        if(isInMap(x,y-1)){
            if(Uz[x][y-1] == false && Dist[x][y-1] >= minDist)
                Q.push(make_pair(x,y-1));
                Uz[x][y-1] = 1;
        }
    }

    return false;
}

int searchMaxValue()
{
    int i, step;
    for (step = 1; step < N*M; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < N*M && findPath(i+step) == true)
           i += step;
    if(i != 0) {
        return i;
    }
    else {
        return -1;
    }
}

int main()
{
    int i,j;
    in >> N >> M;

    for(i = 0; i < N; i++){
        in >>Map[i];
    }

    fillDist();

    for(i = 0; i < N; i++){
        for(j = 0; j < M; j++){
            if(Map[i][j] == 'D'){
                Q.push(make_pair(i,j));
                Dist[i][j] = 0;
            }
            else if(Map[i][j] == 'I'){
                IX = i, IY = j;
            }
            else if(Map[i][j] == 'O'){
                OX = i, OY = j;
            }
        }
    }

    calcMinDist();

    out << searchMaxValue();
    return 0;
}