Cod sursa(job #2076063)

Utilizator Andreiii500Andrei Puiu Andreiii500 Data 26 noiembrie 2017 04:11:02
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.32 kb
#include<bits/stdc++.h>
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

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

const int N = 1001;

enum {DRAGON = -9, PERETE, INCEPUT, SFARSIT, LIBER_DAR_IN_COADA = -1,LIBER = 0};
int dist[N][N]; /// dist[i][j] = distance from (i, j) to the nearest dragon
int ystart, xstart, ysfarsit, xsfarsit;
int n,m;
pair<int, int> coada[N*N];

int addx[] = {0, 1, 0, -1};
int addy[] = {-1, 0, 1, 0};

int inside_map(int y, int x){
    if(y<0 || y>=n) return 0;
    if(x<0 || x>=m) return 0;
    return 1;
}

void calculate_dist_dragons(){
    int st=1,dr=0;
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            if(dist[i][j] == DRAGON)
            {
                coada[++dr] = make_pair(i, j);
            }

    while(st <= dr)
    {
        int y = coada[st].first;
        int x = coada[st].second;

        for(int dir = 0; dir < 4; ++dir)
        {
            int new_x = x + addx[dir];
            int new_y = y + addy[dir];

            if(inside_map(new_y, new_x) && (dist[new_y][new_x] == LIBER || dist[new_y][new_x] == INCEPUT || dist[new_y][new_x] == SFARSIT))
            {
                if(dist[y][x] == DRAGON)
                    dist[new_y][new_x] = 1;
                else if(dist[y][x] > 0)
                    dist[new_y][new_x] = dist[y][x] + 1;
                else
                    cout<<"Eroare! Am ajuns intr-un punct printr-un mod bizar!\n";

                coada[++dr] = make_pair(new_y, new_x);
            }
        }

        ++st;
    }
}

int parcurs_in_drumul_x[N][N]; /// [i][j] = x daca punctul (i, j) a fost parcurs in drumul de distanta minima X :D
int pot_parcurge_drumul_de_distanta_dist_minima(int dist_minima){
    int st=1, dr=1;
    coada[1] = make_pair(ystart, xstart);
    while(st <= dr){
        int y = coada[st].first;
        int x = coada[st].second;

        for(int dir = 0; dir < 4; ++dir)
        {
            int new_x = x + addx[dir];
            int new_y = y + addy[dir];

            if(inside_map(new_y, new_x) && dist[new_y][new_x] >= dist_minima && parcurs_in_drumul_x[new_y][new_x] != dist_minima)
            {
                if(new_y == ysfarsit && new_x == xsfarsit)
                    return 1;
                parcurs_in_drumul_x[new_y][new_x] = dist_minima;

                coada[++dr] = make_pair(new_y, new_x);
            }
        }

        ++st;
    }

    return 0; /// Didn't reach the final point yet? Then, we can't reach it!
}

int binary_search_answer(int minimal, int maximal){
    while(minimal < maximal){
        int mij = (minimal + maximal) / 2;
        if((minimal + maximal) % 2 == 1)
            ++mij;
        if(!pot_parcurge_drumul_de_distanta_dist_minima(mij))
            maximal = mij-1;
        else
            minimal = mij;
    }

    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            parcurs_in_drumul_x[i][j] = 0;
    if(!pot_parcurge_drumul_de_distanta_dist_minima(minimal))
        return -1;
    else
        return minimal;
}

int main()
{
    int i,j;
    char c;

    in>>n>>m;
    for(int i=0; i<n; ++i)
    {
        for(int j=0; j<m; ++j)
        {
            in>>c;
            if(c == 'D')
            {
                dist[i][j] = DRAGON;
            }
            else if(c == '*')
            {
                dist[i][j] = PERETE;
            }
            else if(c == 'I')
            {
                dist[i][j] = INCEPUT;
                ystart = i;
                xstart = j;
            }
            else if(c == 'O')
            {
                dist[i][j] = SFARSIT;
                ysfarsit = i;
                xsfarsit = j;
            }
            else if(c == '.')
            {
                dist[i][j] = LIBER;
            }
            else
            {
                cout<<"Eroare! Caracter necunoscut!\n";
                return -1;
            }
            //cout<<c;
        }
        //cout<<"\n";
    }

    //for(int i=0; i<n; ++i){for(int j=0; j<m; ++j)cout<<setw(3)<<dist[i][j];cout<<"\n";} cout<<"\n";
    calculate_dist_dragons();
    //for(int i=0; i<n; ++i){for(int j=0; j<m; ++j)cout<<setw(3)<<dist[i][j];cout<<"\n";} cout<<"\n";

    out<<binary_search_answer(0, 2*n);


    return 0;
}