📜  将给定矩阵转换为另一个矩阵所需的最小旋转次数

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

将给定矩阵转换为另一个矩阵所需的最小旋转次数

给定两个大小相等的二维数组arr[][]brr[][] 。任务是找到将arr[][]转换为brr[][]所需的顺时针或逆时针最小旋转次数,方法如下:

  • +x 表示 x 顺时针方向旋转
  • -x 表示x逆时针方向旋转
  • 0 表示不需要旋转,并且
  • NA 表示无法旋转

例子

方法:给定的问题是基于实现的,类似于 90 度旋转矩阵。请按照以下步骤解决给定的问题。

  • 创建一个函数,比如rotate() ,将矩阵顺时针旋转 90 度。
  • 创建一个函数说isEqual() ,以检查两个矩阵是否相等。
  • 首先,检查两个数组是否已经相等。如果相等则打印0并返回,因为在这种情况下需要0 次操作。
  • arr[][]旋转904 次,每次检查它是否等于 brr。
  • 如果在任何时候arr[][]变得等于brr[][]然后检查顺时针逆时针是否给出最小数量或旋转以达到brr[][]
  • 打印上面找到的结果。
  • 如果在顺时针旋转 4 次后, arr[][]从未等于brr[][] ,则在最后打印NA

下面是该方法的实现:

C++
// C++ program for above approach
#include 
#include 
using namespace std;
 
// Function to check if two matrices
// are equal or not
bool isEqual(vector >& arr,
             vector >& brr)
{
    int n = arr.size();
    int m = arr[0].size();
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (arr[i][j] != brr[i][j])
                return false;
        }
    }
 
    return true;
}
 
// Function to rotate matrix by 90 degrees clockwise
void rotate(vector >& m)
{
    int n = m.size();
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < i; j++)
            swap(m[i][j], m[j][i]);
 
    for (int i = 0; i < n; i++)
        reverse(m[i].begin(), m[i].end());
}
 
// Find Minimum rotation to reach the desired matrix
void findRotation(vector > arr,
                  vector > brr)
{
 
    if (isEqual(arr, brr)) {
        cout << 0;
        return;
    }
 
    for (int i = 1; i < 4; i++) {
 
        // Rotate by 90 degrees clockwise
        rotate(arr);
 
        // Checking if both arr[][] and brr[]
        // are now equal or not
        if (isEqual(arr, brr)) {
            if (i < 4 - i) {
                cout << "+" << i;
            }
            else
                cout << "-" << 4 - i;
            return;
        }
    }
 
    // If converting brr[][] is not possible
    cout << "NA";
}
 
// Driver Code
int main()
{
 
    vector > arr, brr;
 
    arr = { { 2, 3 }, { 4, 5 } };
    brr = { { 4, 2 }, { 5, 3 } };
 
    // Function Call
    findRotation(arr, brr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to check if two matrices
    // are equal or not
   static Boolean isEqual(int[ ][ ] arr, int[ ][ ] brr)
    {
        int n = arr.length;
        int m = arr[0].length;
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (arr[i][j] != brr[i][j])
                    return false;
            }
        }
 
        return true;
    }
 
    // Function to rotate matrix by 90 degrees clockwise
    static void rotate(int [ ][ ] m)
    {
        int n = m.length;
 
        for (int i = 0; i < n; i++)
            for (int j = 0; j < i; j++){
                      //swap
                      int temp = m[i][j];
                      m[i][j] = m[j][i];
                      m[j][i] = temp;
            }
 
        for (int i = 0; i < n; i++)
           Collections.reverse(Arrays.asList(m[i]));
    }
 
    // Find Minimum rotation to reach the desired matrix
    static void findRotation(int[ ][ ] arr,  int[ ][ ] brr)
    {
 
        if (isEqual(arr, brr) == true) {
            System.out.print("0");
              return;
        }
 
        for (int i = 1; i < 4; i++) {
 
            // Rotate by 90 degrees clockwise
            rotate(arr);
 
            // Checking if both arr[][] and brr[]
            // are now equal or not
            if (!isEqual(arr, brr)) {
                if (i < 4 - i) {
                  System.out.print("+" + i );
                }
                else
                      System.out.print("-" + (4 - i));
                return;
            }
        }
 
        // If converting brr[][] is not possible
        System.out.print("NA");
    }
 
    // Driver Code
    public static void main (String[] args) {
 
        int [ ][ ] arr = { { 2, 3 }, { 4, 5 } };
        int [ ][ ] brr = { { 4, 2 }, { 5, 3 } };
       
        // Function Call
        findRotation(arr, brr);
    }
}
 
// This code is contributed by hrithikgarg03188


Python3
# python3 program for above approach
 
# Function to check if two matrices
# are equal or not
def isEqual(arr, brr):
    n = len(arr)
    m = len(arr[0])
 
    for i in range(0, n):
        for j in range(0, m):
            if (arr[i][j] != brr[i][j]):
                return False
 
    return True
 
# Function to rotate matrix by 90 degrees clockwise
def rotate(m):
    n = len(m)
 
    for i in range(0, n):
        for j in range(0, i):
            temp = m[i][j]
            m[i][j] = m[j][i]
            m[j][i] = temp
 
    for i in range(0, n):
        m[i].reverse()
 
# Find Minimum rotation to reach the desired matrix
def findRotation(arr, brr):
    if (isEqual(arr, brr)):
        print(0)
        return
 
    for i in range(1, 4):
 
        # Rotate by 90 degrees clockwise
        rotate(arr)
 
        # Checking if both arr[][] and brr[]
        # are now equal or not
        if (isEqual(arr, brr)):
            if (i < 4 - i):
                print(f"+{i}")
 
            else:
                print(f"-{4-i}")
            return
 
    # If converting brr[][] is not possible
    print("NA")
 
# Driver Code
if __name__ == "__main__":
 
    arr = [[2, 3], [4, 5]]
    brr = [[4, 2], [5, 3]]
 
    # Function Call
    findRotation(arr, brr)
 
    # This code is contributed by rakeshsahni


Javascript


输出
+1

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