📜  检查是否可以使给定的矩阵增加矩阵

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

检查是否可以使给定的矩阵增加矩阵

给定一个由正整数组成的 NXM 矩阵,任务是找出是否可以使矩阵增加。打印构造的矩阵,否则打印 -1。矩阵元素应大于零。
如果满足以下条件,则称矩阵为递增矩阵:-

  • 对于每一行,元素按递增顺序排列。
  • 对于每一列,元素按递增顺序排列。

例子:

注意:一个矩阵可以有多个解决方案。

方法:让 dp[i][j] 表示矩阵 dp 的第 i 行和第 j 列的元素。由于矩阵是非递减的,因此应满足以下两个条件:



  • dp[i][j] >= dp[i][j-1],第 i 行元素不减。
  • dp[i][j] >= dp[i-1][j],第 j 列的元素是非递减的。

这意味着 dp[i][j] >= dp[r] 对于每 1 <= r <= i, 1 <= c <= j(一个元素大于左侧的所有元素)。
让 i 是包含 -1 的 dp 的第一行,在这一行中让 j 是最左边的 -1 的列。用最小的可能值替换 dp[i][j] 总是很方便的,否则,可能无法为右下角的另一个 -1 找到有效值。因此,一种可能的解决方案(也是字典序最小的)是设置 dp[i][j] = max { dp[i][j-1], dp[i-1][j] }。
在填充了 dp 中的一些未知位置后,可能会发现 dp 的值之一小于最左边的一些元素。在这种情况下,没有解决方案。

C++
// CPP program to Check if we can make
// the given matrix increasing matrix or not
#include 
using namespace std;
#define n 4
#define m 4
 
// Function to find increasing matrix
void findIncreasingMatrix(int dp[n + 1][m + 1])
{
    bool flag = false;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
 
            // Putting max into b as per the above approach
            int b = max(dp[i - 1][j], dp[i][j - 1]);
 
            // If b is -1 than putting 1 to it
            b = max(1, b);
 
            // If dp[i][j] has to be filled with max
            if (dp[i][j] == -1)
                dp[i][j] = b;
 
            // If dp[i][j] is less than from it's left
            // element or from it's upper element
            else if (dp[i][j] < b)
                flag = true;
        }
    }
 
    // If it is not possible
    if (flag)
        cout << -1 << '\n';
 
    else {
 
        // Printing the increasing matrix
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cout << dp[i][j] << ' ';
            }
            cout << endl;
        }
    }
}
 
// Drivers code
int main()
{
    /* Here the matrix is 1 2 3 3
                          1 -1 7 -1
                          6 -1 -1 -1
                          -1 -1 -1 -1
       Putting 0 in first row & column */
 
    int dp[n + 1][m + 1] = { { 0, 0, 0, 0, 0 },
                             { 0, 1, 2, 2, 3 },
                             { 0, 1, -1, 7, -1 },
                             { 0, 6, -1, -1, -1 },
                             { 0, -1, -1, -1, -1 } };
 
    findIncreasingMatrix(dp);
}


Java
// Java program to Check if we
// can make the given matrix
// increasing matrix or not
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
static final int n = 4;
static final int m = 4;
 
// Function to find increasing matrix
static void findIncreasingMatrix(int dp[][])
{
    boolean flag = false;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
 
            // Putting max into b as per
            // the above approach
            int b = Math.max(dp[i - 1][j],
                             dp[i][j - 1]);
 
            // If b is -1 than putting 1 to it
            b = Math.max(1, b);
 
            // If dp[i][j] has to be
            // filled with max
            if (dp[i][j] == -1)
                dp[i][j] = b;
 
            // If dp[i][j] is less than from
            // it's left element or from
            // it's upper element
            else if (dp[i][j] < b)
                flag = true;
        }
    }
 
    // If it is not possible
    if (flag == true)
        System.out.println("-1");
 
    else
    {
 
        // Printing the increasing matrix
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= m; ++j)
            {
                System.out.print(dp[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
// Driver code
public static void main(String args[])
{
    /* Here the matrix is 1 2 3 3
                        1 -1 7 -1
                        6 -1 -1 -1
                        -1 -1 -1 -1
    Putting 0 in first row & column */
 
    int dp[][] = {{ 0, 0, 0, 0, 0 },
                  { 0, 1, 2, 2, 3 },
                  { 0, 1, -1, 7, -1 },
                  { 0, 6, -1, -1, -1 },
                  { 0, -1, -1, -1, -1 }};
 
    findIncreasingMatrix(dp);
}
}
 
// This code is contributed
// by Subhadeep


Python3
# Python3 program to Check if we can make
# the given matrix increasing matrix or not
 
# Function to find increasing matrix
def findIncreasingMatrix(dp):
 
    flag = False
    for i in range(1, n + 1):
        for j in range(1, m + 1):
 
            # Putting max into b as per
            # the above approach
            b = max(dp[i - 1][j], dp[i][j - 1])
 
            # If b is -1 than putting 1 to it
            b = max(1, b)
 
            # If dp[i][j] has to be filled with max
            if dp[i][j] == -1:
                dp[i][j] = b
 
            # If dp[i][j] is less than from it's left
            # element or from it's upper element
            elif dp[i][j] < b:
                flag = True
 
    # If it is not possible
    if flag:
        print(-1)
 
    else:
        # Printing the increasing matrix
        for i in range(1, n + 1):
            for j in range(1, m + 1):
                print(dp[i][j], end = ' ')
             
            print()
 
# Driver code
if __name__ == "__main__":
 
    dp = [[0, 0, 0, 0, 0],
          [0, 1, 2, 2, 3],
          [0, 1, -1, 7, -1],
          [0, 6, -1, -1, -1],
          [0, -1, -1, -1, -1]]
    n = m = 4
 
    findIncreasingMatrix(dp)
 
# This code is contributed
# by Rituraj Jain


C#
// C# program to Check if we
// can make the given matrix
// increasing matrix or not
using System;
 
class GFG
{
     
static readonly int n = 4;
static readonly int m = 4;
 
// Function to find increasing matrix
static void findIncreasingMatrix(int [,]dp)
{
    bool flag = false;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
 
            // Putting max into b as per
            // the above approach
            int b = Math.Max(dp[i - 1, j],
                            dp[i, j - 1]);
 
            // If b is -1 than putting 1 to it
            b = Math.Max(1, b);
 
            // If dp[i,j] has to be
            // filled with max
            if (dp[i, j] == -1)
                dp[i, j] = b;
 
            // If dp[i,j] is less than from
            // it's left element or from
            // it's upper element
            else if (dp[i, j] < b)
                flag = true;
        }
    }
 
    // If it is not possible
    if (flag == true)
        Console.WriteLine("-1");
 
    else
    {
 
        // Printing the increasing matrix
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= m; ++j)
            {
                Console.Write(dp[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}
 
// Driver code
public static void Main()
{
    /* Here the matrix is 1 2 3 3
                        1 -1 7 -1
                        6 -1 -1 -1
                        -1 -1 -1 -1
    Putting 0 in first row & column */
 
    int [,]dp = {{ 0, 0, 0, 0, 0 },
                { 0, 1, 2, 2, 3 },
                { 0, 1, -1, 7, -1 },
                { 0, 6, -1, -1, -1 },
                { 0, -1, -1, -1, -1 }};
 
    findIncreasingMatrix(dp);
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:
1 2 2 3 
1 2 7 7 
6 6 7 7 
6 6 7 7

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