-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.cpp
More file actions
70 lines (58 loc) · 1.58 KB
/
box.cpp
File metadata and controls
70 lines (58 loc) · 1.58 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
#include <iostream>
using namespace std;
int n, m;
char **maze;
bool **seen;
void displayMaze() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maze[i][j];
}
cout << "\n";
}
}
void init() {
cin >> n >> m;
maze = new char*[n];
for (int i = 0; i < n; ++i) {
maze[i] = new char[m];
}
seen = new bool*[n];
for (int i = 0; i < n; ++i) {
seen[i] = new bool[m];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> maze[i][j];
seen[i][j] = (maze[i][j] == '.') ? false : true;
}
}
}
bool dfs(int x, int y) {
if (x < 0 || x + 1 >= m || y < 0 || y + 1 >= n) return false;
if (seen[y][x] && seen[y][x + 1] && seen[y + 1][x] && seen[y + 1][x + 1]) return false;
if (maze[y][x] != '.' || maze[y + 1][x] != '.' || maze[y][x + 1] != '.' || maze[y + 1][x + 1] != '.') return false;
if (y + 1 == n - 1 && maze[y][x] == '.' && maze[y + 1][x] == '.' && maze[y][x + 1] == '.' && maze[y + 1][x + 1] == '.') return true;
seen[y][x] = true;
seen[y][x + 1] = true;
seen[y + 1][x] = true;
seen[y + 1][x + 1] = true;
if (dfs(x + 1, y)) return true;
if (dfs(x, y + 1)) return true;
if (dfs(x - 1, y)) return true;
if (dfs(x, y - 1)) return true;
return false;
}
int main() {
init();
bool canReachEnd = false;
for(int i = 0; i < m; i++){
canReachEnd |= dfs(i, 0);
if(canReachEnd){
cout << "yes";
break;
}
}
if(!canReachEnd) cout << "no";
return 0;
}