Cod sursa(job #1820450)

Utilizator Vally77FMI Calinescu Valentin Gelu Vally77 Data 1 decembrie 2016 18:58:04
Problema Barbar Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.81 kb
#include <fstream>
#include <queue>
using namespace std;
ifstream ka("barbar.in");
ofstream ki("barbar.out");

struct punct
{
    int x, y;
    int dist;
    bool operator < (const punct arg) const
    {
        return dist > arg.dist;
    }
};

struct punct2
{
    int x, y;
    int dist;
    bool operator < (const punct2 arg) const
    {
        return dist < arg.dist;
    }
};

queue <punct> coada;
priority_queue <punct2> dijk;

const int N_MAX = 1000;
char c;
int destx, desty, x, y, n, m, a[N_MAX + 1][N_MAX + 1], distante[N_MAX + 1][N_MAX + 1], sol[N_MAX + 1][N_MAX + 1];
int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
bool marcat[N_MAX + 1][N_MAX + 1];

bool bun(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

int dijkstra()
{
    while(!dijk.empty())
    {
        punct2 p = dijk.top();
        dijk.pop();
        int x = p.x, y = p.y, dist = p.dist;
        if(x == destx && y == desty)
            return sol[x][y];
        for(int i = 0; i < 4; i++)
            if(bun(x + dx[i], y + dy[i]) && a[x + dx[i]][y + dy[i]] != -1 && sol[x + dx[i]][y + dy[i]] < min(dist, distante[x + dx[i]][y + dy[i]]))
            {
                sol[x + dx[i]][y + dy[i]] = min(dist, distante[x + dx[i]][y + dy[i]]);
                punct2 pp;
                pp.x = x + dx[i];
                pp.y = y + dy[i];
                pp.dist = min(dist, distante[x + dx[i]][y + dy[i]]);
                dijk.push(pp);
            }
    }
    return -1;
}

int main()
{
    ka >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            ka >> c;
            sol[i][j] = -1;
            if(c == 'I')
            {
                x = i;
                y = j;
            }
            else if(c == 'D')
            {
                punct p;
                p.x = i;
                p.y = j;
                p.dist = 0;
                marcat[i][j] = true;
                coada.push(p);
            }
            else if(c == 'O')
            {
                destx = i;
                desty = j;
            }
            else if(c == '*')
                a[i][j] = -1;
            else
                a[i][j] = 0;
        }
    while(!coada.empty())
    {
        punct p = coada.front();
        coada.pop();
        int x = p.x, y = p.y, dist = p.dist;
        distante[x][y] = dist;
        for(int i = 0; i < 4; i++)
            if(bun(x + dx[i], y + dy[i]) && a[x + dx[i]][y + dy[i]] == 0 && !marcat[x + dx[i]][y + dy[i]])
            {
                marcat[x + dx[i]][y + dy[i]] = true;
                punct pp;
                pp.x = x + dx[i];
                pp.y = y + dy[i];
                pp.dist = dist + 1;
                coada.push(pp);
            }
    }
    punct2 p;
    p.x = x;
    p.y = y;
    p.dist = distante[x][y];
    sol[x][y] = distante[x][y];
    dijk.push(p);
    ki << dijkstra();
}