#include <fstream>
#include <climits>
#include <iomanip>
#include <queue>
using namespace std;
ifstream fin("muzeu.in");
ofstream fout("muzeu.out");
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
char a[250][250];
int c[250][250];
int n;
queue< pair <int, int> > Q;
int Ok (int I, int J);
int main()
{
fin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
fin >> a[j];
if (a[j] == 'P')
{
c[j] = 1;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (c[j] == 0)
c[j] = INT_MAX;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (c[j] == 1)
{
c[j] = 0;
Q.push({i, j});
}
int i, j, iv, jv;
while(!Q.empty())
{
i = Q.front().first;
j = Q.front().second;
Q.pop();
for (int d = 1; d <= 4; d++)
{
iv = i + di[d];
jv = j + dj[d];
if (Ok(iv, jv) && c[iv][jv] > c[j] + 1)
{
c[iv][jv] = c[j] + 1;
Q.push({iv, jv});
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (c[j] == INT_MAX)
c[j] = -1;
for (int i = 1; i <= n; ++i, fout << endl)
for (int j = 1; j <= n; ++j)
fout << c[j] << ' ';
}
int Ok(int I, int J)
{
if (I > n || J > n || I < 1 || J < 1)
return false;
if (a[J] == '#')
return false;
return true;
}
ce nu e bine?
