-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1162.cpp
78 lines (76 loc) · 1.72 KB
/
P1162.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
#include <bits/stdc++.h>
using namespace std;
int n;
struct point
{
int x, y;
};
int i_change[] = {1, 0, -1, 0};
int j_change[] = {0, -1, 0, 1};
bool change[4];
queue<point> q;
char my[31][31];
bool possible[31][31];
void bfs()
{
struct point temp;
while (!q.empty())
{
temp = q.front();
q.pop();
if (my[temp.x][temp.y] == '1')
{
return;
}
for (int i = 0; i < 4; i++)
{
if (!possible[temp.x + i_change[i]][temp.y + j_change[i]] && temp.x + i_change[i] > 0 && temp.x + i_change[i] <= n && temp.y + j_change[i] > 0 && temp.y + j_change[i] <= n && my[temp.x + i_change[i]][temp.y + j_change[i]] == '0')
{
q.push((point){temp.x + i_change[i], temp.y + j_change[i]});
possible[temp.x + i_change[i]][temp.y + j_change[i]] = true;
}
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> my[i][j];
}
}
int loop[] = {1, n};
for (int i = 0; i < 2; i++)
{
for (int j = 1; j <= n; j++)
{
if (!possible[loop[i]][j])
{
q.push((point){loop[i], j});
bfs();
}
if (!possible[j][loop[i]])
{
q.push((point){j, loop[i]});
bfs();
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (!possible[i][j] && my[i][j] != '1')
{
cout << 2 << ' ';
}
else
cout << my[i][j] << ' ';
}
cout << endl;
}
return 0;
}