728x90
처음엔 *, #에서 동시에 시작하는 그래프 문제인가? 싶었는데 그냥 구현문제였다.
각 자리의 좌표를 기억하고 갱신해주면 되는 간단한 문제이다.
뭔가 반복문을 잘 쓰면 코드를 줄일 수 있을 것 같은데 그냥 무식하게 했다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(vector<int> numbers, string hand) {
string answer = "";
int leftx = 3;
int lefty = 0;
int rightx = 3;
int righty = 2;
for(int i = 0; i < numbers.size(); i++){
if(numbers[i] == 1){
leftx = 0;
lefty = 0;
answer.push_back('L');
}
if(numbers[i] == 4){
leftx = 1;
lefty = 0;
answer.push_back('L');
}
if(numbers[i] == 7){
leftx = 2;
lefty = 0;
answer.push_back('L');
}
if(numbers[i] == 3){
rightx = 0;
righty = 2;
answer.push_back('R');
}
if(numbers[i] == 6){
rightx = 1;
righty = 2;
answer.push_back('R');
}
if(numbers[i] == 9){
rightx = 2;
righty = 2;
answer.push_back('R');
}
if(numbers[i] == 2){
int tmpright = rightx + abs(righty - 1);
int tmpleft = leftx + abs(lefty - 1);
if(tmpright > tmpleft){
leftx = 0;
lefty = 1;
answer.push_back('L');
}else if(tmpright < tmpleft){
rightx = 0;
righty = 1;
answer.push_back('R');
}
else{
if(hand == "right"){
rightx = 0;
righty = 1;
answer.push_back('R');
}
else{
leftx = 0;
lefty = 1;
answer.push_back('L');
}
}
}
if(numbers[i] == 5){
int tmpright = abs(rightx - 1) + abs(righty - 1);
int tmpleft = abs(leftx - 1) + abs(lefty - 1);
if(tmpright > tmpleft){
leftx = 1;
lefty = 1;
answer.push_back('L');
}else if(tmpright < tmpleft){
rightx = 1;
righty = 1;
answer.push_back('R');
}
else{
if(hand == "right"){
rightx = 1;
righty = 1;
answer.push_back('R');
}
else{
leftx = 1;
lefty = 1;
answer.push_back('L');
}
}
}
if(numbers[i] == 8){
int tmpright = abs(rightx - 2) + abs(righty - 1);
int tmpleft = abs(leftx - 2) + abs(lefty - 1);
if(tmpright > tmpleft){
leftx = 2;
lefty = 1;
answer.push_back('L');
}else if(tmpright < tmpleft){
rightx = 2;
righty = 1;
answer.push_back('R');
}
else{
if(hand == "right"){
rightx = 2;
righty = 1;
answer.push_back('R');
}
else{
leftx = 2;
lefty = 1;
answer.push_back('L');
}
}
}
if(numbers[i] == 0){
int tmpright = abs(rightx - 3) + abs(righty - 1);
int tmpleft = abs(leftx - 3) + abs(lefty - 1);
if(tmpright > tmpleft){
leftx = 3;
lefty = 1;
answer.push_back('L');
}
else if(tmpright < tmpleft){
rightx = 3;
righty = 1;
answer.push_back('R');
}
else{
if(hand == "right"){
rightx = 3;
righty = 1;
answer.push_back('R');
}
else{
leftx = 3;
lefty = 1;
answer.push_back('L');
}
}
}
}
return answer;
}
'프로그래머스 풀이 > Lv 1' 카테고리의 다른 글
프로그래머스 - 콜라 문제(C++) (0) | 2022.11.21 |
---|---|
프로그래머스 - 로또의 최고 순위와 최저 순위(C++) (0) | 2022.11.16 |
프로그래머스 - [1차] 다트 게임 (0) | 2022.11.15 |