Cod sursa(job #2673044)

Utilizator BereaBerendea Andrei Berea Data 15 noiembrie 2020 17:51:59
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda ursus_retro_fara_alcool Marime 3.02 kb
//#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>

using namespace std;
const int maxn=1e3+1;
int n,m,i,j,x,t;
int a[maxn][maxn];
int ras=0;
char c;
pair<int,int>ION;
pair<int,int>IES;
queue<pair<int,int> >q;
queue<pair<int,int> >cox1;
queue<pair<int,int> >cox2;
vector<pair<int,int> >v;

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

void lee()
{
    q.push(make_pair(ION.first,ION.second));
    while (q.size()>0)
    {
        i=q.front().first;
        j=q.front().second;
        x=a[i][j];
        if (a[i+1][j]==0 && i+1<=n)
        {
            q.push(make_pair(i+1,j));
            a[i+1][j]=x+1;
        }
        if (a[i-1][j]==0 && i-1>=1)
        {
            q.push(make_pair(i-1,j));
            a[i-1][j]=x+1;
        }
        if (a[i][j+1]==0 && j+1<=m)
        {
            q.push(make_pair(i,j+1));
            a[i][j+1]=x+1;
        }
        if (a[i][j-1]==0 && j-1>=1)
        {
            q.push(make_pair(i,j-1));
            a[i][j-1]=x+1;
        }
        q.pop();
    }
}

void kkt()
{
    if (cox1.size()>0)
    {
        while (cox1.size()>0)
        {
            i=cox1.front().first;
            j=cox1.front().second;
            a[i][j]=-1;
            if (a[i+1][j]==0 && i+1<=n) cox2.push(make_pair(i+1,j));
            if (a[i-1][j]==0 && i-1>=1) cox2.push(make_pair(i-1,j));
            if (a[i][j+1]==0 && j+1<=m) cox2.push(make_pair(i,j+1));
            if (a[i][j-1]==0 && j-1<=n) cox2.push(make_pair(i,j-1));
            cox1.pop();
        }
    }
    else
    {
        while (cox2.size()>0)
        {
            i=cox2.front().first;
            j=cox2.front().second;
            a[i][j]=-1;
            if (a[i+1][j]==0 && i+1<=n) cox1.push(make_pair(i+1,j));
            if (a[i-1][j]==0 && i-1>=1) cox1.push(make_pair(i-1,j));
            if (a[i][j+1]==0 && j+1<=m) cox1.push(make_pair(i,j+1));
            if (a[i][j-1]==0 && j-1<=n) cox1.push(make_pair(i,j-1));
            cox2.pop();
        }
    }
}

int main()
{
    cin>>n>>m;
    for (i=1;i<=n;i++) for (j=1;j<=m;j++)
    {
        cin>>c;
        if (c=='O')
        {
            IES.first=i;
            IES.second=j;
        }
        else if (c=='I')
        {
            ION.first=i;
            ION.second=j;
        }
        else if (c=='*') v.push_back(make_pair(i,j));
        else if (c=='D') cox1.push(make_pair(i,j));
    }
    for (t=0;t<=2*n;t++)
    {
        memset(a,0,sizeof(a));
        for (j=0;j<v.size();j++) a[v[j].first][v[j].second]=-1;
        kkt();
        if (a[ION.first][ION.second]<0)
        {
            cout<<ras;
            return 0;
        }
        lee();
        /*for (i=1;i<=n;i++)
        {
            for (j=1;j<=m;j++) cout<<a[i][j]<<" ";
            cout<<"\n";
        }
        cout<<"\n\n";*/
        if (a[IES.first][IES.second]>0 /*&& a[ION]*/) ras=t;
        else
        {
            cout<<ras;
            return 0;
        }
    }
}