-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++.cpp
99 lines (80 loc) · 1.7 KB
/
C++.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
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int w, h;
struct Point
{
int x, y;
};
vector<Point> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char depthToChar(int depth)
{
if (depth < 10)
{
return '0' + depth;
}
return 'A' + depth - 10;
}
void bfs(int startX, int startY, char **MAP)
{
queue<Point> q;
queue<Point> tempQ;
q.push({startX, startY});
int depth = 0;
while (!q.empty())
{
Point currentPoint = q.front();
q.pop();
if (MAP[currentPoint.y][currentPoint.x] == '.')
{
MAP[currentPoint.y][currentPoint.x] = depthToChar(depth);
for (Point d : dir)
{
int x = currentPoint.x + d.x;
int y = currentPoint.y + d.y;
x = x < 0 ? w - 1 : (x >= w ? 0 : x);
y = y < 0 ? h - 1 : (y >= h ? 0 : y);
tempQ.push({x, y});
}
}
if (q.empty())
{
depth++;
swap(q, tempQ);
}
}
}
int main()
{
cin >> w >> h;
cin.ignore();
char **MAP = new char *[h];
Point start;
for (int i = 0; i < h; i++)
{
MAP[i] = new char[w];
string row;
getline(cin, row);
for (int j = 0; j < w; j++)
{
MAP[i][j] = row[j];
if (row[j] == 'S')
{
start = {j, i};
MAP[i][j] = '.';
}
}
}
bfs(start.x, start.y, MAP);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cout << MAP[i][j];
}
cout << '\n';
}
}