-
Notifications
You must be signed in to change notification settings - Fork 4
/
muzeu.cpp
111 lines (95 loc) · 1.98 KB
/
muzeu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <fstream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
ifstream in ("muzeu.in");
ofstream out ("muzeu.out");
int n;
struct dot
{
int x;
int y;
};
dot make_dot(int x,int y)
{
dot newDot;
newDot.x = x;
newDot.y = y;
return newDot;
}
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int main()
{
in >> n;
int** ar = new int*[n+2];
for( int i = 0; i < n+2; i++ )
{
ar[i] = new int[n+2];
memset(ar[i],0,(n+2)*sizeof(int));
}
for( int i = 0; i < n+2; i++ )
{
ar[i][0]=ar[0][i]=ar[n+1][i]=ar[i][n+1]=-2;
}
vector<dot> paz;
for( int i = 1; i <= n; i++ )
{
in.ignore();
for( int j = 1; j <= n; j++ )
{
switch(in.get())
{
case '#':ar[i][j]=-2;break;
case 'P':paz.push_back(make_dot(i,j));break;
}
}
}
queue<dot> q;
int nrPaz = paz.size();
for( int i = 0; i < nrPaz; i++ )
{
q.push(paz[i]);
int x = paz[i].x;
int y = paz[i].y;
ar[x][y] = -3;
}
for( ;!q.empty();q.pop() )
{
dot curDot = q.front();
int x;
int y;
for( int k = 0; k < 4; k++ )
{
x = curDot.x+dx[k];
y = curDot.y+dy[k];
if( ar[x][y]==0 )
{
q.push(make_dot(x,y));
if(ar[curDot.x][curDot.y]==-3)
ar[x][y] = 1;
else
ar[x][y] = ar[curDot.x][curDot.y] + 1;
}
}
}
for( int i = 1; i <=n; i++ )
{
for( int j = 1; j<=n;j++ )
{
if(ar[i][j] == -3)
{
out << 0 << ' ';
continue;
}
if( ar[i][j] == 0 )
{
out << -1<< ' ';
continue;
}
out <<ar[i][j]<< ' ';
}
out << '\n';
}
}