https://programmers.co.kr/learn/courses/30/lessons/49994
코딩테스트 연습 - 방문 길이
programmers.co.kr
#include <string>
#include <set>
#include <unordered_map>
#include <cmath>
#include <iostream>
using namespace std;
typedef struct Load {
pair<int, int> start;
pair<int, int> end;
bool operator < (const Load& other)const {
if (start == other.start) {
return end < other.end;
}
return start < other.start;
}
}Load;
int solution(string dirs) {
int answer = 0;
set<Load> visited;
unordered_map<char, pair<int, int>> m = { {'U',{1,0}},{'D',{-1,0}},{'R',{0,1}},{'L',{0,-1}} };
pair<int, int> now = { 0,0 };
for (char s : dirs) {
cout << s << " | ";
int h, w;
h = now.first + m.find(s)->second.first;
w = now.second + m.find(s)->second.second;
if (abs(h) < 6 && abs(w) < 6) {
cout << " in {" << h << "," << w << "}";
cout << min(now, { h,w }).first << ", " << min(now, { h,w }).second;
cout << " | " << max(now, { h,w }).first << ", " << max(now, { h,w }).second << endl;
if (visited.find({ now,{h,w} }) == visited.end() && visited.find({ {h,w}, now }) == visited.end()) {
cout << " no find : ";
answer++;
visited.insert({ now,{h,w} });
visited.insert({ {h,w}, now });
}
now = { h,w };
}
}
return answer;
}
'전공공부 > 코딩테스트' 카테고리의 다른 글
(c++) 프로그래머스 "땅따먹기" (0) | 2022.04.16 |
---|---|
(c++) 프로그래머스 "올바른 괄호" (0) | 2022.04.15 |
(c++) 프로그래머스 "2개 이하로 다른 비트" (0) | 2022.04.14 |
(c++) 프로그래머스 "이진 변환 반복하기" (0) | 2022.04.13 |
(c++) 프로그래머스 "괄호 회전하기" (0) | 2022.04.13 |