-
Notifications
You must be signed in to change notification settings - Fork 13
/
were-going-in-circles.cpp
59 lines (47 loc) · 1.62 KB
/
were-going-in-circles.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
#include <bits/stdc++.h>
#define EMPTY '.'
#define RIGHT '>'
#define LEFT '<'
#define UP '^'
#define DOWN 'v'
void nextPos(std::pair<int, int>& pos, const char& curDir, char& lastDir) {
switch( curDir ) {
case RIGHT: ++pos.second; break;
case LEFT : --pos.second; break;
case UP : --pos.first ; break;
case DOWN : ++pos.first ; break;
case EMPTY: nextPos(pos, lastDir, lastDir); break;
}
if ( EMPTY != curDir ) lastDir = curDir;
}
int main()
{
int w, h, ret{0};
std::cin >> w >> h; std::cin.ignore();
std::set< std::pair<int, int> > vis, curVis;
std::vector< std::string > grid(h);
for ( auto& line : grid )
std::cin >> line; std::cin.ignore();
for ( auto i = 0; i < h; ++i ) {
for ( auto j = 0; j < w; ++j ) {
std::pair<int, int> pos = {i, j};
const auto initPos = pos;
auto lastDir = grid[i][j];
if ( EMPTY == grid[i][j] || vis.count(pos) ) continue;
curVis.clear();
while ( pos.first >= 0 && pos.first < h &&
pos.second >= 0 && pos.second < w &&
!curVis.count(pos) ) {
const auto& curDir = grid[pos.first][pos.second];
if ( EMPTY != curDir ) { curVis.insert(pos); }
nextPos(pos, curDir, lastDir);
if ( pos == initPos ) {
++ret;
vis.insert(std::begin(curVis), std::end(curVis));
break;
}
}
}
}
std::cout << ret << "\n";
}