98 lines
2.1 KiB
C++
98 lines
2.1 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <set>
|
|
#include <vector>
|
|
#include <utility>
|
|
|
|
using namespace std;
|
|
|
|
void move_rope(pair<int, int>* head, pair<int, int>* tail);
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
vector<pair<int,int>> rope;
|
|
for(int i = 0; i < 10; i++)
|
|
{
|
|
rope.push_back({0,0});
|
|
}
|
|
|
|
set<pair<int, int>> visited;
|
|
|
|
string line;
|
|
while(getline(cin, line))
|
|
{
|
|
char dir = line[0];
|
|
int amount = stoi(line.substr(2));
|
|
int dx = (dir=='R')?1:((dir=='L')?-1:0);
|
|
int dy = (dir=='U')?1:((dir=='D')?-1:0);
|
|
|
|
for(int i = 0; i < amount; i++)
|
|
{
|
|
rope[0].first += dx;
|
|
rope[0].second += dy;
|
|
|
|
vector<pair<int,int>>::iterator knot1 = rope.begin();
|
|
vector<pair<int,int>>::iterator knot2 = rope.begin()+1;
|
|
for(int j = 0; j < rope.size(); j++)
|
|
{
|
|
move_rope(&(*knot1), &(*knot2));
|
|
++knot1;
|
|
++knot2;
|
|
}
|
|
visited.insert(rope.back());
|
|
}
|
|
}
|
|
|
|
cout << visited.size() << endl;
|
|
}
|
|
|
|
void move_rope(pair<int, int>* head, pair<int, int>* tail)
|
|
{
|
|
if (head->first == tail->first)
|
|
{ //same x
|
|
if (head->second > tail->second)
|
|
{
|
|
tail->second = head->second-1;
|
|
}
|
|
else if (head->second < tail->second)
|
|
{
|
|
tail->second = head->second+1;
|
|
}
|
|
}
|
|
else if (head->second == tail->second)
|
|
{ //same y
|
|
if (head->first > tail->first)
|
|
{
|
|
tail->first = head->first-1;
|
|
}
|
|
else if (head->first < tail->first)
|
|
{
|
|
tail->first = head->first+1;
|
|
}
|
|
}
|
|
else
|
|
{ //diagonal
|
|
if (abs(head->first-tail->first) == 1 && abs(head->second-tail->second) == 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (head->first-tail->first > 0)
|
|
{
|
|
tail->first++;
|
|
}
|
|
else
|
|
{
|
|
tail->first--;
|
|
}
|
|
|
|
if (head->second-tail->second > 0)
|
|
{
|
|
tail->second++;
|
|
}
|
|
else
|
|
{
|
|
tail->second--;
|
|
}
|
|
}
|
|
} |