Posición final de la celda en la array.

Dada una array de lista de comandos U (Arriba), D (Abajo), L (Izquierda) y R (Derecha) y la posición inicial de la celda (x, y) en una array. Encuentre la posición de celda final del objeto en la array después de seguir los comandos dados. Se supone que la posición de celda requerida final existe en la array. 
Ejemplos
 

Input : command[] = "DDLRULL"
        x = 3, y = 4
Output : (1, 5)

Input : command[] = "LLRUUUDRRDDDULRLLUDUUR"
        x = 6, y = 5
Output : (6, 3)

Fuente: Entrevista Flipkart (SDE-1 en el campus).
 

Enfoque: Los siguientes son los pasos:
 

  1. Cuente la taza, cabajo, la hendidura y la derecha para los movimientos U (Arriba), D (Abajo), L (Izquierda) y R (Derecha) respectivamente.
  2. Calcula final_x = x + (cright – hendidura) y final_y = y + (cdown – copa).

La posición final de la celda es (final_x, final_y)
 

C++

// C++ implementation to find the final cell position
// in the given matrix
#include <bits/stdc++.h>
 
using namespace std;
 
// function to find the final cell position
// in the given matrix
void finalPos(char command[], int n,
              int x, int y)
{
    // to count up, down, left and cright
    // movements
    int cup, cdown, cleft, cright;
 
    // to store the final coordinate position
    int final_x, final_y;
 
    cup = cdown = cleft = cright = 0;
 
    // traverse the command array
    for (int i = 0; i < n; i++) {
        if (command[i] == 'U')
            cup++;
        else if (command[i] == 'D')
            cdown++;
        else if (command[i] == 'L')
            cleft++;
        else if (command[i] == 'R')
            cright++;
    }
 
    // calculate final values
    final_x = x + (cright - cleft);
    final_y = y + (cdown - cup);
 
    cout << "Final Position: "
         << "("
         << final_x << ", " << final_y << ")";
}
 
// Driver program to test above
int main()
{
    char command[] = "DDLRULL";
    int n = (sizeof(command) / sizeof(char)) - 1;
    int x = 3, y = 4;
    finalPos(command, n, x, y);
    return 0;
}

Java

// Java implementation to find
// the final cell position
// in the given matrix
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
// function to find the
// final cell position
// in the given matrix
static void finalPos(String command, int n,
                              int x, int y)
{
    // to count up, down, left
    // and cright movements
    int cup, cdown, cleft, cright;
 
    // to store the final
    // coordinate position
    int final_x, final_y;
 
    cup = cdown = cleft = cright = 0;
 
    // traverse the command array
    for (int i = 0; i < n; i++)
    {
        if (command.charAt(i) == 'U')
            cup++;
        else if (command.charAt(i) == 'D')
            cdown++;
        else if (command.charAt(i)== 'L')
            cleft++;
        else if (command.charAt(i) == 'R')
            cright++;
    }
 
    // calculate final values
    final_x = x + (cright - cleft);
    final_y = y + (cdown - cup);
 
    System.out.println("Final Position: " +
                       "(" + final_x + ", " +
                              final_y + ")");
}
 
// Driver Code
public static void main(String []args)
{
    String command = "DDLRULL";
    int n = command.length();
    int x = 3, y = 4;
    finalPos(command, n, x, y);
}
}
 
// This code is contributed
// by Subhadeep

C#

// C# implementation to find
// the final cell position
// in the given matrix
class GFG
{
// function to find the
// final cell position
// in the given matrix
static void finalPos(string command, int n,
                              int x, int y)
{
    // to count up, down, left
    // and cright movements
    int cup, cdown, cleft, cright;
 
    // to store the final
    // coordinate position
    int final_x, final_y;
 
    cup = cdown = cleft = cright = 0;
 
    // traverse the command array
    for (int i = 0; i < n; i++)
    {
        if (command[i] == 'U')
            cup++;
        else if (command[i] == 'D')
            cdown++;
        else if (command[i]== 'L')
            cleft++;
        else if (command[i] == 'R')
            cright++;
    }
 
    // calculate final values
    final_x = x + (cright - cleft);
    final_y = y + (cdown - cup);
 
    System.Console.WriteLine("Final Position: " +
                             "(" + final_x + ", " +
                                    final_y + ")");
}
 
// Driver Code
public static void Main()
{
    string command = "DDLRULL";
    int n = command.Length;
    int x = 3, y = 4;
    finalPos(command, n, x, y);
}
}
 
// This code is contributed
// by mits

PHP

<?php
// PHP implementation to find the final
// cell position in the given matrix
 
// function to find the final cell
// position in the given matrix
function finalPos($command, $n, $x, $y)
{
    // to count up, down, left and
    // cright movements
    $cup; $cdown; $cleft; $cright;
 
    // to store the final coordinate
    // position
    $final_x; $final_y;
 
    $cup = $cdown = $cleft = $cright = 0;
 
    // traverse the command array
    for ($i = 0; $i < $n; $i++)
    {
        if ($command[$i] == 'U')
            $cup++;
        else if ($command[$i] == 'D')
            $cdown++;
        else if ($command[$i] == 'L')
            $cleft++;
        else if ($command[$i] == 'R')
            $cright++;
    }
 
    // calculate final values
    $final_x = $x + ($cright - $cleft);
    $final_y = $y + ($cdown - $cup);
 
    echo "Final Position: " . "(" .
                  $final_x . ", " .
                  $final_y . ")";
}
 
// Driver Code
$command = "DDLRULL";
$n = strlen($command);
$x = 3; $y = 4;
finalPos($command, $n, $x, $y);
 
// This code is contributed
// by Akanksha Rai

Javascript

<script>
 
// JavaScript implementation to find
// the final cell position
// in the given matrix
 
// function to find the
// final cell position
// in the given matrix
function finalPos(command, n, x, y)
{
    // to count up, down, left
    // and cright movements
    let cup, cdown, cleft, cright;
   
    // to store the final
    // coordinate position
    let final_x, final_y;
   
    cup = cdown = cleft = cright = 0;
   
    // traverse the command array
    for (let i = 0; i < n; i++)
    {
        if (command[i] == 'U')
            cup++;
        else if (command[i] == 'D')
            cdown++;
        else if (command[i]== 'L')
            cleft++;
        else if (command[i] == 'R')
            cright++;
    }
   
    // calculate final values
    final_x = x + (cright - cleft);
    final_y = y + (cdown - cup);
   
    document.write("Final Position: " +
                             "(" + final_x + ", " +
                                    final_y + ")");
}
  
 
// driver code
 
    let command = "DDLRULL";
    let n = command.length;
    let x = 3, y = 4;
    finalPos(command, n, x, y);
   
</script>

Producción:  

Final Position: (1, 5)

Complejidad de tiempo : O(n), donde  norte   es el número de comandos.

Complejidad del espacio: O(1) ya que usa variables constantes
 

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 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 *