检查机器人的给定移动序列是否是圆形的
给定一个机器人的移动序列,检查该序列是否是圆形的。如果机器人的第一个和最后一个位置相同,则一系列移动是循环的。移动可以是以下之一。
G - Go one unit
L - Turn left
R - Turn right
例子:
Input: path[] = "GLGLGLG"
Output: Given sequence of moves is circular
Input: path[] = "GLLG"
Output: Given sequence of moves is circular
我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。
这个想法是将起始位置视为 (0, 0),将方向视为东(我们可以为这些选择任何值)。如果在给定的移动序列之后,我们回到 (0, 0),那么给定的序列是循环的,否则不是。
N
|
|
W -------------- E
|
|
S
移动“G”根据以下规则改变 x 或 y。
a) 如果当前方向是北,那么“G”增加 y 并且不改变 x。
b) 如果当前方向是东,那么“G”增加 x 并且不改变 y。
c) 如果当前方向为南,则“G”递减 y 并且不改变 x。
d) 如果当前方向是西,那么'G'递减 x 并且不改变 y。
移动'L'和'R',不改变x和y坐标,它们只根据以下规则改变方向。
a) 如果当前方向为北,则“L”将方向更改为西,“R”更改为东
b) 如果当前方向为东,则“L”将方向更改为北,“R”更改为南
c) 如果当前方向为南,则“L”将方向更改为东,“R”更改为西
d) 如果当前方向为西,则“L”将方向更改为南,“R”更改为北。
以下是上述想法的实现:
C++
// A c++ program to check if the given path for a robot is circular or not
#include
using namespace std;
// Macros for East, North, South and West
#define N 0
#define E 1
#define S 2
#define W 3
// This function returns true if the given path is circular, else false
bool isCircular(char path[])
{
// Initialize starting point for robot as (0, 0) and starting
// direction as N North
int x = 0, y = 0;
int dir = N;
// Traverse the path given for robot
for (int i=0; path[i]; i++)
{
// Find current move
char move = path[i];
// If move is left or right, then change direction
if (move == 'R')
dir = (dir + 1)%4;
else if (move == 'L')
dir = (4 + dir - 1)%4;
// If move is Go, then change x or y according to
// current direction
else // if (move == 'G')
{
if (dir == N)
y++;
else if (dir == E)
x++;
else if (dir == S)
y--;
else // dir == W
x--;
}
}
// If robot comes back to (0, 0), then path is cyclic
return (x == 0 && y == 0);
}
// Driver program
int main()
{
char path[] = "GLGLGLG";
if (isCircular(path))
cout << "Given sequence of moves is circular";
else
cout << "Given sequence of moves is NOT circular";
}
Java
// Write Java code here
// A Java program to check if
// the given path for a robot
// is circular or not
class GFG {
// Macros for East, North, South and West
// This function returns true if
// the given path is circular,
// else false
static boolean isCircular(char path[])
{
// Initialize starting
// point for robot as
// (0, 0) and starting
// direction as N North
int x = 0, y = 0;
int dir = 0;
// Traverse the path given for robot
for (int i=0; i < path.length; i++)
{
// Find current move
char move = path[i];
// If move is left or
// right, then change direction
if (move == 'R')
dir = (dir + 1)%4;
else if (move == 'L')
dir = (4 + dir - 1) % 4;
// If move is Go, then
// change x or y according to
// current direction
else // if (move == 'G')
{
if (dir == 0)
y++;
else if (dir == 1)
x++;
else if (dir == 2)
y--;
else // dir == 3
x--;
}
}
// If robot comes back to
// (0, 0), then path is cyclic
return (x == 0 && y == 0);
}
// Driver program
public static void main(String[] args)
{
String path_ = "GLGLGLG";
char path[] = path_.toCharArray();
if (isCircular(path))
System.out.println("Given sequence" +
" of moves is circular");
else
System.out.println("Given sequence" +
" of moves is NOT circular");
}
}
// This code is contributed by prerna saini.
Python
# Python program to check if the given path for a robot is circular
# or not
N = 0
E = 1
S = 2
W = 3
# This function returns true if the given path is circular,
# else false
def isCircular(path):
# Initialize starting point for robot as (0, 0) and starting
# direction as N North
x = 0
y = 0
dir = N
# Traverse the path given for robot
for i in xrange(len(path)):
# Find current move
move = path[i]
# If move is left or right, then change direction
if move == 'R':
dir = (dir + 1)%4
elif move == 'L':
dir = (4 + dir - 1)%4
# If move is Go, then change x or y according to
# current direction
else: # if move == 'G'
if dir == N:
y += 1
elif dir == E:
x += 1
elif dir == S:
y -= 1
else:
x -= 1
return (x == 0 and y == 0)
# Driver program
path = "GLGLGLG"
if isCircular(path):
print "Given sequence of moves is circular"
else:
print "Given sequence of moves is NOT circular"
# This code is contributed by BHAVYA JAIN
C#
// A C# program to check if
// the given path for a robot
// is circular or not
using System;
class GFG {
// Macros for East, North, South and West
// This function returns true if
// the given path is circular,
// else false
static bool isCircular(string path)
{
// Initialize starting
// point for robot as
// (0, 0) and starting
// direction as N North
int x = 0, y = 0;
int dir = 0;
// Traverse the path
// given for robot
for (int i = 0; i < path.Length; i++)
{
// Find current move
char move = path[i];
// If move is left or
// right, then change direction
if (move == 'R')
dir = (dir + 1) % 4;
else if (move == 'L')
dir = (4 + dir - 1) % 4;
// If move is Go, then
// change x or y according to
// current direction
// if (move == 'G')
else
{
if (dir == 0)
y++;
else if (dir == 1)
x++;
else if (dir == 2)
y--;
else // dir == 3
x--;
}
}
// If robot comes back to
// (0, 0), then path is cyclic
return (x == 0 && y == 0);
}
// Driver Code
public static void Main(String[] args)
{
string path = "GLGLGLG";
if (isCircular(path))
Console.WriteLine("Given sequence of moves is circular");
else
Console.WriteLine("Given sequence of moves is NOT circular");
}
}
// This code is contributed by Sam007
Javascript
输出:
Given sequence of moves is circular
时间复杂度:O(n),其中 n 是给定序列中的移动次数。