-
Notifications
You must be signed in to change notification settings - Fork 13
/
annihilation.cpp
42 lines (34 loc) · 1.12 KB
/
annihilation.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
#include <bits/stdc++.h>
typedef std::string ARROW;
typedef std::pair<int, int> POS;
int main()
{
int h, w, turns{0};
std::map<POS, ARROW > state{};
std::cin >> h >> w; std::cin.ignore();
for ( auto i = 0; i < h; ++i ) {
std::string line;
getline(std::cin, line);
for ( auto j = 0; j < std::size(line); ++j )
if( line[j] != '.' ) state[std::pair{i, j}] = line[j];
}
while ( !std::empty(state) ) {
std::map< POS, ARROW > next;
for ( auto [pos, arrow] : state ) {
auto x{ pos.first }, y{ pos.second };
switch(arrow[0]) {
case '^': x = (x - 1 + h) % h; break;
case 'v': x = (x + 1 ) % h; break;
case '<': y = (y - 1 + w) % w; break;
case '>': y = (y + 1 ) % w; break;
}
next[{x, y}] += arrow;
}
for ( auto it = std::begin(next); std::end(next) != it; )
if ( std::size(it->second) > 1 ) it = next.erase(it);
else ++it;
state = next;
++turns;
}
std::cout << turns << '\n';
}