-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06a-guard-gallivant.cpp
More file actions
45 lines (42 loc) · 877 Bytes
/
06a-guard-gallivant.cpp
File metadata and controls
45 lines (42 loc) · 877 Bytes
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef _DEBUG
#include "utils/debug.h"
#else
#define debug(...) 0
#endif
int main() {
cin.tie(0)->sync_with_stdio(0);
string line;
ll ans = 0;
vector<string> g;
while (getline(cin, line)) {
g.push_back(line);
}
int N = g.size(), M = g[0].size();
auto in = [&](int i, int j) { return i >= 0 && j >= 0 && i < N && j < M; };
int x, y;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (g[i][j] == '^') x = i, y = j;
}
}
int dx = -1, dy = 0;
vector<vector<int>> vis(N, vector<int>(M));
while (true) {
vis[x][y] = 1;
int nx = x + dx, ny = y + dy;
if (!in(nx, ny)) break;
if (g[nx][ny] == '#') {
swap(dx, dy); dy *= -1;
}
else {
x = nx, y = ny;
}
}
for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) {
ans += vis[i][j];
}
cout << ans << '\n';
}