-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path220114_BaekJoon_14940.cpp
More file actions
85 lines (69 loc) · 1.43 KB
/
220114_BaekJoon_14940.cpp
File metadata and controls
85 lines (69 loc) · 1.43 KB
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
#include <iostream>
#include <queue>
#define MAX_SIZE 1000 + 1
#define p pair<int, int>
using namespace std;
int rowSize, colSize, targetRow, targetCol;
int graph[MAX_SIZE][MAX_SIZE];
queue<p> que;
void bfs();
void dp();
int main() {
cin >> rowSize >> colSize;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
cin >> graph[i][j];
if (graph[i][j] == 2) {
targetRow = i;
targetCol = j;
graph[i][j] = 0;
}
else if (graph[i][j] == 1) {
graph[i][j] = 0;
}
else {
graph[i][j] = -1;
}
}
}
que.push({ targetRow, targetCol });
graph[targetRow][targetCol] = 1;
bfs();
dp();
return 0;
}
void bfs() {
while (!que.empty()) {
int curRow = que.front().first;
int curCol = que.front().second;
que.pop();
int dY[4] = { -1, 0, 0, 1 };
int dX[4] = { 0, -1, 1, 0 };
for (int i = 0; i < 4; i++) {
int nextRow = curRow + dY[i];
int nextCol = curCol + dX[i];
if (nextRow >= 0 && nextRow < rowSize && nextCol >= 0 && nextCol < colSize) {
if (graph[nextRow][nextCol] == 0) {
graph[nextRow][nextCol] = graph[curRow][curCol] + 1;
que.push({ nextRow, nextCol });
}
}
}
}
}
void dp() {
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
if (graph[i][j] == -1) {
cout << 0 << " ";
}
else if (graph[i][j] == 0) {
cout << -1 << " ";
}
else {
cout << graph[i][j] - 1 << " ";
}
}
cout << "\n";
}
}