Cod sursa(job #1254397)

Utilizator DenisacheDenis Ehorovici Denisache Data 2 noiembrie 2014 17:49:28
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.2 kb
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
//#include <windows.h>
//#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <limits.h>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
#define forA(V,it) for (typeof(V.begin()) it=V.begin();it!=V.end();it++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define MOD 666013
#define INF (1<<31)-1
#define MINF -(1<<31)
#define vi vector <int>
#define vll vector <ll>
#define pii pair <int,int>
#define pll pair <ll,ll>
#define newl printf("\n")
#define DIM 1005
int N,M,i,j,sol[1005][1005],maxim;
bool viz[1005][1005];
char mat[1005][1005];
int d1[]={1,0,-1,0};
int d2[]={0,1,0,-1};
struct point
{
    int x,y;
};
point start,finish;
queue <point> Q;
void BFS()
{
    point now,k;
    while (!Q.empty())
    {
        k=Q.front();
        for (i=0;i<4;i++)
        {
            now.x=k.x+d1[i];
            now.y=k.y+d2[i];
            if (mat[now.x][now.y]!='*' && !viz[now.x][now.y] && now.x>0 && now.x<=N && now.y>0 && now.y<=M)
            {
                Q.push(now);
                viz[now.x][now.y]=true;
                sol[now.x][now.y]=sol[k.x][k.y]+1;
                maxim=max(maxim,sol[now.x][now.y]);
            }
        }
        Q.pop();
    }
}
bool ok(int d)
{
    if (sol[start.x][start.y]<d) return false;
    memset(viz,false,sizeof(viz));
    viz[start.x][start.y]=true;
    Q.push(start);
    point k,now;
    while (!Q.empty())
    {
        k=Q.front();
        for (i=0;i<4;i++)
        {
            now.x=k.x+d1[i];
            now.y=k.y+d2[i];
            if (sol[now.x][now.y]>=d && !viz[now.x][now.y] && mat[now.x][now.y]!='*' && now.x>0 && now.x<=N && now.y>0 && now.y<=M)
            {
                if (now.x==finish.x && now.y==finish.y)
                {
                    while (!Q.empty()) Q.pop();
                    return true;
                }
                Q.push(now);
                viz[now.x][now.y]=true;
            }
        }
        Q.pop();
    }
    return false;
}
int cb()
{
    int L=1,R=maxim,sol=-1;
    while (L<=R)
    {
        int Mid=(L+R)>>1;
        if (ok(Mid)) sol=Mid,L=Mid+1;
        else R=Mid-1;
    }
    return sol;
}
int main()
{
    freopen("barbar.in","r",stdin);
    freopen("barbar.out","w",stdout);
    scanf("%d %d\n",&N,&M);
    point now;
    for (i=1;i<=N;i++)
    {
        gets(mat[i]+1);
        for (j=1;j<=M;j++)
        {
            if (mat[i][j]=='I')
            {
                start.x=i,start.y=j;
                mat[i][j]='.';
            }
            else if (mat[i][j]=='O')
            {
                finish.x=i,finish.y=j;
                mat[i][j]='.';
            }
            else if (mat[i][j]=='D')
            {
                now.x=i,now.y=j;
                viz[i][j]=true;
                Q.push(now);
                mat[i][j]='.';
            }
        }
    }
    BFS();
    printf("%d",cb());
    return 0;
}