Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

charminseok

[2018 윈터코딩] 방문길이 본문

알고리즘/알고리즘 문제

[2018 윈터코딩] 방문길이

charminseok 2019. 9. 10. 13:59

https://www.welcomekakao.com/learn/courses/30/lessons/49994

 

코딩테스트 연습 - 방문 길이 | 프로그래머스

 

www.welcomekakao.com

오버라이딩을 통해 생각보다 간단히 풀었다

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
#include <string>
#include <vector>
#include <algorithm>
#include<iostream>
#include<map>
using namespace std;
pair<intint> operator+(pair<intint> &a, pair<intint> &b) {
    int tmp = a.first + b.first;
    int tmp1 = a.second+ b.second;
    return { tmp, tmp1 };
}
bool operator==(pair<pair<intint>pair<intint>>&a, pair<pair<intint>pair<intint>> &b) {
        return true;
    }
    else return false;
}
vector<pair<pair<intint>pair<intint>>> path;
bool same(pair<intint> be, pair<intint> now) {
    pair<pair<intint>pair<intint>> pathnow;
    pathnow = { be,now };
    for (int i = 0; i < path.size(); i++) {
        if (pathnow == path[i]) {
            return true;
        }
    }
     return false;
}
int solution(string dirs)
{
    int answer = 7;
    int cnt = 0;
    map<charpair<int,int>> di;
    
    di.insert('U',{ 1,0 } });
    di.insert('D',{ -1,0 } });
    di.insert('R',{ 0,1 } });
    di.insert('L',{ 0,-1 } });
    pair<intint> before = { 5,5 };
    pair<intint> now;
    for (int i = 0; i < dirs.size(); i++) {
        char c = dirs[i];
        now = before + di[c];
 
        if (now.first >= 0 && now.first < 11 && now.second >= 0 && now.second < 11 && same(before, now) == false) {
            cnt++;
            path.push_back({ before,now });
            path.push_back({ now,before });
            before = now;
        }
        else if(now.first >= 0 && now.first < 11 && now.second >= 0 && now.second < 11 && same(before, now) == true)
            before = now;
 
    }
    return cnt;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter