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

📅  最后修改于: 2021-04-22 07:16:16             🧑  作者: Mango

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

  • 您只能从N * M网格的左上角开始,并朝右。
  • 您可以朝着您的方向一次走一格。
  • 如果到达网格的边界,或者已经访问过您要访问的下一个正方形,则向右转。

例子:

方法:对于上面的问题陈述,我们必须注意以下几点:

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

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

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

下面是上述方法的实现:

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)