📌  相关文章
📜  检查 N*N Grid 的每一行每一列是否包含从 1 到 N 的所有数字

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

检查 N*N Grid 的每一行每一列是否包含从 1 到 N 的所有数字

给定一个大小为N * N的方阵arr[][] ,任务是检查矩阵的每一和每一是否包含从1N的所有数字。

例子

方法:可以使用集合数据结构(集合存储唯一元素)来解决任务。遍历矩阵,将每一和每一的元素存储在一个集合中,并检查集合的大小是否等于N。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether each row and
// column has all the numbers from 1 to N
int check(vector >& arr)
{
    int N = arr.size();
    set row, col;
    int flag = 1;
    for (int i = 0; i < N; i++) {
        row.clear();
        col.clear();
        for (int j = 0; j < N; j++) {
 
            // Inserting the elements
            // to row set and column set
            col.insert(arr[j][i]);
            row.insert(arr[i][j]);
        }
 
        // Checking the size of each
        // row and column and if it is
        // equal or not
        if (col.size() != N
            || row.size() != N)
            flag = 0;
    }
    return flag;
}
 
// Driver Code
int main()
{
    int N = 3;
    vector > arr{ { 1, 2, 3 },
                              { 3, 1, 2 },
                              { 2, 3, 1 } };
    cout << (!check(arr) ? "false" : "true");
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to check whether each row and
  // column has all the numbers from 1 to N
  static int check(int[][] arr)
  {
    int N = arr.length;
    Set row = new HashSet();
    Set col = new HashSet();
 
    int flag = 1;
    for (int i = 0; i < N; i++) {
      row.clear();
      col.clear();
      for (int j = 0; j < N; j++) {
 
        // Inserting the elements
        // to row set and column set
        col.add(arr[j][i]);
        row.add(arr[i][j]);
      }
 
      // Checking the size of each
      // row and column and if it is
      // equal or not
      if (col.size() != N
          || row.size() != N)
        flag = 0;
    }
    return flag;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 3;
    int[][] arr = { { 1, 2, 3 },
                   { 3, 1, 2 },
                   { 2, 3, 1 } };
 
    if(check(arr) == 1){
      System.out.println("true");
    }
    else{
      System.out.println("false");
    }
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python program for the above approach
 
# Function to check whether each row and
# column has all the numbers from 1 to N
def check (arr):
    N = len(arr)
    row = set()
    col = set();
    flag = 1;
    for i in range(N):
        row = set()
        col = set();
        for j in range(N):
 
            # Inserting the elements
            # to row set and column set
            col.add(arr[j][i]);
            row.add(arr[i][j]);
         
        # Checking the size of each
        # row and column and if it is
        # equal or not
        if (len(col) != N or len(row) != N):
            flag = 0;
     
    return flag;
 
# Driver Code
N = 3;
arr = [[1, 2, 3], [3, 1, 2], [2, 3, 1]];
print("false") if not check(arr) else print("true");
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
   
public class GFG{
 
  // Function to check whether each row and
  // column has all the numbers from 1 to N
  static int check(int[,] arr)
  {
    int N = 3;
    HashSet row = new HashSet();
    HashSet col = new HashSet();
 
    int flag = 1;
    for (int i = 0; i < N; i++) {
      row.Clear();
      col.Clear();
      for (int j = 0; j < N; j++) {
 
        // Inserting the elements
        // to row set and column set
        col.Add(arr[j,i]);
        row.Add(arr[i,j]);
      }
 
      // Checking the size of each
      // row and column and if it is
      // equal or not
      if (col.Count != N
          || row.Count != N)
        flag = 0;
    }
    return flag;
  }
 
  // Driver Code
  static public void Main (){
    //int N = 3;
    int[,] arr = { { 1, 2, 3 },
                   { 3, 1, 2 },
                   { 2, 3, 1 } };
 
    if(check(arr) == 1){
      Console.WriteLine("true");
    }
    else{
      Console.WriteLine("false");
    }
  }
}
 
// This code is contributed by Shubham Singh


Javascript



输出
true

时间复杂度:O(N 2 * logN)
辅助空间:O(N)