Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 에라토스테네스의 체
- 유니온 파인드
- CS
- mst
- 수학
- 스택
- 백준
- Effective Java
- 프로그래머스
- dfs
- 위상정렬
- 문자열
- Network
- 그리디
- 동적계획법
- 백트래킹
- java
- BFS
- JUnit 5
- swea
- 구현
- 후니의 쉽게 쓴 시스코 네트워킹
- 알고리즘
- 완전탐색
- 플로이드-와샬
- 세그먼트 트리
- Kotlin
- 이분탐색
- 투 포인터
- 시뮬레이션
Archives
반갑습니다!
[프로그래머스] 키패드 누르기 본문
풀이
핸드폰 키패드의 좌표를 사전에 미리 저장해두고 왼손과 오른손의 위치를 계속해서 갱신해주는 방식으로 해결하였다.
코드
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct pos {
int x, y;
};
map<int, pos> l_hand = { {1, {0, 0}}, {4, {0, 1}} , {7, {0, 2}} };
map<int, pos> r_hand = { {3, {2, 0}}, {6, {2, 1}} , {9, {2, 2}} };
map<int, pos> mid_hand = { {2, {1, 0}}, {5, {1, 1}}, {8, {1, 2}}, {0, {1, 3}} };
pos l_pos = { 0, 3 }, r_pos = { 2, 3 };
int get_distance(pos p1, pos p2) {
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}
string solution(vector<int> numbers, string hand) {
string answer = "";
for (int i : numbers) {
if (l_hand.find(i) != l_hand.end()) {
answer += "L";
l_pos.x = l_hand[i].x;
l_pos.y = l_hand[i].y;
}
else if (r_hand.find(i) != r_hand.end()) {
answer += "R";
r_pos.x = r_hand[i].x;
r_pos.y = r_hand[i].y;
}
else {
if (get_distance(l_pos, mid_hand[i]) > get_distance(r_pos, mid_hand[i])) {
answer += 'R';
r_pos.x = mid_hand[i].x;
r_pos.y = mid_hand[i].y;
}
else if (get_distance(l_pos, mid_hand[i]) < get_distance(r_pos, mid_hand[i])) {
answer += 'L';
l_pos.x = mid_hand[i].x;
l_pos.y = mid_hand[i].y;
}
else {
if (hand == "right") {
answer += 'R';
r_pos.x = mid_hand[i].x;
r_pos.y = mid_hand[i].y;
}
else {
answer += 'L';
l_pos.x = mid_hand[i].x;
l_pos.y = mid_hand[i].y;
}
}
}
}
return answer;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[프로그래머스] 보석 쇼핑 (0) | 2020.07.06 |
---|---|
[프로그래머스] 수식 최대화 (0) | 2020.07.06 |
[백준] 10164 격자상의 경로 (0) | 2020.06.26 |
[백준] 9507 Generations of Tribbles (0) | 2020.06.21 |
[백준] 2630 색종이 만들기 (0) | 2020.06.08 |