Post

[Summer/Winter Coding(~2018)] 방문 길이 (C++)

https://school.programmers.co.kr/learn/courses/30/lessons/49994

풀이

현재 간선을 방문했는지에 대한 set을 만들어준 후, 해당 (set의 크기 / 2)를 반환하는 방식을 이용했다. set에 현재 정점->다음 정점, 다음 정점->현재 정점, 양방향 모두 고려해서 넣었기 때문에 나누기 2를 해주었다. 간선의 방문 여부를 담는 visited 변수는 이전에 풀었던 방의 개수 문제를 참고했다.

푸는 도중에 if문을 쓸까 switch문을 쓸까 고민을 조금 하긴 했는데…… char를 비교하는 구문이라 if문보다는 switch가 좀 더 가독성이 좋을 것같아서 switch문을 이용했다.

코드

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
#include <string>
#include <set>

using namespace std;

int solution(string dirs) {
    pair<int, int> cur(0, 0);
    set<pair<pair<int, int>, pair<int, int>>> visited;
    for(auto d : dirs){
        pair<int, int> next = cur;
        switch(d){
            case 'U':
                next.second++;
                break;
            case 'D':
                next.second--;
                break;
            case 'L':
                next.first--;
                break;
            case 'R':
                next.first++;
                break;
        }
        if(next.first > 5 || next.first < -5 || next.second > 5 || next.second < -5)    continue;
        visited.insert(make_pair(cur, next));
        visited.insert(make_pair(next, cur));
        cur = next;
    }
    return visited.size() / 2;
}
This post is licensed under CC BY 4.0 by the author.