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

📅  最后修改于: 2021-04-17 17:29:36             🧑  作者: Mango

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

例子:

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

  • 使用变量ij[0,M – 1][0,N – X]范围内遍历矩阵以执行水平运算,并执行以下运算:
    • 如果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” 。否则,打印“ False”

下面是上述方法的实现:

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


输出:
Yes

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