본문 바로가기
코딩 테스트/프로그래머스(lv1)

[프로그래머스]키패드 누르기(c++)

by 계양구놈팽이 2022. 5. 16.

https://programmers.co.kr/learn/courses/30/lessons/67256

 

코딩테스트 연습 - 키패드 누르기

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

복잡한 알고리즘을 요구하는 문제는 아닙니다. 단, 처음에 문제를 주의 깊게 읽지 않으면 많은 시간을 낭비 할 수 있습니다.

#include <string>
#include <vector>
#include <iostream>
#include <unordered_map>;
#include <cmath>
using namespace std;

struct Pose
{
    int i;
    int j;

    Pose(int _i=0, int _j=0) :i(_i), j(_j) {};

    Pose& operator=(const Pose& ref)
    {
        i = ref.i;
        j = ref.j;
        return *this;
    }
};

unordered_map<char, Pose> number_pose =
{
    {'1', Pose(0,0)},
    {'2', Pose(0,1)},
    {'3', Pose(0,2)},
    {'4', Pose(1,0)},
    {'5', Pose(1,1)},
    {'6', Pose(1,2)},
    {'7', Pose(2,0)},
    {'8', Pose(2,1)},
    {'9', Pose(2,2)},
    {'*', Pose(3,0)},
    {'0', Pose(3,1)},
    {'#', Pose(3,2)}
};


vector<string> keypad = {"123","456","789","*0#"};

int compute_cost(Pose hand, Pose goal)
{
    int cost = abs(hand.i - goal.i) + abs(hand.j - goal.j);
    return cost;
}


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

    //init pose of both hand
    Pose right_hand = number_pose['#'];
    Pose left_hand  = number_pose['*'];

    for (auto& num : numbers)
    {
        if (num == 1 || num == 4 || num == 7)
        {
            answer.push_back('L');
            char key_char = num + '0';
            left_hand = number_pose[key_char];
        }
        else if (num == 3 || num == 6 || num == 9)
        {
            answer.push_back('R');
            char key_char = num + '0';
            right_hand = number_pose[key_char];
        }
        else
        {
            char key_char = num + '0';
            Pose goal_pose(number_pose[key_char]);
            if (compute_cost(right_hand, goal_pose) == compute_cost(left_hand, goal_pose))
            {
                if (hand == "right")
                {
                    answer.push_back('R');
                    right_hand = number_pose[key_char];
                }
                else
                {
                    answer.push_back('L');
                    left_hand = number_pose[key_char];
                }
            }
            else if (compute_cost(right_hand, goal_pose) > compute_cost(left_hand, goal_pose))
            {
                answer.push_back('L');
                left_hand = number_pose[key_char];
            }
            else
            {
                answer.push_back('R');
                right_hand = number_pose[key_char];
            }
        }
    }
    

    return answer;
}


int main()
{
    vector<int> numbers = { 1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5 };
    string hand = "right";
    cout << solution(numbers, hand);
}