📜  检查矩阵是否为拉丁方

📅  最后修改于: 2021-05-17 21:33:29             🧑  作者: Mango

给定大小为N x N的方阵,任务是检查它是否为拉丁方。

例子:

Input: 1 2 3 4
       2 1 4 3
       3 4 1 2
       4 3 2 1
Output: YES

Input: 2 2 2 2
       2 3 2 3
       2 2 2 3
       2 2 2 2
Output: NO



天真的方法:

  1. 对于每个元素,我们首先通过遍历给定行和给定列的所有元素来检查给定元素在给定行和给定列中是否已经存在。
  2. 如果不是,则检查该值是否小于或等于N,如果是,则移动到下一个元素。
  3. 如果以上任何点为假,则矩阵不是拉丁方。

高效的方法:这是在C++中使用Set数据结构的更高效的方法:

  1. 为每一行和每一列定义集合,并创建两个集合数组,一个集合用于所有行,另一个集合用于列。
  2. 遍历所有元素,并将给定元素的值插入相应的行集中和相应的列集中。
  3. 另外,检查给定值是否小于N。如果不是,请打印“否”并返回。
  4. 现在,遍历所有行集和列集,并检查集的大小是否小于N。
  5. 如果是,则打印“是”。否则,打印“否”。

下面是上述方法的实现。

C++
// C++ program to check if given matrix
// is natural latin square or not
 
#include 
using namespace std;
 
void CheckLatinSquare(int mat[4][4])
{
    // Size of square matrix is NxN
    int N = sizeof(mat[0]) / sizeof(mat[0][0]);
 
    // Vector of N sets corresponding
    // to each row.
    vector > rows(N);
 
    // Vector of N sets corresponding
    // to each column.
    vector > cols(N);
 
    // Number of invalid elements
    int invalid = 0;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            rows[i].insert(mat[i][j]);
            cols[j].insert(mat[i][j]);
 
            if (mat[i][j] > N || mat[i][j] <= 0) {
                invalid++;
            }
        }
    }
    // Number of rows with
    // repeatative elements.
    int numrows = 0;
 
    // Number of columns with
    // repeatative elements.
    int numcols = 0;
 
    // Checking size of every row
    // and column
    for (int i = 0; i < N; i++) {
        if (rows[i].size() != N) {
            numrows++;
        }
        if (cols[i].size() != N) {
            numcols++;
        }
    }
 
    if (numcols == 0 && numrows == 0
        && invalid == 0)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
 
    return;
}
 
// Driver code
int main()
{
 
    int Matrix[4][4] = { { 1, 2, 3, 4 },
                         { 2, 1, 4, 3 },
                         { 3, 4, 1, 2 },
                         { 4, 3, 2, 1 } };
 
    // Funtion call
    CheckLatinSquare(Matrix);
 
    return 0;
}


Java
// Java program to check if given matrix
// is natural latin square or not
import java.util.*;
 
class GFG{
     
@SuppressWarnings("unchecked")
static void CheckLatinSquare(int mat[][])
{
     
    // Size of square matrix is NxN
    int N = mat.length;
     
    // Vector of N sets corresponding
    // to each row.
    HashSet[] rows = new HashSet[N];
     
    // Vector of N sets corresponding
    // to each column.
    HashSet[] cols = new HashSet[N];
     
    for(int i = 0; i < N; i++)
    {
        rows[i] = new HashSet();
        cols[i] = new HashSet();
    }
     
    // Number of invalid elements
    int invalid = 0;
     
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
            rows[i].add(mat[i][j]);
            cols[j].add(mat[i][j]);
     
            if (mat[i][j] > N || mat[i][j] <= 0)
            {
                invalid++;
            }
        }
    }
     
    // Number of rows with
    // repeatative elements.
    int numrows = 0;
     
    // Number of columns with
    // repeatative elements.
    int numcols = 0;
     
    // Checking size of every row
    // and column
    for(int i = 0; i < N; i++)
    {
        if (rows[i].size() != N)
        {
            numrows++;
        }
        if (cols[i].size() != N)
        {
            numcols++;
        }
    }
     
    if (numcols == 0 &&
        numrows == 0 && invalid == 0)
        System.out.print("YES" + "\n");
    else
        System.out.print("NO" + "\n");
     
    return;
}
     
// Driver code
public static void main(String[] args)
{
     
    int Matrix[][] = { { 1, 2, 3, 4 },
                       { 2, 1, 4, 3 },
                       { 3, 4, 1, 2 },
                       { 4, 3, 2, 1 } };
     
    // Funtion call
    CheckLatinSquare(Matrix);
}
}
 
// This code is contributed by 29AjayKumar


C#
// C# program to check if given matrix
// is natural latin square or not
using System;
using System.Collections.Generic;
class GFG{
    static void CheckLatinSquare(int[, ] mat)
    {
 
        // Size of square matrix is NxN
        int N = mat.GetLength(0);
 
        // List of N sets corresponding
        // to each row.
        HashSet[] rows = new HashSet[ N ];
 
        // List of N sets corresponding
        // to each column.
        HashSet[] cols = new HashSet[ N ];
 
        for (int i = 0; i < N; i++)
        {
            rows[i] = new HashSet();
            cols[i] = new HashSet();
        }
 
        // Number of invalid elements
        int invalid = 0;
 
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                rows[i].Add(mat[i, j]);
                cols[j].Add(mat[i, j]);
 
                if (mat[i, j] > N || mat[i, j] <= 0)
                {
                    invalid++;
                }
            }
        }
 
        // Number of rows with
        // repeatative elements.
        int numrows = 0;
 
        // Number of columns with
        // repeatative elements.
        int numcols = 0;
 
        // Checking size of every row
        // and column
        for (int i = 0; i < N; i++)
        {
            if (rows[i].Count != N)
            {
                numrows++;
            }
            if (cols[i].Count != N)
            {
                numcols++;
            }
        }
       
        if (numcols == 0 && numrows == 0 && invalid == 0)
            Console.Write("YES" + "\n");
        else
            Console.Write("NO" + "\n");
        return;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[, ] Matrix = {{1, 2, 3, 4},
                          {2, 1, 4, 3},
                          {3, 4, 1, 2},
                          {4, 3, 2, 1}};
 
        // Funtion call
        CheckLatinSquare(Matrix);
    }
}
 
// This code is contributed by Amit Katiyar


输出:
YES