Cod sursa(job #2797012)

Utilizator AndreidreiGresoiu Liviu-Andrei Andreidrei Data 9 noiembrie 2021 10:03:47
Problema Barbar Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <bits/stdc++.h>
#define din cin
#define dout out
#define pi 3.14159265359
#define sw(x,y) x^=y,y^=x,x^=y
#define bmin(a,b)((a<b)?(a):(b))
#define bmax(a,b)((a>b)?(a):(b))
#define ll long long
#define forq(i,ii,n)for(i=ii;i<n;i++)
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
char a[1003][1003];int i,j,n,m,s,d[1003][1003],p[2],pp[2],b[2][1000000],u,l,k;bitset<1003>c[1003];pair<int,int>r[1000000],rr;
#define qq(x,y)if(a[x][y]!='*'&&c[x][y]==0)c[x][y]=1,b[0][l]=x,b[1][l]=y,++l,d[x][y]=s;
#define tt(x,y)if(a[x][y]!='*'&&c[x][y])c[x][y]=0,r[l]=make_pair(x,y),push_heap(r,r+ ++l,f);
inline bool f(pair<int,int>x,pair<int,int>y)
{
return d[x.first][x.second]<d[y.first][y.second];
}
int main()
{
in>>n>>m;
for(i=1;i<=n;i++)
    for(j=1;j<=m;j++)
    {
        in>>a[i][j],d[i][j]=INT_MAX;
        switch(a[i][j])
        {
        case 'O':p[0]=i,p[1]=j;break;
        case 'I':pp[0]=i,pp[1]=j;break;
        case 'D':b[0][l]=i,b[1][l]=j,++l,c[i][j]=1;
        }
    }
for(i=1;i<=n;i++)
    a[i][0]=a[i][m+1]='*';
for(i=1;i<=m;i++)
    a[0][i]=a[n+1][i]='*';
while(u<l)
{
    k=l,++s;
    while(u<k)
    {
        qq(b[0][u]+1,b[1][u]);
        qq(b[0][u]-1,b[1][u]);
        qq(b[0][u],b[1][u]+1);
        qq(b[0][u],b[1][u]-1);
        ++u;
    }
}
for(i=0;i<1003;i++)c[i].set();
l=1,k=INT_MAX;r[0]=make_pair(p[0],p[1]);c[p[0]][p[1]]=0;
while(c[pp[0]][pp[1]]&&l)
{
    rr=r[0];
    pop_heap(r,r+l,f);
    k=bmin(k,d[rr.first][rr.second]);
    --l;
    tt(rr.first+1,rr.second);
    tt(rr.first-1,rr.second);
    tt(rr.first,rr.second+1);
    tt(rr.first,rr.second-1);
    //cout<<l;
}
if(l)
    out<<k;
    else out<<-1;
}