-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.cpp
More file actions
85 lines (69 loc) · 1.65 KB
/
maze.cpp
File metadata and controls
85 lines (69 loc) · 1.65 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 <string>
#include <vector>
using namespace std;
vector<pair<int, int>> removables;
bool maze[505][505];
bool visited[505][505];
int n, m;
void dfs (int x, int y) {
if (maze[x][y]) {
visited[x][y] = true;
if (x+1 < n && !visited[x+1][y]) {
dfs(x+1, y);
}
if (x - 1 >= 0 && !visited[x-1][y]) {
dfs(x-1, y);
}
if (y+1 < m && !visited[x][y+1]) {
dfs(x, y+1);
}
if (y - 1 >= 0 && !visited[x][y-1]) {
dfs(x, y-1);
}
removables.push_back(make_pair(x, y));
}
}
int main ()
{
int k;
cin >> n >> m >> k;
string final[n][m];
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
visited[i][j] = false;
}
}
int firI = -1;
int firJ = -1;
bool found = false;
for (int i = 0; i < n; i++) {
string curr;
cin >> curr;
vector<char> v(curr.begin(), curr.end());
for (int j = 0; j < m; j++) {
if (v[j] == '.') {
maze[i][j] = true;
if (!found) {
firI = i;
firJ = j;
found = true;
}
}
else maze[i][j] = false;
final[i][j] = string(1, v[j]);
}
};
dfs(firI, firJ);
for (int i = 0; i < k; i++) {
int x = removables[i].first;
int y = removables[i].second;
final[x][y] = "X";
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << final[i][j];
}
cout << "\n";
}
}