Cod sursa(job #2988933)

Utilizator MemeComan Mihai Matei Meme Data 5 martie 2023 16:36:44
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;
const int N = 252;
const int dl[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
char a[N][N];
int b[N][N];
struct c{ // coordonate
  int l, c;
};
int main()
{

    ifstream in("muzeu.in");
    ofstream out("muzeu.out");
    queue <c> q;
    int n;
    in >> n;
    for(int i = 1; i <= n; i++){
      for(int j = 1; j <= n; j++){
        in >> a[i][j];
        switch(a[i][j]){
          case '.':
            b[i][j] = -1;
            break;
          case '#':
            b[i][j] = -2;
          break;
          case 'P':
            b[i][j] = 0;
            q.push((c){i,j});
          break;
        }
      }
    }

    while(!q.empty()){
      c x = q.front();
      q.pop();
      for(int k = 0; k < 4; k++){
        c y;
        y.l = x.l + dl[k];
        y.c = x.c + dc[k];
        if(b[y.l][y.c] == -1){
          b[y.l][y.c] = b[x.l][x.c] + 1;
          q.push((c){y.l,y.c});
        }
      }
    }
    for(int i = 1; i <= n; i++){
      for(int j = 1; j <= n; j++){
        out << b[i][j] << " ";
      }
      out << '\n';
    }
    return 0;
}