给定一个数组commands[] ,由带符号的整数组成,表示与坐标一起行进的距离和方向,数组障碍 []表示无法访问的坐标,任务是找到最大欧几里德距离的平方的平方可以从原点 (0, 0) 开始并朝北行驶,遵循在命令 []数组中的序列中指定的命令,分为以下三种类型:
- -2 : 左转 90 度。
- -1 : 向右转 90 度。
- 1<= X <= 9 :向前移动 X 个单位。
例子:
Input: commands[] = {4, -1, 4, -2, 4}, obstacles[] = {{ 2, 4 }}
Output: 65
Explanation:
Step 1: (0, 0) -> (0, 4)
Step 2: (0, 4) -> (1, 4)
Step 3 and 4:
Obstacles
Step 5: (1, 4) -> (1, 8)
Input: commands[] = {4, -1, 3}, obstacles[] = {}
Output: 25
处理方法:按照以下步骤解决问题:
- 最初,机器人在(0, 0)朝北。
- 分配变量以在每一步后跟踪机器人的当前位置和方向。
- 将障碍物的坐标存储在 HashMap 中。
- 制作2 个数组(dx[], dy[])并根据方向的变化将所有可能的运动存储在x和y坐标中。
- 如果遇到方向变化,请参考 2 个阵列更改当前方向。
- 否则,如果中间没有障碍物,继续朝同一方向移动,直到遇到方向改变。
- 最后,计算 x 和 y 坐标的平方。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
void robotSim(vector commands,
vector> obstacles)
{
// Possible movements in x coordinate.
vector dx = { 0, 1, 0, -1 };
// Possible movements in y coordinate.
vector dy = { 1, 0, -1, 0 };
int x = 0, y = 0;
int di = 0;
// Put all obstacles into hashmap.
map, int>obstacleSet;
for(auto i:obstacles)
obstacleSet[i] = 1;
// Maximum distance
int ans = 0;
// Iterate commands.
for(int cmd : commands)
{
// Left direction
if (cmd == -2)
di = (di-1) % 4;
// Right direction
else if (cmd == -1)
di = (di + 1) % 4;
// If no direction changes
else
{
for(int i = 0; i < cmd; i++)
{
// Checking for obstacles.
if (obstacleSet.find({x + dx[di],
y + dy[di]}) ==
obstacleSet.end())
{
// Update x coordinate
x += dx[di];
// Update y coordinate
y += dy[di];
// Updating for max distance
ans = max(ans, x * x + y * y);
}
}
}
}
cout << ans << endl;
}
// Driver Code
int main()
{
vector commands = { 4, -1, 4, -2, 4 };
vector> obstacles = { { 2, 4 } };
robotSim(commands, obstacles);
}
// This code is contributed by grand_master
Java
// Java program to implement
// the above approach
import java.util.*;
import java.awt.Point;
class GFG{
static void robotSim(List commands,
List obstacles)
{
// Possible movements in x coordinate.
List dx = Arrays.asList(0, 1, 0, -1);
// Possible movements in y coordinate.
List dy = Arrays.asList(1, 0, -1, 0);
int x = 0, y = 0;
int di = 0;
// Put all obstacles into hashmap.
HashMap obstacleSet = new HashMap<>();
for(Point i : obstacles)
obstacleSet.put(i, 1);
// Maximum distance
int ans = 0;
// Iterate commands.
for(Integer cmd : commands)
{
// Left direction
if (cmd == -2)
di = (di - 1) % 4;
// Right direction
else if (cmd == -1)
di = (di + 1) % 4;
// If no direction changes
else
{
for(int i = 0; i < cmd; i++)
{
// Checking for obstacles.
if (!obstacleSet.containsKey(
new Point(x + dx.get(di),
y + dy.get(di))))
{
// Update x coordinate
x += dx.get(di);
// Update y coordinate
y += dy.get(di);
// Updating for max distance
ans = Math.max(ans, x * x + y * y);
}
}
}
}
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
List commands = Arrays.asList(
4, -1, 4, -2, 4);
List obstacles = new ArrayList<>();
obstacles.add(new Point(2, 4));
robotSim(commands, obstacles);
}
}
// This code is contributed by divyesh072019
Python3
# Python3 Program to implement
# the above approach
def robotSim(commands, obstacles):
# Possible movements in x coordinate.
dx = [0, 1, 0, -1]
# Possible movements in y coordinate.
dy = [1, 0, -1, 0]
# Initialise position to (0, 0).
x, y = 0, 0
# Initial direction is north.
di = 0
# Put all obstacles into hashmap.
obstacleSet = set(map(tuple, obstacles))
# maximum distance
ans = 0
# Iterate commands.
for cmd in commands:
# Left direction
if cmd == -2:
di = (di-1) % 4
# Right direction
elif cmd == -1:
di = (di + 1) % 4
# If no direction changes
else:
for i in range(cmd):
# Checking for obstacles.
if (x + dx[di], y + dy[di]) \
not in obstacleSet:
# Update x coordinate
x += dx[di]
# Update y coordinate
y += dy[di]
# Updating for max distance
ans = max(ans, x * x + y * y)
print(ans)
# Driver Code
if __name__ == "__main__":
commands = [4, -1, 4, -2, 4]
obstacles = [[2, 4]]
robotSim(commands, obstacles)
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG {
static void robotSim(List commands,
List> obstacles)
{
// Possible movements in x coordinate.
List dx = new List(new int[]{ 0, 1, 0, -1 });
// Possible movements in y coordinate.
List dy = new List(new int[]{ 1, 0, -1, 0 });
int x = 0, y = 0;
int di = 0;
// Put all obstacles into hashmap.
Dictionary, int> obstacleSet =
new Dictionary, int>();
foreach(Tuple i in obstacles)
obstacleSet[i] = 1;
// Maximum distance
int ans = 0;
// Iterate commands.
foreach(int cmd in commands)
{
// Left direction
if (cmd == -2)
di = (di - 1) % 4;
// Right direction
else if (cmd == -1)
di = (di + 1) % 4;
// If no direction changes
else
{
for(int i = 0; i < cmd; i++)
{
// Checking for obstacles.
if (!obstacleSet.ContainsKey(new Tuple(x + dx[di],
y + dy[di])))
{
// Update x coordinate
x += dx[di];
// Update y coordinate
y += dy[di];
// Updating for max distance
ans = Math.Max(ans, x * x + y * y);
}
}
}
}
Console.WriteLine(ans);
}
// Driver code
static void Main()
{
List commands = new List(new int[]{ 4, -1, 4, -2, 4 });
List> obstacles = new List>();
obstacles.Add(new Tuple(2,4));
robotSim(commands, obstacles);
}
}
// This code is contributed by divyeshrabadiya07.
输出:
65
时间复杂度: O(N)
辅助空间: O(N + M),其中 M 是障碍物阵列的长度。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。