Cod sursa(job #2067708)

Utilizator mjmilan11Mujdar Milan mjmilan11 Data 16 noiembrie 2017 19:31:27
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.91 kb
#include <iostream>
#include <fstream>
#include <queue>

#define NMAX 1001

using namespace std;

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

int n,m,ok,xout,yout,xi,yi,xa,ya,xin,yin,mij;
int v[NMAX][NMAX],v2[NMAX][NMAX];
int dx[]={0, 0, 0, 1, -1};
int dy[]={0, 1, -1, 0, 0};

int Interior(int a,int b)
{
    if(a<=n && a>=1 && b<=m && b>=1) return 1;
    return 0;
}

queue <pair<int ,int> > q;
pair <int ,int> aux;
char aa;

int main()
{
    fin >> n >> m;
    int i,j;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            fin >> aa;
            if(aa=='*') /// Perete
                v[i][j]=-1;
            if(aa=='D') /// Dragon
            {
                q.push(make_pair(i,j));
                v[i][j]=1;
            }
            if(aa=='I')
            {
                xin=i; /// coordonate de intrare (al lui Paftenie)
                yin=j;
            }
            if(aa=='O')
            {
                xout=i; /// coordonate de iesire din pivnita
                yout=j;
            }
        }
    }
    while(!q.empty())
    {
        aux=q.front();
        q.pop();
        xi=aux.first;
        yi=aux.second;
        for(i=1;i<=4;i++)
        {
            xa=xi+dx[i];
            ya=yi+dy[i];
            if(v[xa][ya]==0 and Interior(xa,ya))
            {
                v[xa][ya]=v[xi][yi]+1;
                aux.first=xa;
                aux.second=ya;
                q.push(aux); /// q.push(make_pair( .....)) ?

            }
        }
    }
    /// PARTEA CU CAUTAREA BINARA ============>>
    int st=1,dr=max(n,m);
    int ok=1,ok2;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        aux.first=xin;
        aux.second=yin;
        while(!q.empty()) q.pop();
        if(mij>v[xin][yin]) /// v[xin][yin] e cea mai aporpiata distanta de un dragon
        {
            dr=mij-1;
            continue;
        }
        v2[xin][yin]=mij;
        q.push(aux); /// q.push(make_pair(aux.first,aux.second)) ?
        ok2=0;
        while(!q.empty() and ok2==0)
        {
            aux=q.front();
            q.pop();
            xi=aux.first;
            yi=aux.second;
            for(i=1;i<=4;i++)
            {
                xa=xi+dx[i];
                ya=yi+dy[i];
                if(v[xa][ya]>=mij and Interior(xa,ya) and v2[xa][ya]!=mij)
                {
                    v2[xa][ya]=mij;
                    aux.first=xa;
                    aux.second=ya;
                    q.push(aux); /// q.push(make_pair( .....)) ?
                    if(xa==xout and ya==yout)
                        ok2=1;/// daca se afla la iesirea din temnita (s-a terminat drumul)
                }
            }
        }
        if(ok2==1) /// daca s-a iesit din temnita
            st=mij+1;
        else dr=mij-1;
    }

    if(dr-1>=0) fout << dr-1;
    else fout << -1;
    return 0;
}