📌  相关文章
📜  从 (0, 0) 开始访问 Matrix 的每个单元格后的最终方向

📅  最后修改于: 2021-10-26 05:35:20             🧑  作者: Mango

给定一个大小为N x M的二维网格。任务是在给定条件下访问每个单元格后找到最终方向。

  • 您只能从 N*M 网格的左上角开始并面向右侧。
  • 你可以在你面对的方向上一次走一格。
  • 如果您到达网格的边界,或者您即将访问的下一个方格已经访问过,请向右转。

例子:

处理方法:对于上面的问题陈述,我们必须遵守以下几点:

  • 形成的路径将始终是螺旋路径。所以,我们可以说任何中间的单元格都将是最后一个单元格,我们需要找到那个单元格的方向。
  • 如果n > m ,则最终方向只能是 Up 或 Down 取决于 m 的值,因为当所有其他列都被覆盖时,最后未覆盖的列中总会留下一些单元格。
  • 如果n <= m ,则最终方向只能是 Left 或 Right ,具体取决于 n 的值。

因此根据以上观察,4个方向只能有4种情况:

  1. 如果 n > m 且 m 为偶数,则最终方向将为 Up。
  2. 如果 n > m 且 m 为奇数,则最终方向将为向下。
  3. 如果 n <= m 且 n 为偶数,则最终方向将为 Left。
  4. 如果 n <= m 且 n 为奇数,则最终方向将为右。

下面是上述方法的实现:

C++
// C++ program to find the direction
// when stopped moving
 
#include 
using namespace std;
 
// Function to find the direction
// when stopped moving
void findDirection(int n, int m)
{
    if (n > m) {
        if (m % 2 == 0)
            printf("Up\n");
        else
            printf("Down\n");
    }
    else {
        if (n % 2 == 0)
            printf("Left\n");
        else
            printf("Right\n");
    }
}
 
// Driver Code
int main()
{
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
    return 0;
}


Java
// Java program to find the direction
// when stopped moving
class GFG{
 
// Function to find the direction
// when stopped moving
static void findDirection(int n, int m)
{
    if (n > m)
    {
        if (m % 2 == 0)
            System.out.print("Up\n");
        else
            System.out.print("Down\n");
    }
    else
    {
        if (n % 2 == 0)
            System.out.print("Left\n");
        else
            System.out.print("Right\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
}
}
 
// This code is contributed by shubham


Python3
# Python3 program to find the direction
# when stopped moving
 
# Function to find the direction
# when stopped moving
def findDirection(n, m):
 
    if (n > m):
        if (m % 2 == 0):
            print("Up\n");
        else:
            print("Down\n");
     
    else:
        if (n % 2 == 0):
            print("Left\n");
        else:
            print("Right\n");
     
# Driver Code
 
# Given size of NxM grid
n = 3; m = 3;
 
# Function Call
findDirection(n, m);
 
# This code is contributed by Code_Mech


C#
// C# program to find the direction
// when stopped moving
using System;
class GFG{
 
// Function to find the direction
// when stopped moving
static void findDirection(int n, int m)
{
    if (n > m)
    {
        if (m % 2 == 0)
            Console.Write("Up\n");
        else
            Console.Write("Down\n");
    }
    else
    {
        if (n % 2 == 0)
            Console.Write("Left\n");
        else
            Console.Write("Right\n");
    }
}
 
// Driver code
public static void Main()
{
     
    // Given size of NxM grid
    int n = 3, m = 3;
 
    // Function Call
    findDirection(n, m);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Right

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程