Pagini recente » Cod sursa (job #128692) | Cod sursa (job #1598010) | Cod sursa (job #419534) | Cod sursa (job #1110859) | Cod sursa (job #2988933)
#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;
}