📌  相关文章
📜  检查矩阵是否可以通过向行或列中的 X 个连续元素重复添加任何值来转换为另一个矩阵

📅  最后修改于: 2021-09-07 02:02:31             🧑  作者: Mango

给定两个大小为M × N 的矩阵A[][]B[][]以及一个整数X ,任务是检查是否可以通过添加将矩阵A[][]转换为矩阵B[][]任意次数(可能为零)在同一行或同一列中的X个连续单元格的任何值。

例子:

方法:这个问题可以通过先执行所有水平操作然后执行垂直操作来贪婪地解决。
请按照以下步骤解决问题:

  • 遍历矩阵以执行水平操作,使用范围[0, M – 1][0, N – X] 上的变量ij ,并执行以下操作:
    • 如果A[i][j]不等于B[i][j] ,则将同一行中的下X 个元素增加A[i][j] – B[i][j]
  • 现在,遍历矩阵以执行垂直操作,使用范围[0, M – X][0, N – 1] 上的变量ij 并执行以下操作:
    • 检查A[i][j]是否等于B[i][j]
    • 如果发现为假,则将同一列中的下X 个元素增加A[i][j] – B[i][j]
  • 如果矩阵 A[][] 和 B[][] 相等,则打印“True” 。否则,打印“假”

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
bool Check(int A[][2], int B[][2],
           int M, int N, int X)
{
    // Traverse the matrix to perform
    // horizontal operations
    for (int i = 0; i < M; i++) {
        for (int j = 0; j <= N - X; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
 
                for (int k = 0; k < X; k++) {
 
                    // Update next X elements
                    A[i][j + k] = A[i][j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for (int i = 0; i <= M - X; i++) {
        for (int j = 0; j < N; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
                for (int k = 0; k < X; k++) {
 
                    // Update next K elements
                    A[i + k][j] = A[i + k][j] + diff;
                }
            }
        }
    }
 
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
 
            // A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j]) {
 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
int main()
{
    // Input
    int M = 2, N = 2, X = 2;
    int A[2][2] = { { 0, 0 }, { 0, 0 } };
    int B[2][2] = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
static int Check(int A[][], int B[][],
                 int M, int N, int X)
{
     
    // Traverse the matrix to perform
    // horizontal operations
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j <= N - X; j++)
        {
            if (A[i][j] != B[i][j])
            {
                 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
 
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next X elements
                    A[i][j + k] = A[i][j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for(int i = 0; i <= M - X; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (A[i][j] != B[i][j])
            {
                 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next K elements
                    A[i + k][j] = A[i + k][j] + diff;
                }
            }
        }
    }
 
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j])
            {
                 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int M = 2, N = 2, X = 2;
    int A[][] = { { 0, 0 }, { 0, 0 } };
    int B[][] = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X) != 0)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to check whether Matrix A[][]
# can be transformed to Matrix B[][] or not
def Check(A, B, M, N, X):
     
    # Traverse the matrix to perform
    # horizontal operations
    for i in range(M):
        for j in range(N - X + 1):
            if (A[i][j] != B[i][j]):
 
                # Calculate difference
                diff = B[i][j] - A[i][j]
 
                for k in range(X):
                     
                    # Update next X elements
                    A[i][j + k] = A[i][j + k] + diff
 
    # Traverse the matrix to perform
    # vertical operations
    for i in range(M - X + 1):
        for j in range(N):
            if (A[i][j] != B[i][j]):
 
                # Calculate difference
                diff = B[i][j] - A[i][j]
                for k in range(X):
                     
                    # Update next K elements
                    A[i + k][j] = A[i + k][j] + diff
 
    for i in range(M):
        for j in range(N):
             
            # A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j]):
                 
                # Conversion is not possible
                return 0
 
    # Conversion is possible
    return 1
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    M, N, X = 2, 2, 2
    A = [ [ 0, 0 ], [ 0, 0 ] ]
    B = [ [ 1, 2 ], [ 0, 1 ] ]
 
    if (Check(A, B, M, N, X)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
static int Check(int [,]A, int [,]B,
                 int M, int N, int X)
{
     
    // Traverse the matrix to perform
    // horizontal operations
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j <= N - X; j++)
        {
            if (A[i, j] != B[i, j])
            {
                 
                // Calculate difference
                int diff = B[i, j] - A[i, j];
 
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next X elements
                    A[i, j + k] = A[i, j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for(int i = 0; i <= M - X; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (A[i, j] != B[i, j])
            {
                 
                // Calculate difference
                int diff = B[i,j] - A[i,j];
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next K elements
                    A[i + k, j] = A[i + k, j] + diff;
                }
            }
        }
    }
 
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // A[i][j] is not equal to B[i][j]
            if (A[i, j] != B[i, j])
            {
                 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
public static void Main()
{
     
    // Input
    int M = 2, N = 2, X = 2;
    int [,]A = { { 0, 0 }, { 0, 0 } };
    int [,]B = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X) == 1)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
Yes

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

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