2D 평면 a와 b에 좌표가 (x1, y1) 및 (x2, y2)인 두 점이 있다고 가정합니다. 현재 우리는 '점'에 있으며 수직 또는 수평으로 1의 거리에서 이동할 수 있습니다. 우리는 지점에서 지점 b로 이동한 다음 지점으로 돌아가서 다시 지점 b로 이동합니다. 점 및 b를 제외하고 동일한 점을 두 번 이상 이동할 수 없습니다. 우리는 이 전체 여행에서 수행할 움직임을 찾아 출력해야 합니다. 오른쪽으로 이동하면 'R', 왼쪽으로 이동하면 'L', 위로 이동하면 'U', 아래로 이동하면 'D'를 인쇄합니다. 한 가지 주목해야 할 점은 x2> x1 및 y2> y1입니다.
따라서 입력이 x1 =0, y1 =1, x2 =3, y2 =4와 같으면 출력은 UURRRDDDLLLLUUURRRRDRDDDDLLLLU가 됩니다.
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
s := a blank string for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "U" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "R" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "D" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "L" at the end of s add "LU" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "U" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "R" at the end of s add "RD" at the end of s add "RD" at the end of s for initialize i := 0, when i < y2 - y1, update (increase i by 1), do: add "D" at the end of s for initialize i := 0, when i < x2 - x1, update (increase i by 1), do: add "L" at the end of s add "LU" at the end of s return s
예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
#include <bits/stdc++.h>
using namespace std;
string solve(int x1, int y1, int x2, int y2){
string s = "";
for(int i = 0; i < y2 - y1; i++)
s.append("U");
for(int i = 0; i < x2 - x1; i++)
s.append("R");
for(int i = 0; i < y2 - y1; i++)
s.append("D");
for(int i = 0; i < x2 - x1; i++)
s.append("L");
s.append("LU");
for(int i = 0; i < y2 - y1; i++)
s.append("U");
for(int i = 0; i < x2 - x1; i++)
s.append("R");
s.append("RD");
s.append("RD");
for(int i = 0; i < y2 - y1; i++)
s.append("D");
for(int i = 0; i < x2 - x1; i++) s.append("L");
s.append("LU");
return s;
}
int main() {
int x1 = 0, y1 = 1, x2 = 3, y2 = 4; cout<< solve(x1, y1, x2, y2);
return 0;
} 입력
0, 1, 3, 4
출력
UUURRRDDDLLLLUUUURRRRDRDDDDLLLLU