๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
using namespace std;
string solution(vector<int> numbers, string hand) {
string answer = "";
int x_L = 3, y_L = 0;
int x_R = 3, y_R = 2;
for(int number:numbers){
if(number==1||number==4||number==7){
answer+='L';
if(number==1) x_L = 0, y_L = 0;
else if(number==4) x_L = 1, y_L=0;
else if(number==7) x_L = 2, y_L = 0;
}
else if(number==3||number==6||number==9){
answer+='R';
if(number==3) x_R = 0, y_R = 2;
else if(number==6) x_R = 1, y_R = 2;
else if(number==9) x_R = 2, y_R = 2;
}
else{ //2, 5, 8, 0 ์ผ๋
if(number==2){
int dist_L = abs(x_L-0) + abs(y_L-1);
int dist_R = abs(x_R-0) + abs(y_R-1);
if(dist_L<dist_R){ //์ผ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_L = 0, y_L = 1;
answer+='L';
}
else if(dist_L>dist_R){ //์ค๋ฅธ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_R = 0, y_R = 1;
answer+='R';
}
else{ //๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋
if(hand=="right"){ //์ค๋ฅธ์ ์ก์ด์ผ๋
x_R = 0, y_R = 1;
answer+='R';
}
else{ //์ผ์ ์ก์ด์ผ ๋
x_L = 0, y_L = 1;
answer+='L';
}
}
}
else if(number==5){
int dist_L = abs(x_L-1) + abs(y_L-1);
int dist_R = abs(x_R-1) + abs(y_R-1);
if(dist_L<dist_R){ //์ผ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_L = 1, y_L = 1;
answer+='L';
}
else if(dist_L>dist_R){ //์ค๋ฅธ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_R = 1, y_R = 1;
answer+='R';
}
else{ //๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋
if(hand=="right"){ //์ค๋ฅธ์ ์ก์ด์ผ๋
x_R = 1, y_R = 1;
answer+='R';
}
else{ //์ผ์ ์ก์ด์ผ ๋
x_L = 1, y_L = 1;
answer+='L';
}
}
}
else if(number==8){
int dist_L = abs(x_L-2) + abs(y_L-1);
int dist_R = abs(x_R-2) + abs(y_R-1);
if(dist_L<dist_R){ //์ผ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_L = 2, y_L = 1;
answer+='L';
}
else if(dist_L>dist_R){ //์ค๋ฅธ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_R = 2, y_R = 1;
answer+='R';
}
else{ //๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋
if(hand=="right"){ //์ค๋ฅธ์ ์ก์ด์ผ๋
x_R = 2, y_R = 1;
answer+='R';
}
else{ //์ผ์ ์ก์ด์ผ ๋
x_L = 2, y_L = 1;
answer+='L';
}
}
}
else if(number==0){
int dist_L = abs(x_L-3) + abs(y_L-1);
int dist_R = abs(x_R-3) + abs(y_R-1);
if(dist_L<dist_R){ //์ผ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_L = 3, y_L = 1;
answer+='L';
}
else if(dist_L>dist_R){ //์ค๋ฅธ์ชฝ ์์ง์์ ๊ฑฐ๋ฆฌ๊ฐ ๋ ์งง์ ๋
x_R = 3, y_R = 1;
answer+='R';
}
else{ //๊ฑฐ๋ฆฌ๊ฐ ๊ฐ์ ๋
if(hand=="right"){ //์ค๋ฅธ์ ์ก์ด์ผ๋
x_R = 3, y_R = 1;
answer+='R';
}
else{ //์ผ์ ์ก์ด์ผ ๋
x_L = 3, y_L = 1;
answer+='L';
}
}
}
}
}
return answer;
}
ํ์ด(17๋ถ)
์ด๋ฐ ๋ฌธ์ ๋ ํด๋ฆฐ ํ๊ฒ ์ง๋ ค๊ณ ๊ณ ๋ฏผํ๋ ๊ฒ๋ณด๋ค ๋จธ๋ฆฟ์์์ ์๊ฐ๋๋ ๋ฐ๋ก ์ต๋ํ ๋นจ๋ฆฌ ์๊ฐ์ ์๊ปด์ ํธ๋ ๊ฒ ์ค์ํ ๊ฒ ๊ฐ์ต๋๋ค.
ํด๋ํฐ ํคํจ๋๋ฅผ [4][4] ํฌ๊ธฐ์ ๋ฐฐ์ด์ด๋ผ๊ณ ์๊ฐํ๊ณ ์ข์ธก๊ณผ ์ฐ์ธก์ ์์ง ์๊ฐ๋ฝ ์์น๋ฅผ ๊ฐฑ์ ํด ์ฃผ๋ฉด์ ๊ฐ์ด๋ฐ ์ซ์๋ค 2, 5, 8, 0์ผ ๋ ์ข์ธก๊ณผ ์ฐ์ธก ์์ง์๊ฐ๋ฝ ์ค ์ด๋ ๊ณณ์ ๊ฑฐ๋ฆฌ๊ฐ ์งง์์ง ์ฐพ์์ ๊ตฌํํด์ฃผ์์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - 3์ง๋ฒ ๋ค์ง๊ธฐ(Level 1) (0) | 2022.04.13 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ๋ง๋ค๊ธฐ(Level 1) (0) | 2022.04.11 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์(Level 1) (0) | 2022.04.10 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ(Level 1) (0) | 2022.04.08 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ํ๋ ฌ ํ ๋๋ฆฌ ํ์ ํ๊ธฐ(Level 2) (0) | 2022.04.08 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - [1์ฐจ] ์บ์(Level 2) (0) | 2022.04.07 |
๋๊ธ