📌  相关文章
📜  检查是否可以创建一个矩阵,使得每行都有 A 1s,每列都有 B 1s

📅  最后修改于: 2021-09-03 13:49:55             🧑  作者: Mango

给定四个整数N、M、A、B ,其中 N 是行数,M 是列数,任务是检查是否可以创建一个维度为N x M的二进制矩阵,使得每一行都有一个数字1s并且每列都有一个B1s 。如果任何这样的矩阵是可能的,则打印它,否则打印“-1”

例子:

方法:我们的想法是观察到,由于每一行都应该正好有 A 1s ,而每列应该正好有 B 1s ,因此所有行A * N 中的 1 数应该等于所有列B * 中的 1 数。因此,所需矩阵存在当且仅当A*N = B*M 。下图为:

  1. 发现任意数量的0 使得(d * N)%M == 0,其中A%BA除以B的余数。
  2. 在所需矩阵的第一行中,在位置[1, A]处插入那些。
  3. 第i,把那些,如我- 1行,但d循环右移。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Number of rows
const int n = 3;
 
// Number of columns
const int m = 6;
 
// Function that prints the matrix
// if it exists
void printMatrix(int arr[][m],
                 string ans)
{
    if (ans == "No")
        cout << "No\n";
    else {
        // Print if matrix exists
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++)
                cout << arr[i][j] << " ";
            cout << '\n';
        }
    }
}
 
// Function to check if it is possible
// to create a matrix such that every
// row has A 1s & every column has B 1s
void createMatrix(int a, int b)
{
    int matrix[n][m], row[n], col[m];
 
    // Initialize all matrix
    // entries equal to 0
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            matrix[i][j] = 0;
        }
    }
 
    // Initialize the number of
    // ones required in every row
    for (int i = 0; i < n; i++)
        row[i] = a;
 
    // Initialize the number of
    // ones required in each column
    for (int i = 0; i < m; i++)
        col[i] = b;
 
    int l = 0, d = 0;
 
    // Check if the total number of
    // ones required in every row is
    // not equal to total number of
    // ones required in every column
    // then print No
    if (n * a != m * b)
        printMatrix(matrix, "No");
 
    else {
 
        for (int i = 0; i < n; i++) {
            int j;
            if (l == m)
                l = 0;
 
            for (j = l; j < m; j++) {
 
                if (row[i] > 0 && col[j] > 0) {
 
                    // Fill a one if there is a
                    // place to be filled
                    matrix[i][j] = 1;
 
                    // Decrease the number of
                    // ones required in ith row
                    row[i]--;
 
                    // Decrease the number of
                    // ones required in jth column
                    col[j]--;
                    d = j;
                }
            }
            l = d + 1;
            if (row[i] != 0) {
 
                for (j = 0; j < m; j++) {
 
                    if (row[i] > 0 && col[j] > 0) {
 
                        // Fill a one if there is
                        // a place to be filled
                        matrix[i][j] = 1;
 
                        // Decrease the number of 1s
                        // required in ith row
                        row[i]--;
                        // Decrease the number of 1s
                        // required in jth column
                        col[j]--;
                        l = j + 1;
                    }
                    // Break the loop if no place
                    // is left for ones to filled
                    if (row[i] == 0)
                        break;
                }
            }
        }
 
        // Function call to print the matrix
        printMatrix(matrix, "Yes");
    }
}
 
// Driver Code
int main()
{
 
    // Number of ones required
    // in every row
    int a = 2;
 
    // Number of ones required
    // in every column
    int b = 1;
 
    // Function call
    createMatrix(a, b);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Number of rows
static int n = 3;
 
// Number of columns
static int m = 6;
 
// Function that prints the matrix
// if it exists
static void printMatrix(int arr[][],
                        String ans)
{
    if (ans == "No")
        System.out.print("No\n");
    else
    {
         
        // Print if matrix exists
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                System.out.print(arr[i][j] + " ");
            System.out.println();
        }
    }
}
 
// Function to check if it is possible
// to create a matrix such that every
// row has A 1s & every column has B 1s
static void createMatrix(int a, int b)
{
    int [][]matrix = new int[n][m];
    int []row = new int[n];
    int []col = new int[m];
 
    // Initialize all matrix
    // entries equal to 0
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            matrix[i][j] = 0;
        }
    }
 
    // Initialize the number of
    // ones required in every row
    for(int i = 0; i < n; i++)
        row[i] = a;
 
    // Initialize the number of
    // ones required in each column
    for(int i = 0; i < m; i++)
        col[i] = b;
 
    int l = 0, d = 0;
 
    // Check if the total number of
    // ones required in every row is
    // not equal to total number of
    // ones required in every column
    // then print No
    if (n * a != m * b)
        printMatrix(matrix, "No");
 
    else
    {
        for(int i = 0; i < n; i++)
        {
            int j;
            if (l == m)
                l = 0;
 
            for(j = l; j < m; j++)
            {
                if (row[i] > 0 && col[j] > 0)
                {
                     
                    // Fill a one if there is a
                    // place to be filled
                    matrix[i][j] = 1;
 
                    // Decrease the number of
                    // ones required in ith row
                    row[i]--;
 
                    // Decrease the number of
                    // ones required in jth column
                    col[j]--;
                    d = j;
                }
            }
            l = d + 1;
            if (row[i] != 0)
            {
                for(j = 0; j < m; j++)
                {
                    if (row[i] > 0 && col[j] > 0)
                    {
                         
                        // Fill a one if there is
                        // a place to be filled
                        matrix[i][j] = 1;
 
                        // Decrease the number of 1s
                        // required in ith row
                        row[i]--;
                        // Decrease the number of 1s
                        // required in jth column
                        col[j]--;
                        l = j + 1;
                    }
                     
                    // Break the loop if no place
                    // is left for ones to filled
                    if (row[i] == 0)
                        break;
                }
            }
        }
 
        // Function call to print the matrix
        printMatrix(matrix, "Yes");
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Number of ones required
    // in every row
    int a = 2;
 
    // Number of ones required
    // in every column
    int b = 1;
 
    // Function call
    createMatrix(a, b);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
 
# Number of rows
n = 3
 
# Number of columns
m = 6
 
# Function that prints the matrix
# if it exists
def printMatrix(arr, ans):
     
    if (ans == "No"):
        print("No")
     
    else:
         
        # Print if matrix exists
        for i in range(n):
            for j in range(m):
                print(arr[i][j], end = " ")
                 
            print()
 
# Function to check if it is possible
# to create a matrix such that every
# row has A 1s & every column has B 1s
def createMatrix(a, b):
     
    matrix = [[0 for i in range(m)]
                 for i in range(n)]
    row = [a for i in range(n)]
    col = [b for i in range(m)]
 
    l = 0
    d = 0
 
    # Check if the total number of
    # ones required in every row is
    # not equal to total number of
    # ones required in every column
    # then prNo
    if (n * a != m * b):
        printMatrix(matrix, "No")
 
    else:
        for i in range(n):
            j = 0
             
            if (l == m):
                l = 0
 
            for j in range(l, m):
                if (row[i] > 0 and col[j] > 0):
 
                    # Fill a one if there is a
                    # place to be filled
                    matrix[i][j] = 1
 
                    # Decrease the number of
                    # ones required in ith row
                    row[i] -= 1
 
                    # Decrease the number of
                    # ones required in jth column
                    col[j] -= 1
                    d = j
 
            l = d + 1
            if (row[i] != 0):
                for j in range(m):
                    if (row[i] > 0 and col[j] > 0):
 
                        # Fill a one if there is
                        # a place to be filled
                        matrix[i][j] = 1
 
                        # Decrease the number of 1s
                        # required in ith row
                        row[i] -= 1
                         
                        # Decrease the number of 1s
                        # required in jth column
                        col[j] -= 1
                        l = j + 1
 
                    # Break the loop if no place
                    # is left for ones to filled
                    if (row[i] == 0):
                        break
 
        # Function call to print matrix
        printMatrix(matrix, "Yes")
 
# Driver Code
if __name__ == '__main__':
 
    # Number of ones required
    # in every row
    a = 2
 
    # Number of ones required
    # in every column
    b = 1
 
    # Function call
    createMatrix(a, b)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
// Number of rows
static int n = 3;
 
// Number of columns
static int m = 6;
 
// Function that prints the matrix
// if it exists
static void printMatrix(int [,]arr,
                        String ans)
{
    if (ans == "No")
        Console.Write("No\n");
    else
    {
         
        // Print if matrix exists
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
                Console.Write(arr[i, j] + " ");
            Console.WriteLine();
        }
    }
}
 
// Function to check if it is possible
// to create a matrix such that every
// row has A 1s & every column has B 1s
static void createMatrix(int a, int b)
{
    int [,]matrix = new int[n, m];
    int []row = new int[n];
    int []col = new int[m];
 
    // Initialize all matrix
    // entries equal to 0
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            matrix[i, j] = 0;
        }
    }
 
    // Initialize the number of
    // ones required in every row
    for(int i = 0; i < n; i++)
        row[i] = a;
 
    // Initialize the number of
    // ones required in each column
    for(int i = 0; i < m; i++)
        col[i] = b;
 
    int l = 0, d = 0;
 
    // Check if the total number of
    // ones required in every row is
    // not equal to total number of
    // ones required in every column
    // then print No
    if (n * a != m * b)
        printMatrix(matrix, "No");
 
    else
    {
        for(int i = 0; i < n; i++)
        {
            int j;
            if (l == m)
                l = 0;
 
            for(j = l; j < m; j++)
            {
                if (row[i] > 0 && col[j] > 0)
                {
                     
                    // Fill a one if there is a
                    // place to be filled
                    matrix[i,j] = 1;
 
                    // Decrease the number of
                    // ones required in ith row
                    row[i]--;
 
                    // Decrease the number of
                    // ones required in jth column
                    col[j]--;
                    d = j;
                }
            }
            l = d + 1;
            if (row[i] != 0)
            {
                for(j = 0; j < m; j++)
                {
                    if (row[i] > 0 && col[j] > 0)
                    {
                         
                        // Fill a one if there is
                        // a place to be filled
                        matrix[i,j] = 1;
 
                        // Decrease the number of 1s
                        // required in ith row
                        row[i]--;
                        // Decrease the number of 1s
                        // required in jth column
                        col[j]--;
                        l = j + 1;
                    }
                     
                    // Break the loop if no place
                    // is left for ones to filled
                    if (row[i] == 0)
                        break;
                }
            }
        }
 
        // Function call to print the matrix
        printMatrix(matrix, "Yes");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
 
    // Number of ones required
    // in every row
    int a = 2;
 
    // Number of ones required
    // in every column
    int b = 1;
 
    // Function call
    createMatrix(a, b);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
1 1 0 0 0 0 
0 0 1 1 0 0 
0 0 0 0 1 1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live