📜  制作 3*3 矩阵幻方所需的最小更改

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

制作 3*3 矩阵幻方所需的最小更改

给定一个 3*3 矩阵,找出需要对其进行的最少更改次数,以便将其变成幻方。幻方是一个方阵,其所有行的总和相同,所有列的总和相同,并且两条对角线的总和相同。

例子:

Input :  8 5 6 
         3 8 7 
         4 9 2 
Output : 2
5 in row 1 col 2 should be changed to 1
8 in row 2 col 2 should be changed to 5
Total 2 changes are required.

Input : 8 9 4 
        5 9 3 
        6 1 8 
Output : 3
8 in row 1 col 1 should be changed to 2
5 in row 2 col 1 should be changed to 7
9 in row 2 col 2 should be changed to 5
Total 3 changes are required.

8 个可能的 3*3 维幻方,它们是
8 1 6
3 5 7
4 9 2

6 1 8
7 5 3
2 9 4

4 9 2
3 5 7
8 1 6



2 9 4
7 5 3
6 1 8

8 3 4
1 5 9
6 7 2

4 3 8
9 5 1
2 7 6

6 7 2
1 5 9
8 3 4

2 7 6
9 5 1
4 3 8

我们创建了一个 3D 数组来存储这 8 个矩阵。现在我们检查这 8 个矩阵中的每一个的输入矩阵,并找出可以通过最少数量的变化获得的矩阵。

C++
#include 
using namespace std;
 
// This program takes in two 2D arrays as
// input and compares them to find out the
// minimum number of changes that needs to
// be made to convert arr to ms.
int findMinimumFromMS(int arr[][3], int ms[][3])
{
    int count = 0;
     
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if (arr[i][j] != ms[i][j])
                count++;
        }
    }
    return count;
}
 
int findMinimum(int arr[][3])
{
    int ms[][3][3] = {
        { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } },
        { { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } },
        { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } },
        { { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } },
        { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } },
        { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } },
        { { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } },
        { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } }, };
 
    // If all the elements need to be changed,
    // there would be 9 changes, so we take the
    // max as 9
    int min = 9;
     
    for(int i = 0; i < 8; i++)
    {
        int x = findMinimumFromMS(arr, ms[i]);
        if (x < min)
            min = x;
    }
    return min;
}
 
// Driver code   
int main()
{
    int arr[][3] = { { 8, 5, 6 },
                     { 3, 8, 7 }, 
                     { 4, 9, 2 } };
                      
    cout << findMinimum(arr) << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
class GfG {
 
    // this program takes in two 2D arrays as
    // input and compares them to find out the
    // minimum number of changes that needs to
    // be made to convert arr to ms.
    public static int findMinimumFromMS(int[][] arr,
                                        int[][] ms)
    {
        int count = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (arr[i][j] != ms[i][j])
                    count++;
            }
        }
        return count;
    }
 
    public static int findMinimum(int[][] arr)
    {
        int[][][] ms = {
            { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } },
            { { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } },
            { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } },
            { { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } },
            { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } },
            { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } },
            { { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } },
            { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } },
        };
 
        // If all the elements need to be changed,
        // there would be 9 changes, so we take the
        // max as 9
        int min = 9;
        for (int i = 0; i < 8; i++) {
            int x = findMinimumFromMS(arr, ms[i]);
            if (x < min)
                min = x;
        }
        return min;
    }
 
    public static void main(String[] args)
    {
        int[][] arr = { { 8, 5, 6 }, { 3, 8, 7 },
                                     { 4, 9, 2 } };
        System.out.println(findMinimum(arr));
    }
}


Python3
# This program takes in two 2D arrays as
# input and compares them to find out the
# minimum number of changes that needs to
# be made to convert arr to ms.
def findMinimumFromMS(arr, ms):
     
    count = 0
    for i in range(0, 3):
        for j in range(0, 3):
            if arr[i][j] != ms[i][j]:
                count += 1
     
    return count
 
def findMinimum(arr):
  
    ms = [
            [ [ 8, 1, 6 ], [ 3, 5, 7 ], [ 4, 9, 2 ] ],
            [ [ 6, 1, 8 ], [ 7, 5, 3 ], [ 2, 9, 4 ] ],
            [ [ 4, 9, 2 ], [ 3, 5, 7 ], [ 8, 1, 6 ] ],
            [ [ 2, 9, 4 ], [ 7, 5, 3 ], [ 6, 1, 8 ] ],
            [ [ 8, 3, 4 ], [ 1, 5, 9 ], [ 6, 7, 2 ] ],
            [ [ 4, 3, 8 ], [ 9, 5, 1 ], [ 2, 7, 6 ] ],
            [ [ 6, 7, 2 ], [ 1, 5, 9 ], [ 8, 3, 4 ] ],
            [ [ 2, 7, 6 ], [ 9, 5, 1 ], [ 4, 3, 8 ] ],
        ]
 
    # If all the elements need to be changed, there
    # would be 9 changes, so we take the max as 9
    Min = 9
    for i in range(0, 8):
        x = findMinimumFromMS(arr, ms[i])
        if x < Min:
            Min = x
     
    return Min
 
if __name__ == "__main__":
  
    arr = [ [ 8, 5, 6 ], [ 3, 8, 7 ], [ 4, 9, 2 ] ]
    print(findMinimum(arr))
  
# This code is contributed by Rituraj Jain


C#
using System;
 
class GFG{
     
// This program takes in two 2D arrays as
// input and compares them to find out the
// minimum number of changes that needs to
// be made to convert arr to ms.
static int findMinimumFromMS(int[,] arr, int[,,] ms,
                             int row)
{
    int count = 0;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            if (arr[i, j] != ms[row, i, j])
            {
                count++;
            }
        }
    }
    return count;
}
 
static int findMinimum(int[,] arr)
{
    int[,,] ms = {
        { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } },
        { { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } },
        { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } },
        { { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } },
        { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } },
        { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } },
        { { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } },
        { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } },
    };
     
    // If all the elements need to be changed,
    // there would be 9 changes, so we take the
    // max as 9
    int min = 9;
     
    for(int i = 0; i < 8; i++)
    {
        int x = findMinimumFromMS(arr, ms, i);
         
        if (x < min)
        {
            min = x;
        }
    }
    return min;
}
 
// Driver Code
static public void Main()
{
    int[,] arr = { { 8, 5, 6 },
                   { 3, 8, 7 },
                   { 4, 9, 2 } };
                    
    Console.WriteLine(findMinimum(arr));
}
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:
2

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