Cod sursa(job #1404664)

Utilizator Ionut228Ionut Calofir Ionut228 Data 28 martie 2015 13:54:28
Problema Barbar Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <fstream>
#include <queue>
#include <algorithm>

using namespace std;

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

const int INF = 1000005;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};

char A[1005][1005], snul[1005];
int R, C, solmax;
int xinit, yinit, xfin, yfin;
int D[1005][1005], sol[1005][1005];
queue<pair<int, int> > Q, QD;

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

        for (int k = 0; k < 4; ++k)
        {
            int nowi = i + dx[k];
            int nowj = j + dy[k];

            if (nowi > R || nowi < 1 || nowj > C || nowj < 1)
                continue;
            if (A[nowi][nowj] == '*')
                continue;

            if (D[i][j] + 1 < D[nowi][nowj])
            {
                D[nowi][nowj] = D[i][j] + 1;
                QD.push(make_pair(nowi, nowj));
            }
        }
    }
}

void solve()
{
    sol[xinit][yinit] = D[xinit][yinit];
    Q.push(make_pair(xinit, yinit));

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

        for (int k = 0; k < 4; ++k)
        {
            int nowi = i + dx[k];
            int nowj = j + dy[k];

            if (nowi > R || nowi < 1 || nowj > C || nowj < 1)
                continue;
            if (A[nowi][nowj] == '*')
                continue;

            int nowdist = min(sol[i][j], D[nowi][nowj]);
            if (A[nowi][nowj] == 'O')
            {
                solmax = max(solmax, nowdist);
                continue;
            }

            if (nowdist > sol[nowi][nowj]) // vad daca pot imbunatati
            {
                sol[nowi][nowj] = nowdist;
                Q.push(make_pair(nowi, nowj));
            }
        }
    }
}

int main()
{
    fin >> R >> C;
    fin.getline(snul, 1005);
    for (int i = 1; i <= R; ++i)
    {
        fin.getline(A[i] + 1, 1005);
        for (int j = 1; j <= C; ++j)
        {
            D[i][j] = INF;
            sol[i][j] = -1;
            if (A[i][j] == 'I')
            {
                xinit = i;
                yinit = j;
            }
            else if (A[i][j] == 'O')
            {
                xfin = i;
                yfin = j;
            }
            else if (A[i][j] == 'D')
            {
                D[i][j] = 0;
                QD.push(make_pair(i, j));
            }
        }
    }

    goDragon();

    solmax = -1;
    solve();

    fout << solmax << '\n';

    fin.close();
    fout.close();
    return 0;
}