Cod sursa(job #1850377)

Utilizator Matei_IgnutaMatei Ignuta Matei_Ignuta Data 18 ianuarie 2017 16:59:36
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <stdio.h>
#include <iostream>
#include <queue>

#define INF 1<<30
using namespace std;
int d[1010][1010];
bool a[1010][1010];
struct tip{int x, y;};
queue <tip> q;
int dl[]={1, 0, -1, 0};
int dc[]={0, 1, 0, -1};
char citit;
tip start, aux, y, finish;
int m, n;
int citire()
{
    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++)
        {
            scanf("%c", &citit);
            if(citit=='.')
                d[i][j]=INF;
            else if(citit=='I')
            {
                start.x=i;
                start.y=j;
                d[i][j]=INF;
            }
            else if(citit=='D')
                {
                    d[i][j]=0;
                    aux.x=i;
                    aux.y=j;
                    q.push(aux);
                }
            else if(citit=='*')
                d[i][j]=-1;
            else if(citit=='O')
                {
                    finish.x=i;
                    finish.y=j;
                    d[i][j]=INF;
                }
            else
                cout<<"-1753";
        }
        scanf("\n");
    }
    for(int i=0; i<=m+1; i++)
    {
        d[i][0]=-1;
        d[i][n+1]=-1;
    }
    for(int i=0; i<=n+1; i++)
    {
        d[0][i]=-1;
        d[0][m+1]=-1;
    }
        for(int i=0; i<=m+1; i++)
    {
        a[i][0]=true;
        a[i][n+1]=true;
    }
    for(int i=0; i<=n+1; i++)
    {
        a[0][i]=true;
        a[0][m+1]=true;
    }
}
void bfs()
{
    while(!q.empty())
    {
        tip aux=q.front();
        q.pop();
        for(int i=0; i<=3; ++i)
            {
                y.x=aux.x+dl[i];
                y.y=aux.y+dc[i];
                    if(d[y.x][y.y]==INF)
                    {
                        q.push(y);
                        d[y.x][y.y]=d[aux.x][aux.y]+1;
                    }
            }
    }
}
bool pot(int val)
{
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            a[i][j]=false;
    q.push(start);
    a[start.x][start.y]=true;
    if(d[start.x][start.y]<val) return false;
    while(!q.empty())
    {
        tip aux=q.front();
        q.pop();
        for(int i=0; i<=3; ++i)
            {
                y.x=aux.x+dl[i];
                y.y=aux.y+dc[i];
                    if(d[y.x][y.y]>=val and a[y.x][y.y]==false)
                    {
                        q.push(y);
                        a[y.x][y.y]=true;
                    }
            }
    }
    if(a[finish.x][finish.y]==true) return true;
    return false;
}
int cautbin()
{
    int i, step=1<<30;
    for(i=0; step; step >>=1)
        if(pot(i+step))
            i+=step;
    return i;
}
int main()
{
    freopen("barbar.in", "r", stdin);
    freopen("barbar.out", "w", stdout);
    scanf("%d%d\n", &m, &n);
    citire();
    bfs();
    int ans=cautbin();
    if(ans==0)
    {
        printf("-1");
        return 0;
    }
    printf("%d", cautbin());
    return 0;
}