charminseok
[2018 윈터코딩] 방문길이 본문
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<int, int> operator+(pair<int, int> &a, pair<int, int> &b) {
int tmp = a.first + b.first;
int tmp1 = a.second+ b.second;
return { tmp, tmp1 };
}
bool operator==(pair<pair<int, int>, pair<int, int>>&a, pair<pair<int, int>, pair<int, int>> &b) {
if (a.first.first == b.first.first && a.first.second == b.first.second && a.second.first == b.second.first && a.second.second == b.second.second) {
return true;
}
else return false;
}
vector<pair<pair<int, int>, pair<int, int>>> path;
bool same(pair<int, int> be, pair<int, int> now) {
pair<pair<int, int>, pair<int, int>> 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<char, pair<int,int>> di;
pair<int, int> before = { 5,5 };
pair<int, int> 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
|
'알고리즘 > 알고리즘 문제' 카테고리의 다른 글
[삼성 swea] 1949. 등산로 조성 (0) | 2019.09.11 |
---|---|
[카카오 코딩 테스트] 후보키 (0) | 2019.09.10 |
[2018 윈터코딩] 쿠키 구입 (0) | 2019.09.10 |
[카카오 코딩테스트] 실패율 (0) | 2019.09.10 |
[카카오 코딩테스트] 오픈채팅방 (0) | 2019.09.10 |