给定一个由字符‘U’ 、 ‘D’ 、 ‘L’和‘R’组成的数组A[]表示向上、向下、向左和向右的方向,另一个数组B[]由N 个正整数组成,任务是找到机器人的位移,从(0, 0) 开始,面向北,经过N 次移动,其中在第i次移动中,机器人向A[i]方向移动距离B [i] 。
例子:
Input: A[] = {U, R, R, R, R}, B[] = {1, 1, 1, 1, 0}
Output: 0 North
Explanation:
Initially, the robot is at (0, 0) facing North.
After 1st move, the position is (0, 1) facing North.
After 2nd move, the position is (1, 1) facing East.
After 3rd move, the position is (1, 0) facing South.
After 4th move, the position is (0, 0) facing West.
After 5th move, the position is (0, 0) facing North.
Therefore, the displacement is 0 and the final direction is North.
Input: A[] = {U, L, R, D, R}, B[] = {5, 5, 5, 5, 5}
Output: 11 West
方法:给定的问题可以通过遍历给定的数组来解决,并保持对North(N) 、 South(S) 、 East(E)和West(W)方向行进的方向和距离的跟踪。请按照以下步骤解决问题:
- 初始化变量,例如N 、 S 、 E和W ,它们分别存储在给定的一组运动后沿North 、 South 、 East和West方向穿越的距离。
- 初始化一个变量,比如P as North ,它在执行给定的一组运动后存储最终方向。
- 同时遍历给定的数组A[]和B[] ,并用当前方向更新所有方向的距离遍历值。
- 现在,垂直位移由(N – S)给出,水平位移由(E – S) 给出。
- 完成以上步骤后,打印最终位移为以及存储在P 中的最终方向。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
void finalPosition(char a[], int b[],
int M)
{
// Stores the distances travelled
// in the directions North, South,
// East, and West respectively
int n = 0, s = 0, e = 0, w = 0;
// Store the initial
// position of robot
char p = 'N';
// Traverse the array B[]
for (int i = 0; i < M; i++) {
// If the current
// direction is North
if (p == 'N') {
if (a[i] == 'U') {
p = 'N';
n = n + b[i];
}
else if (a[i] == 'D') {
p = 'S';
s = s + b[i];
}
else if (a[i] == 'R') {
p = 'E';
e = e + b[i];
}
else if (a[i] == 'L') {
p = 'W';
w = w + b[i];
}
}
// If the current
// direction is South
else if (p == 'S') {
if (a[i] == 'U') {
p = 'S';
s = s + b[i];
}
else if (a[i] == 'D') {
p = 'N';
n = n + b[i];
}
else if (a[i] == 'R') {
p = 'W';
w = w + b[i];
}
else if (a[i] == 'L') {
p = 'E';
e = e + b[i];
}
}
// If the current
// direction is East
else if (p == 'E') {
if (a[i] == 'U') {
p = 'E';
e = e + b[i];
}
else if (a[i] == 'D') {
p = 'W';
w = w + b[i];
}
else if (a[i] == 'R') {
p = 'S';
s = s + b[i];
}
else if (a[i] == 'L') {
p = 'N';
n = n + b[i];
}
}
// If the current
// direction is West
else if (p == 'W') {
if (a[i] == 'U') {
p = 'W';
w = w + b[i];
}
else if (a[i] == 'D') {
p = 'E';
e = e + b[i];
}
else if (a[i] == 'R') {
p = 'N';
n = n + b[i];
}
else if (a[i] == 'L') {
p = 'S';
s = s + b[i];
}
}
}
// Stores the total
// vertical displacement
int ver_disp = n - s;
// Stores the total
// horizontal displacement
int hor_disp = e - w;
// Find the displacement
int displacement = floor(
sqrt((ver_disp * ver_disp)
+ (hor_disp * hor_disp)));
// Print the displacement and
// direction after N moves
cout << displacement << " " << p;
}
// Driver Code
int main()
{
char A[] = { 'U', 'R', 'R', 'R', 'R' };
int B[] = { 1, 1, 1, 1, 0 };
int N = sizeof(A) / sizeof(B[0]);
finalPosition(A, B, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
static void finalPosition(char[] a, int[] b, int M)
{
// Stores the distances travelled
// in the directions North, South,
// East, and West respectively
int n = 0, s = 0, e = 0, w = 0;
// Store the initial
// position of robot
char p = 'N';
// Traverse the array B[]
for(int i = 0; i < M; i++)
{
// If the current
// direction is North
if (p == 'N')
{
if (a[i] == 'U')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'D')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'R')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'L')
{
p = 'W';
w = w + b[i];
}
}
// If the current
// direction is South
else if (p == 'S')
{
if (a[i] == 'U')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'D')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'R')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'L')
{
p = 'E';
e = e + b[i];
}
}
// If the current
// direction is East
else if (p == 'E')
{
if (a[i] == 'U')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'D')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'R')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'L')
{
p = 'N';
n = n + b[i];
}
}
// If the current
// direction is West
else if (p == 'W')
{
if (a[i] == 'U')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'D')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'R')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'L')
{
p = 'S';
s = s + b[i];
}
}
}
// Stores the total
// vertical displacement
int ver_disp = n - s;
// Stores the total
// horizontal displacement
int hor_disp = e - w;
// Find the displacement
int displacement = (int)Math.ceil(Math.sqrt(
(ver_disp * ver_disp) + (hor_disp * hor_disp)));
// Print the displacement and
// direction after N moves
System.out.print(displacement + " " + p);
}
// Driver Code
public static void main(String args[])
{
char[] A = { 'U', 'R', 'R', 'R', 'R' };
int[] B = { 1, 1, 1, 1, 0 };
int N = 1;
finalPosition(A, B, N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
from math import sqrt, floor
# Function to find the displacement
# from the origin and direction after
# performing the given set of moves
def finalPosition(a, b, M):
# Stores the distances travelled
# in the directions North, South,
# East, and West respectively
n = 0
s = 0
e = 0
w = 0
# Store the initial
# position of robot
p = 'N'
# Traverse the array B[]
for i in range(M):
# If the current
# direction is North
if (p == 'N'):
if (a[i] == 'U'):
p = 'N'
n = n + b[i]
elif (a[i] == 'D'):
p = 'S'
s = s + b[i]
elif (a[i] == 'R'):
p = 'E'
e = e + b[i]
elif (a[i] == 'L'):
p = 'W'
w = w + b[i]
# If the current
# direction is South
elif (p == 'S'):
if (a[i] == 'U'):
p = 'S'
s = s + b[i]
elif(a[i] == 'D'):
p = 'N'
n = n + b[i]
elif(a[i] == 'R'):
p = 'W'
w = w + b[i]
elif(a[i] == 'L'):
p = 'E'
e = e + b[i]
# If the current
# direction is East
elif(p == 'E'):
if (a[i] == 'U'):
p = 'E'
e = e + b[i]
elif (a[i] == 'D'):
p = 'W'
w = w + b[i]
elif (a[i] == 'R'):
p = 'S'
s = s + b[i]
elif (a[i] == 'L'):
p = 'N'
n = n + b[i]
# If the current
# direction is West
elif (p == 'W'):
if (a[i] == 'U'):
p = 'W'
w = w + b[i]
elif (a[i] == 'D'):
p = 'E'
e = e + b[i]
elif (a[i] == 'R'):
p = 'N'
n = n + b[i]
elif (a[i] == 'L'):
p = 'S'
s = s + b[i]
# Stores the total
# vertical displacement
ver_disp = n - s
# Stores the total
# horizontal displacement
hor_disp = e - w
# Find the displacement
displacement = floor(sqrt((ver_disp * ver_disp) +
(hor_disp * hor_disp)) + 1)
# Print the displacement and
# direction after N moves
print(displacement,p)
# Driver Code
if __name__ == '__main__':
A = [ 'U', 'R', 'R', 'R', 'R' ]
B = [ 1, 1, 1, 1, 0 ]
N = len(A)
finalPosition(A, B, N)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the displacement
// from the origin and direction after
// performing the given set of moves
static void finalPosition(char[] a, int[] b, int M)
{
// Stores the distances travelled
// in the directions North, South,
// East, and West respectively
int n = 0, s = 0, e = 0, w = 0;
// Store the initial
// position of robot
char p = 'N';
// Traverse the array B[]
for(int i = 0; i < M; i++)
{
// If the current
// direction is North
if (p == 'N')
{
if (a[i] == 'U')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'D')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'R')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'L')
{
p = 'W';
w = w + b[i];
}
}
// If the current
// direction is South
else if (p == 'S')
{
if (a[i] == 'U')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'D')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'R')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'L')
{
p = 'E';
e = e + b[i];
}
}
// If the current
// direction is East
else if (p == 'E')
{
if (a[i] == 'U')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'D')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'R')
{
p = 'S';
s = s + b[i];
}
else if (a[i] == 'L')
{
p = 'N';
n = n + b[i];
}
}
// If the current
// direction is West
else if (p == 'W')
{
if (a[i] == 'U')
{
p = 'W';
w = w + b[i];
}
else if (a[i] == 'D')
{
p = 'E';
e = e + b[i];
}
else if (a[i] == 'R')
{
p = 'N';
n = n + b[i];
}
else if (a[i] == 'L')
{
p = 'S';
s = s + b[i];
}
}
}
// Stores the total
// vertical displacement
int ver_disp = n - s;
// Stores the total
// horizontal displacement
int hor_disp = e - w;
// Find the displacement
int displacement = (int)Math.Ceiling(Math.Sqrt(
(ver_disp * ver_disp) + (hor_disp * hor_disp)));
// Print the displacement and
// direction after N moves
Console.WriteLine(displacement + " " + p);
}
// Driver Code
public static void Main()
{
char[] A = { 'U', 'R', 'R', 'R', 'R' };
int[] B = { 1, 1, 1, 1, 0 };
int N = 1;
finalPosition(A, B, N);
}
}
// This code is contributed by ukasp
Javascript
1 N
时间复杂度: O(N)
辅助空间: O(1)