Cod sursa(job #1723104)

Utilizator antracodRadu Teodor antracod Data 29 iunie 2016 18:08:13
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.3 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;

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


queue<int> Qx;
queue<int> Qy;

int n,m;

int v[1000][1000];
int a[1000][1000];

int dl[]= {0,0,1,-1};
int dc[]= {1,-1,0,0};

int startx,starty,stopx,stopy;
void citire()
{
    int i,j;
    char c;
    in>>n>>m;

    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            in>>c;
            if(c=='.')
            {
                v[i][j]=0;
            }
            else if(c=='D')
            {
                v[i][j]=1;
                Qx.push(i);
                Qy.push(j);
            }
            else if(c=='I')
            {
                startx=i;
                starty=j;
                v[i][j]=0;
            }
            else if(c=='O')
            {
                stopx=i;
                stopy=j;
                v[i][j]=-2;
            }
            else if(c=='*')
            {
                v[i][j]=-3;
            }
        }
    }
}

int safe(int k,int n)
{
    if(Qx.front()+dl[k]<n && Qx.front()+dl[k]>=0 && Qy.front()+dc[k]<m && Qy.front()+dc[k]>=0)
        return 1;
    return 0;
}

void genmat()
{
    int k;
    while(Qx.empty()==0)
    {
        for(k=0; k<4; k++)
        {
            if(safe(k,n)==1)
            {
                if(v[Qx.front()+dl[k]][Qy.front()+dc[k]]==0)
                {
                    Qx.push(Qx.front()+dl[k]);
                    Qy.push(Qy.front()+dc[k]);
                    v[Qx.front()+dl[k]][Qy.front()+dc[k]]=v[Qx.front()][Qy.front()]+1;
                }
            }
        }
        Qx.pop();
        Qy.pop();
    }

}
void clean()
{
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            a[i][j]=0;
        }
    }
    a[startx][starty]=-1;
    a[stopx][stopy]=-2;
    while(Qx.empty()==0)
    {
        Qx.pop();
        Qy.pop();
    }
    Qx.push(startx);
    Qy.push(starty);


}

bool bfs(int mid)
{
    int k,i,j;
    if(v[startx][starty]<mid)
        return 0;
    while(Qx.empty()==0)
    {
        for(k=0; k<4; k++)
        {
            if(safe(k,n)==1)
            {
                if(Qx.front()+dl[k]==stopx && Qy.front()+dc[k]==stopy)
                {
                    return 1;
                }
                else if(v[Qx.front()+dl[k]][Qy.front()+dc[k]]>=mid && a[Qx.front()+dl[k]][Qy.front()+dc[k]]==0)
                {
                    Qx.push(Qx.front()+dl[k]);
                    Qy.push(Qy.front()+dc[k]);
                    a[Qx.front()+dl[k]][Qy.front()+dc[k]]=a[Qx.front()][Qy.front()]+1;
                }
            }
        }
        Qx.pop();
        Qy.pop();

    }
    return 0;
}

int BinarySearch()
{
    int answer=-1,mid,x;
    int low=1;
    int high=1000;
    while(low<=high)
    {
        clean();
        mid=low+(high-low)/2;

        x=bfs(mid);
        if(x==1)   /// 1 se poate
        {
            answer=mid;
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }

    }
    if(answer!=-1 && answer!=1)
        return answer-1;
    else
        return -1;
}

int main()
{
    citire();
    genmat();
    out<<BinarySearch();
}