프로그래머스 - 키패드누르기 Cpp

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/77484

#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

bool isLeft(int number) {
if (number == 1 || number == 4 || number == 7) {
return true;
}
return false;
}

bool isRight(int number) {
if (number == 3 || number == 6 || number == 9) {
return true;
}
return false;
}

bool isMiddle(int number) {
if (number == 2 || number == 5 || number == 8 || number == 0) {
return true;
}
return false;
}

string solution(vector<int> numbers, string hand) {
string answer = "";
map<int, pair<int, int>> keypad;

keypad[1] = {0, 0}, keypad[2] = {0, 1}, keypad[3] = {0, 2};
keypad[4] = {1, 0}, keypad[5] = {1, 1}, keypad[6] = {1, 2};
keypad[7] = {2, 0}, keypad[8] = {2, 1}, keypad[9] = {2, 2};
keypad[0] = {3, 1};

pair<int, int> left_point = {3, 0};
pair<int, int> right_point = {3, 2};

cout << keypad[2].first << " " << keypad[2].second << endl;

for (int number : numbers) {
if (isLeft(number)) {
left_point = keypad[number];
answer.append("L");
}

if (isRight(number)) {
right_point = keypad[number];
answer.append("R");
}

if (isMiddle(number)) {
pair<int, int> key_point = keypad[number];
int left_dist = abs(key_point.first - left_point.first) + abs(key_point.second - left_point.second);
int right_dist = abs(key_point.first - right_point.first) + abs(key_point.second - right_point.second);

if (left_dist == right_dist) {
if (hand == "right") {
right_point = keypad[number];
answer.append("R");
} else {
left_point = keypad[number];
answer.append("L");
}
} else if (left_dist < right_dist) {
left_point = keypad[number];
answer.append("L");
} else {
right_point = keypad[number];
answer.append("R");
}
}
}

return answer;
}
Share