관리 메뉴

有希

프로그래머스/키패드 누르기 본문

프로그래밍/알고리즘+코딩테스트

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

有希. 2022. 3. 16. 04:38

맨하탄 거리니, 피타고라스 거리니 어렵게 수학적 용어 가져올 필요 없이 좌표축에 대입시켜서 x,y 좌표 합 구하면 그게 거리다. 물론 이게 맨하탄 거리이긴 하지만.

그리고 Pos 배열을 하드코딩 하는게 싫다면 0은 특수 경우이니 직접 넣어주고 나머지는 for문으로 채워넣으면 된다.

프로그래머스는 테스트 케이스를 보여주지 않는다는 점이 진짜 짜증난다. 영업비밀인건가? LeetCode는 다 보여주는데.

int garo_max = 3;
int sero_max = 3;
vector<vector<int>> Pos;
for(int i=sero_max; i>=0; --i)
{
	for(int j=0; j<garo_max; ++j)
    {
 		vector<int> temp = {j, i}; 
    }
}
#include <string>
#include <vector>
#include <cmath>

using namespace std;

string solution(vector<int> numbers, string hand) {
    string answer = "";
    int CurLeft[2] = {0,0};
    int CurRight[2] = {2,0};
    int DistLeft;
    int DistRight;
    vector<vector<int>>Pos = {
        {1,0},
        {0,3}, {1,3}, {2,3},
        {0,2}, {1,2}, {2,2},
        {0,1}, {1,1}, {2,1},
    };
    int target = -1;
    for(int i=0; i<numbers.size(); ++i)
    {
        target = numbers[i];
        DistLeft = abs(Pos[target][0] - CurLeft[0]) + abs(Pos[target][1] - CurLeft[1]);
        DistRight = abs(Pos[target][0] - CurRight[0]) + abs(Pos[target][1] - CurRight[1]);
        //왼쪽 손가락만 쓰는 경우
        if(target == 1 || target == 4 || target == 7)
        {
            answer += "L";
            CurLeft[0] = Pos[target][0];
            CurLeft[1] = Pos[target][1];
        }
        //오른쪽 손가락만 쓰는 경우
        else if(target == 3 || target == 6 || target ==9)
        {
            answer += "R";
            CurRight[0] = Pos[target][0];
            CurRight[1] = Pos[target][1];
        }
        //거리 짧은쪽이 가야 하는 경우
        else
        {
            if(DistLeft < DistRight)
            {
                answer += "L";
                CurLeft[0] = Pos[target][0];
                CurLeft[1] = Pos[target][1];
            }
            else if(DistLeft > DistRight)
            {
                answer += "R";
                CurRight[0] = Pos[target][0];
                CurRight[1] = Pos[target][1];
            }
            else
            {
                if(hand=="left")
                {
                    answer += "L";
                    CurLeft[0] = Pos[target][0];
                    CurLeft[1] = Pos[target][1];
                }
                else
                {
                    answer += "R";
                    CurRight[0] = Pos[target][0];
                    CurRight[1] = Pos[target][1];
                }
            }
        }
    }
    return answer;
}