Cod sursa(job #3042326)

Utilizator rutakateIvanovici Vlad rutakate Data 5 aprilie 2023 19:38:26
Problema BFS - Parcurgere in latime Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, sir[1004];

void functie(int mt[105][105], int poz, int contor, int initial) {
   // cout << "contor = " << contor << endl;
    //cout << "poz = " << poz << endl;
    if(poz == initial) {
        contor = 0;
    }
    for(int i = 1; i <= n; ++i) {
        if(mt[poz][i] == 1) {
            if(sir[i] > contor + 1 || sir[i] == -1) sir[i] = contor + 1;
            mt[poz][i] = 0;
            return functie(mt, i, contor + 1, initial);
        }
    }
    return;
}

int main()
{
    int m, s;
    fin >> n >> m >> s;
    for(int i = 1; i <= n; ++i) {
        sir[i] = -1;
    }
    sir[s] = 0;
    int mt[105][105];
    int x, y;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            mt[i][j] = 0;
        }
    }
    for(int i = 0; i < m; ++i) {
        fin >> x >> y;
        mt[x][y] = 1;
    }

    functie(mt, s, 0, s);
    for(int i = 1; i <= n; ++i) {
       fout << sir[i] << " ";
    }
  //  cout << endl;
 //   for(int i = 1; i <= n; ++i) {
 //       for(int j = 1; j <= n; ++j) {
   //         cout << mt[i][j] << " ";
  //      }
   //     cout << endl;
  //  }
    return 0;
}