Cod sursa(job #2857773)

Utilizator C_R_I_S_T_I_2_3Cristi Gavrila C_R_I_S_T_I_2_3 Data 26 februarie 2022 12:20:13
Problema Barbar Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.81 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int n, m, xs, ys, xf, yf, maxim;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int a[1005][1005], b[1005][1005];
struct coord
{
    int x, y;
};
queue<coord> q;
stack<coord> s;
inline bool Interior(int x, int y)
{
    if (x < 1 || x > n || y < 1 || y > m)
        return false;
    return true;
}
inline void Lee()
{
    coord w1, w2;
    while (!q.empty())
    {
        w1 = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            w2.x = w1.x + dx[i];
            w2.y = w1.y + dy[i];
            if (Interior(w2.x, w2.y) && a[w2.x][w2.y] == 0 && (b[w2.x][w2.y] == 0 || b[w2.x][w2.y] > b[w1.x][w1.y] + 1))
            {
                b[w2.x][w2.y] = b[w1.x][w1.y] + 1;
                q.push(w2);
            }
        }
    }
}
inline void Fill(int xs, int ys, int v1, int v2)
{
    coord w1;
    w1.x = xs;
    w1.y = ys;
    s.push(w1);
    a[w1.x][w1.y] = v2;
    while (!s.empty())
    {
        w1 = s.top();
        s.pop();
        for (int i = 0; i < 4; i++)
        {
            int x, y;
            x = w1.x + dx[i];
            y = w1.y + dy[i];
            if (Interior(x, y) && a[x][y] >= v1 && a[x][y] != v2)
            {
                coord w2;
                w2.x = x;
                w2.y = y;
                s.push(w2);
                a[x][y] = v2;
            }
        }
    }
}
inline void Citire()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char c;
            fin >> c;
            if (c == '*')
                a[i][j] = 1;
            if (c == 'D')
            {
                a[i][j] = 1;
                coord aux;
                aux.x = i;
                aux.y = j;
                q.push(aux);
            }
            if (c == 'I')
                xs = i, ys = j;
            if (c == 'O')
                xf = i, yf = j;
        }
    }
}
inline void Copiere()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            a[i][j] = b[i][j];
        }
    }
}
void Afisare()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            fout << a[i][j] << " ";
        }
        fout << "\n";
    }
}
int main()
{
    Citire();
    Lee();
    Copiere();
    // Afisare();
    maxim = -1;
    int aux = max(n, m);
    for (int i = 1; i <= aux; i ++)
    {
        Fill(xs, ys, i, -2);
        if(a[xf][yf] == -2 && b[xs][ys] >= i)
        {
            maxim = i;
        }
        else
            break;
        Copiere();
    }
    // Fill(xs, ys, 2, 0);
    // Afisare();
    fout << maxim;
    return 0;
}