📌  相关文章
📜  矩阵中的最终单元格位置

📅  最后修改于: 2021-10-26 07:00:21             🧑  作者: Mango

给定一组命令 U(Up)、D(Down)、L(Left) 和 R(Right) 以及矩阵中的初始单元格位置 (x, y)。按照给定的命令查找对象在矩阵中的最终单元格位置。假设最终需要的单元格位置存在于矩阵中。
例子

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

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

资料来源:Flipkart 采访(校园内的 SDE-1)。

方法:以下是步骤:

  1. 分别为 U(Up)、D(Down)、L(Left) 和 R(Right) 运动计算 cup、cdown、cleft 和 cright。
  2. 计算 final_x = x + (cright – cleft) 和 final_y = y + (cdown – cup)。

最终单元格位置为 (final_x, final_y)

C++
// C++ implementation to find the final cell position
// in the given matrix
#include 
 
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


Javascript


输出:

Final Position: (1, 5)

时间复杂度:O(n),其中n  是命令的数量。