Comprobar si una determinada secuencia de movimientos de un robot es circular o no

Dada una secuencia de movimientos de un robot, compruebe si la secuencia es circular o no. Una secuencia de movimientos es circular si la primera y la última posición del robot son las mismas. Un movimiento puede ser uno de los siguientes. 

  G - Go one unit
  L - Turn left
  R - Turn right 

Ejemplos: 

C++

// A c++ program to check if the given path for a robot is circular or not
#include<iostream>
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

<script>
    // A Javascript program to check if
    // the given path for a robot
    // is circular or not
     
    // Macros for East, North, South and West
   
    // This function returns true if
    // the given path is circular,
    // else false
    function isCircular(path)
    {
 
        // Initialize starting
        // point for robot as
        // (0, 0) and starting
        // direction as N North
        let x = 0, y = 0;
        let dir = 0;
 
        // Traverse the path
        // given for robot
        for (let i = 0; i < path.length; i++)
        {
 
            // Find current move
            let 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);
    }
     
    let path = "GLGLGLG";
       
    if (isCircular(path))
      document.write("Given sequence of moves is circular");
    else
      document.write("Given sequence of moves is NOT circular");
     
    // This code is contributed by decode2207.
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *