-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathisland-escape.cpp
More file actions
76 lines (58 loc) · 1.83 KB
/
island-escape.cpp
File metadata and controls
76 lines (58 loc) · 1.83 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
#include <bits/stdc++.h>
struct Cell {
int val{ 0 };
bool visited{ false };
};
struct Point {
int x, y;
constexpr Point(int x_ = 0, int y_ = 0) noexcept : x{x_}, y{y_} {}
};
Point operator+(const Point& a, const Point& b) {
return {a.x + b.x, a.y + b.y};
}
class Grid {
private:
std::vector< std::vector<Cell> > _cells;
const int _size;
public:
Grid(int n) noexcept :
_size{ n }, _cells{ static_cast<size_t>(n), std::vector<Cell>(n) } {}
Cell& at(const Point& p) noexcept {
return _cells.at(p.x).at(p.y);
}
std::vector<Cell>& operator[](int i) noexcept {
return _cells[i];
}
auto begin() noexcept { return _cells.begin(); }
auto end() noexcept { return _cells.end(); }
};
int main() {
constexpr Point dirs[]{ {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
int n;
std::cin >> n; std::cin.ignore();
Grid grid{n};
for ( auto& raw : grid )
for ( auto& cell : raw )
std::cin >> cell.val; std::cin.ignore();
Point curPos { n/2, n/2 };
Cell* curCell{ nullptr };
std::deque<Point> stack;
stack.push_back(curPos);
while (!stack.empty()) {
curPos = stack.front();
stack.pop_front();
curCell = &grid.at(curPos);
curCell->visited = true;
if ( 0 == curCell->val )
break;
for ( const auto& dir : dirs ) {
Point nxtPos{ curPos + dir };
if ( nxtPos.x < 0 || nxtPos.x >= n ||
nxtPos.y < 0 || nxtPos.y >= n ) continue;
Cell* nxtCell{ &grid.at(nxtPos) };
if ( nxtCell->visited || (1 < abs(curCell->val - nxtCell->val)) ) continue;
stack.push_back(nxtPos);
}
}
std::cout << (( 0 == curCell->val ) ? "yes" : "no" ) << std::endl;
}