Cod sursa(job #2671424)

Utilizator Leonard123Mirt Leonard Leonard123 Data 12 noiembrie 2020 08:42:30
Problema Barbar Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.94 kb
#include <bits/stdc++.h>
#define maxn 1005
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;

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

int linii, coloane, dl[] = {1, 0, -1, 0}, dc[] = {0, 1, 0, -1};
short distanta[maxn][maxn], drum[maxn][maxn];
PII start, stop, current_poz, next_poz;
char temnita[maxn][maxn];
queue <PII> q;

void calculeaza_distante(){
    while(!q.empty()) {
        current_poz = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            next_poz.fi = current_poz.fi + dl[i];
            next_poz.se = current_poz.se + dc[i];
            if (temnita[next_poz.fi][next_poz.se] != '*')
                if (distanta[next_poz.fi][next_poz.se] > distanta[current_poz.fi][current_poz.se] + 1) {
                   distanta[next_poz.fi][next_poz.se] = distanta[current_poz.fi][current_poz.se] + 1;
                   q.push(make_pair(next_poz.fi, next_poz.se));
            }
        }
    }
}

void calcueaza_drum() {
    q.push(make_pair(start.fi, start.se));
    drum[start.fi][start.se] = distanta[start.fi][start.se];
    while (!q.empty()) {
        current_poz = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            next_poz.fi = current_poz.fi + dl[i];
            next_poz.se = current_poz.se + dc[i];
            if (distanta[next_poz.fi][next_poz.se]  > 0)
                if (drum[next_poz.fi][next_poz.se] < min (drum[current_poz.fi][current_poz.se], distanta[next_poz.fi][next_poz.se])) {
                    drum[next_poz.fi][next_poz.se] = min (drum[current_poz.fi][current_poz.se], distanta[next_poz.fi][next_poz.se]);
                    q.push(make_pair(next_poz.fi, next_poz.se));
                }
        }
    }
}

int main() {
    fin >> linii >> coloane;
    for (int i = 1; i <= linii; i++)
        for (int j = 1; j <= coloane; j++) {
            fin >> temnita[i][j];
            if (temnita[i][j] == '.') {
                distanta[i][j] = maxn;
            } else if (temnita[i][j] == '*') {
                distanta[i][j] = -1;
                drum[i][j] = -1;
            } else if (temnita[i][j] == 'D') {
                q.push(make_pair(i, j));
            } else if (temnita[i][j] == 'I') {
                start.fi = i;
                start.se = j;
                distanta[i][j] = maxn;
            } else {
                stop.fi = i;
                stop.se = j;
                drum[i][j] = -1;
                distanta[i][j] = maxn;
            }
        }
    for(int i = 1; i <= linii; i++) {
        temnita[i][0] = temnita[i][coloane + 1] = '*';
        distanta[i][0] = distanta[i][coloane + 1] = -1;
    }

    for (int j = 1; j <= coloane; j++) {
        temnita[0][j] = temnita[linii + 1][j] = '*';
        distanta[0][j] = distanta[linii + 1][j] = - 1;
    }

    calculeaza_distante();
    calcueaza_drum();
    fout << drum[stop.fi][stop.se];
    return 0;
}