📜  最后一个方块的方向

📅  最后修改于: 2022-05-13 01:57:21.955000             🧑  作者: Mango

最后一个方块的方向

给定一个 R x C (1 <= R, C <= 1000000000) 网格,初始位置为左上角,方向为东。现在我们开始向前运行并穿过矩阵的每个方块。每当我们发现死胡同或到达一个已经访问过的单元格时,我们就向右走,因为我们不能再次穿过访问过的方块。当我们到达最后一个方块时,告诉方向。
例如:考虑 R = 3,C = 3 的情况。遵循的路径将是 (0, 0) — (0, 1) — (0, 2) — (1, 2) — (2, 2) — (2, 1) — (2, 0) — (1, 0) — (1, 1)。至此,所有的方格都被访问过了,并且都面向右边。
例子 :

Input  :  R = 1, C = 1
Output :  Right

Input   :  R = 2, C = 2
Output  :  Left

Input   :  R = 3, C = 1
Output  :  Down

Input  :  R = 3, C = 3
Output :  Right

简单解决方案:这个问题的一个简单解决方案是将其 R x C 矩阵初始化为零,并以螺旋形式遍历它,并采用一个变量“Dir”来指示当前方向。每当我们在任何行和列的末尾时,请选择“Right”并根据您当前的方向更改“Dir”的值。现在遵循给定的条件:

  • 如果您正在穿越顶行,那么您当前的方向是“右”。
  • 如果您在右列,那么您当前的方向是“向下”。
  • 如果您正在遍历底行,那么您当前的方向是“左”。
  • 如果您正在遍历左列,那么您当前的方向是“向上”。

当我们到达最后一个方格时,只需打印当前方向即; 'Dir' 变量的值。
此问题的时间和空间复杂度为 O(R x C),这仅适用于 R、C 的小值,但此处 R 和 C 太大,因此对于太大的 R 和值,创建 R x C 矩阵是不可能的C。
有效的方法:这种方法需要很少的观察和一些纸笔工作。在这里,我们必须考虑 R 和 C 的所有可能情况,然后我们只需要为所有可能的情况设置 IF 条件。在这里,我们拥有所有可能的条件:

  1. R != C,R 为偶数,C 为奇数,R
  2. R != C,R 为奇数,C 为偶数,R
  3. R != C 且 R 为偶数且 C 为偶数且 R
  4. R != C 且 R 为奇数且 C 为奇数且 R
  5. R != C 且 R 为偶数且 C 为奇数且 R>C,方向将为“向下”。
  6. R != C,R 为奇数,C 为偶数,R>C,方向为“向上”。
  7. R != C 且 R 为偶数且 C 为偶数且 R>C,方向为“向上”。
  8. R != C 且 R 为奇数且 C 为奇数且 R>C,方向将为“向下”。
  9. R == C 且 R 为偶数且 C 为偶数,方向为“左”。
  10. R == C,R 为奇数,C 为奇数,方向为“右”。

下面是上述想法的实现。

C++
// C++ program to tell the Current direction in
// R x C grid
#include 
using namespace std;
typedef long long int ll;
 
// Function which tells the Current direction
void direction(ll R, ll C)
{
    if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
        cout << "Left" << endl;
        return;
    }
    if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
        cout << "Up" << endl;
        return;
    }
    if (R == C && R % 2 != 0 && C % 2 != 0) {
        cout << "Right" << endl;
        return;
    }
    if (R == C && R % 2 == 0 && C % 2 == 0) {
        cout << "Left" << endl;
        return;
    }
    if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
        cout << "Right" << endl;
        return;
    }
    if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
        cout << "Down" << endl;
        return;
    }
    if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
        cout << "Left" << endl;
        return;
    }
    if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
        cout << "Up" << endl;
        return;
    }
    if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
        cout << "Down" << endl;
        return;
    }
    if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
        cout << "Right" << endl;
        return;
    }
}
 
// Driver program to test the Cases
int main()
{
    ll R = 3, C = 1;
    direction(R, C);
    return 0;
}


Java
// Java program to tell the Current direction in
// R x C grid
import java.io.*;
 
class GFG {
 
    // Function which tells the Current direction   
    static void direction(int R, int C)
    {
        if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) {
            System.out.println("Left");
            return;
        }
        if (R != C && R % 2 != 0 && C % 2 == 0 && R > C) {
            System.out.println("Up");
            return;
        }
        if (R == C && R % 2 != 0 && C % 2 != 0) {
            System.out.println("Right");
            return;
        }
        if (R == C && R % 2 == 0 && C % 2 == 0) {
            System.out.println("Left");
            return;
        }
        if (R != C && R % 2 != 0 && C % 2 != 0 && R < C) {
            System.out.println("Right");
            return;
        }
        if (R != C && R % 2 != 0 && C % 2 != 0 && R > C) {
            System.out.println("Down");
            return;
        }
        if (R != C && R % 2 == 0 && C % 2 == 0 && R < C) {
            System.out.println("Left");
            return;
        }
        if (R != C && R % 2 == 0 && C % 2 == 0 && R > C) {
            System.out.println("Up");
            return;
        }
        if (R != C && R % 2 == 0 && C % 2 != 0 && R > C) {
            System.out.println("Down");
            return;
        }
        if (R != C && R % 2 != 0 && C % 2 == 0 && R < C) {
            System.out.println("Right");
            return;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int R = 3, C = 1;
         
        direction(R, C);
    }
}
 
// This code is contributed by KRV.


Python3
# Python3 program to tell the Current
# direction in R x C grid
 
# Function which tells the Current direction
def direction(R, C):
    if (R != C and R % 2 == 0 and
        C % 2 != 0 and R < C):
        print("Left")
        return
 
    if (R != C and R % 2 == 0 and
        C % 2 == 0 and R > C):
        print("Up")
        return
 
    if R == C and R % 2 != 0 and C % 2 != 0:
        print("Right")
        return
 
    if R == C and R % 2 == 0 and C % 2 == 0:
        print("Left")
        return
 
    if (R != C and R % 2 != 0 and
        C % 2 != 0 and R < C):
        print("Right")
        return
 
    if (R != C and R % 2 != 0 and
        C % 2 != 0 and R > C):
        print("Down")
        return
 
    if (R != C and R % 2 == 0 and
        C % 2 != 0 and R < C):
        print("Left")
        return
 
    if (R != C and R % 2 == 0 and
        C % 2 == 0 and R > C):
        print("Up")
        return
 
    if (R != C and R % 2 != 0 and
        C % 2 != 0 and R > C):
        print("Down")
        return
 
    if (R != C and R % 2 != 0 and
        C % 2 != 0 and R < C):
        print("Right")
        return
 
# Driver code
R = 3; C = 1
direction(R, C)
 
# This code is contributed by Shrikant13


C#
// C# program to tell the Current
// direction in R x C grid
using System;
 
class GFG
{
     
    // Function which tells
    // the Current direction
    static void direction(int R, int C)
    {
        if (R != C && R % 2 == 0 &&
            C % 2 != 0 && R < C)
        {
            Console.WriteLine("Left");
            return;
        }
        if (R != C && R % 2 != 0 &&
            C % 2 == 0 && R > C)
        {
            Console.WriteLine("Up");
            return;
        }
        if (R == C && R % 2 != 0 &&
            C % 2 != 0)
        {
            Console.WriteLine("Right");
            return;
        }
        if (R == C && R % 2 == 0 &&
            C % 2 == 0)
        {
            Console.WriteLine("Left");
            return;
        }
        if (R != C && R % 2 != 0 &&
            C % 2 != 0 && R < C)
        {
            Console.WriteLine("Right");
            return;
        }
        if (R != C && R % 2 != 0 &&
            C % 2 != 0 && R > C)
        {
            Console.WriteLine("Down");
            return;
        }
        if (R != C && R % 2 == 0 &&
            C % 2 == 0 && R < C)
        {
            Console.WriteLine("Left");
            return;
        }
        if (R != C && R % 2 == 0 &&
            C % 2 == 0 && R > C)
        {
            Console.WriteLine("Up");
            return;
        }
        if (R != C && R % 2 == 0 &&
            C % 2 != 0 && R > C)
        {
            Console.WriteLine("Down");
            return;
        }
        if (R != C && R % 2 != 0 &&
            C % 2 == 0 && R < C)
        {
            Console.WriteLine("Right");
            return;
        }
    }
 
    // Driver code
    static public void Main ()
    {
        int R = 3, C = 1;
         
        direction(R, C);
    }
}
 
// This code is contributed by m_kit


PHP
 $C)
    {
        echo "Up" ,"\n";
        return;
    }
    if ($R == $C && $R % 2 != 0
                 && $C % 2 != 0)
    {
        echo "Right" ,"\n";
        return;
    }
    if ($R == $C && $R % 2 == 0
                 && $C % 2 == 0)
    {
        echo "Left" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 != 0 &&
                    $C % 2 != 0 && $R < $C)
    {
        echo "Right" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 != 0 &&
                    $C % 2 != 0 && $R > $C)
    {
        echo "Down" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 == 0 &&
                    $C % 2 == 0 && $R < $C)
    {
        echo "Left" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 == 0 &&
                    $C % 2 == 0 && $R > $C)
    {
        echo "Up" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 == 0 &&
                    $C % 2 != 0 && $R > $C)
    {
        echo "Down" ,"\n";
        return;
    }
    if ($R != $C && $R % 2 != 0 &&
                    $C % 2 == 0 && $R < $C)
    {
        echo "Right" ,"\n";
        return;
    }
}
 
// Driver Code
$R = 3; $C = 1;
direction($R, $C);
 
// This code is contributed by aj_36
?>


Javascript


输出 :

Down