Cod sursa(job #3241784)

Utilizator AndreiNicolaescuEric Paturan AndreiNicolaescu Data 3 septembrie 2024 19:57:21
Problema Barbar Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("barbar.in");
ofstream cout("barbar.out");
const int Nmax = 1005;
const int di[] = {-1, 1, 0, 0};
const int dj[] = {0, 0, -1, 1};
int n, m, a[Nmax][Nmax], viz[Nmax][Nmax], xs, ys, xf, yf, dist[Nmax][Nmax], v[Nmax][Nmax];
char c;
queue <pair<int, int> > q;
void reset()
{
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            viz[i][j] = 0;
}
bool inmat(int x,int y)
{
    return x >= 1 && y >= 1 && x <= n && y <= m;
}
void lee()
{
    while(!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;


        for(int d=0; d<=3; d++)
        {
            int inou = di[d] + x;
            int jnou = dj[d] + y;
            if(inmat(inou, jnou) && !a[inou][jnou]  && !dist[inou][jnou])
            {
                dist[inou][jnou] = dist[x][y] + 1;
                q.push({inou,jnou});
            }
        }
        q.pop();
    }
}
void lee2(int g)
{
    reset();
    if(dist[xs][ys] < g)
        return;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            if(dist[i][j] >= g)
                v[i][j] = 1;
            else
                v[i][j] = 0;
    queue<pair<int, int>> qq;
    qq.push({xs, ys});
    viz[xs][ys] = 1;
    while(!qq.empty())
    {
        int x = qq.front().first;
        int y = qq.front().second;

        qq.pop();
        for(int d=0; d<=3; d++)
        {
            int inou = di[d] + x;
            int jnou = dj[d] + y;
            if(inmat(inou, jnou) && !viz[inou][jnou] && v[inou][jnou])
            {
                viz[inou][jnou] = 1;
                qq.push({inou, jnou});
            }
        }

    }
}
int main()
{
    cin >> n >> m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            cin >> c;
            if(c == '.')
                a[i][j] = 0;
            else
                if(c == '*')
                    a[i][j] = -1;
                else
                    if(c == 'D')
                    {
                        q.push({i, j});
                        dist[i][j] = 0;
                    }
                    else
                        if(c == 'I')
                            xs = i, ys = j;
                        else
                            xf = i, yf = j;
        }
    lee();
    int st = 0, dr = n + m + 1;
    bool ok = false;
    while(dr - st > 1)
    {
        int mij = (st + dr)/ 2;
        lee2(mij);
        bool var = viz[xf][yf];
        if(var == 1)
            st = mij, ok = 1;
        else
            dr = mij;
    }
    if(ok == 0)
        cout << "-1";
    else
        cout << st;
    /*for(int i=1; i<=n; i++,cout << endl)
        for(int j=1; j<=m; j++)
            cout << dist[i][j] << " ";
    cout  << endl;
    lee2(2);
    for(int i=1; i<=n; i++,cout << endl)
        for(int j=1; j<=m; j++)
            cout << v[i][j] << " ";*/
    return 0;
}