Cod sursa(job #2943256)

Utilizator YosifIosif Andrei Stefan Yosif Data 20 noiembrie 2022 19:22:18
Problema Barbar Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 8.21 kb
#include <bits/stdc++.h>
using namespace std;

class input {

private:

    FILE* fin;
    const int sz = 10000;
    char* buff, c;
    int p;
    const char stop = char(-51);

    char read()
    {
        if (p == sz)
        {
            fread(buff, 1, sz, fin);
            p = 0;
            return buff[p++];
        }
        else
            return buff[p++];
    }

    bool digit(char c)
    {
        return c >= '0' && c <= '9';
    }

public:

    input(const char* name)
    {
        c = '-';
        buff = new char[sz];
        p = 0;
        fin = fopen(name, "r");
        fread(buff, 1, sz, fin);
    }

    void close()
    {
        fclose(fin);
    }

    void open(const char* name)
    {
        c = '-';
        p = sz;
        fin = fopen(name, "r");
    }

    bool eof()
    {
        int i = p;

        while (true)
        {
            while (i < sz && (buff[i] == ' ' || buff[i] == '\n'))
                i++;

            if (i != sz)
            {
                if (buff[i] == stop)
                    return 1;
                return 0;
            }
            else
            {
                fread(buff, 1, sz, fin);
                p = 0;
                i = 0;
            }
        }
    }

    input& operator >> (int& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (unsigned long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (char& ch)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        ch = c;

        return *this;
    }

    input& operator >> (string& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = "";

        while (c != ' ' && c != '\n' && c != '\0')
            n += c, c = read();

        return *this;
    }

    input& operator >> (double& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        if (c != '.')
            return *this;

        c = read();

        double p10 = 10;

        while (digit(c))
            n = n + double(c - '0') / p10, c = read(), p10 *= 10;

        return *this;
    }

    input& operator >> (char* s)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        while (c != ' ' && c != '\n')
            *s = c, s++, c = read();

        *s = '\0';

        return *this;
    }

    void getline(string& s)
    {
        s = "";

        c = read();

        while (c == ' ')
            c = read();

        while (c != '\n')
            s += c, c = read();
    }
};
class output {

private:
    FILE* fout;
    const int sz = 10000;
    char* buff;
    int p;

    void write(char ch)
    {
        if (p == sz)
        {
            fwrite(buff, 1, sz, fout);
            p = 0;
            buff[p++] = ch;
        }
        else
            buff[p++] = ch;
    }

public:

    output(const char* name)
    {
        buff = new char[sz];
        p = 0;
        fout = fopen(name, "w");
    }

    ~output()
    {
        fwrite(buff, 1, p, fout);
    }

    output& operator << (int n)
    {
        if (n < 0)
            write('-'), n *= -1;

        if (n == 0)
        {
            write(n + '0');
            return *this;
        }

        if (n / 10 != 0)
            *this << (n / 10);

        write(('0' + (n % 10)));

        return *this;
    }

    output& operator << (long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (unsigned long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (char c)
    {
        write(c);
        return *this;
    }

    void precision(double n, int x)
    {
        *this << int(n);

        if (!x)
            return;

        write('.');

        n -= int(n);
        n *= 10;

        for (int i = 1; i <= x; i++)
        {
            *this << int(n);
            n -= int(n);
            n *= 10;
        }
    }

    output& operator << (string n)
    {
        for (auto i : n)
            write(i);
        return *this;
    }
};

ifstream fin("barbar.in");
ofstream fout("barbar.out");

int n, m, minDst[1001][1001];
char a[1001][1001];
pair <int, int> Begin, End;

int d1[] = { 0, -1, 0, 1, 0 };
int d2[] = { 0, 0, 1, 0, -1 };

struct nod {

    int i, j;

    bool operator < (const nod& aux) const
    {
        return minDst[i][j] > minDst[aux.i][aux.j];
    }
};

inline bool in(int i, int j)
{
    return i >= 1 && i <= n && j >= 1 && j <= m;
}

bool lee(int val)
{
    bitset <1001> f[1001];

    queue <pair<int, int>> Q;   
    Q.push(Begin);
    f[Begin.first][Begin.second] = 1;

    while (!Q.empty())
    {
        int i = Q.front().first;
        int j = Q.front().second;
        Q.pop();

        if (End == make_pair(i, j))
            return true;

        for (int k = 1; k <= 4; k++)
        {
            int x = i + d1[k];
            int y = j + d2[k];

            if (in(x, y) && a[x][y] != '*' && minDst[x][y] >= val && !f[x][y])
            {
                f[x][y] = 1;
                Q.push({ x, y });
            }
        }
    }

    return false;
}

int main()
{
    fin.tie(0);
    fout.tie(0);
    fin >> n >> m;

    priority_queue <nod> Q;

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            fin >> a[i][j];

            if (a[i][j] == 'I')
                Begin = { i, j }, minDst[i][j] = INT_MAX;
            else if (a[i][j] == 'O')
                End = { i, j }, minDst[i][j] = INT_MAX;
            else if (a[i][j] == 'D')
                minDst[i][j] = 0, Q.push({ i, j });
            else
                minDst[i][j] = INT_MAX;
        }

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            fin >> a[i][j];

    while (!Q.empty())
    {
        int i = Q.top().i;
        int j = Q.top().j;
        Q.pop();

        for (int k = 1; k <= 4; k++)
        {
            int x = i + d1[k];
            int y = j + d2[k];

            if (in(x, y) && a[x][y] != '*' && minDst[x][y] > minDst[i][j] + 1)
            {
                minDst[x][y] = minDst[i][j] + 1;
                Q.push({ x, y });
            }
        }
    }

    int l = 1, r = min(minDst[Begin.first][Begin.second], minDst[End.first][End.second]), poz = -1;

    while (l <= r)
    {
        int m = (l + r) / 2;

        if (lee(m))
        {
            poz = m;
            l = m + 1;
        }
        else
            r = m - 1;
    }

    fout << poz;

    return 0;
}