📌  相关文章
📜  最小化所需的翻转,以使从二进制矩阵的左上角到右下角的所有最短路径等于 S

📅  最后修改于: 2021-09-04 08:26:30             🧑  作者: Mango

给定一个维度为N * M的二进制矩阵mat[][]和一个长度为N + M – 1的二进制字符串S ,任务是找到使所有最短路径从左上角单元格右下角的单元格等于给定的字符串S

例子:

朴素方法:最简单的方法是递归地在给定矩阵的每个单元格中生成所有可能的翻转,并检查最小翻转的哪个组合生成满足所需条件的矩阵。
时间复杂度: O(2 N * M )
辅助空间: O(N * M)

高效方法:为了优化上述方法,思想是遍历矩阵并观察如果(i, j)是给定矩阵的当前索引,则该位置将在索引(i + j)处的最短路径字符串中)其中, i ∈ [0, N-1]j ∈ [0, M-1]
请按照以下步骤解决问题:

  1. 将计数器初始化为0
  2. 遍历矩阵arr[][] 的每个位置。
  3. 如果给定矩阵中的当前位置是(i, j),则该位置在第(i + j)索引处的最短路径字符串中。
  4. 在每个位置,比较arr[i][j]S[i + j] 。如果发现相等,则继续下一个位置。否则,将计数增加1
  5. 对整个矩阵执行上述步骤后,将计数值打印为所需的最小翻转次数

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum
// number of flips required
int minFlips(vector >& mat,
             string s)
{
    // Dimensions of matrix
    int N = mat.size();
    int M = mat[0].size();
 
    // Stores the count the flips
    int count = 0;
 
    for (int i = 0; i < N; i++) {
 
        for (int j = 0; j < M; j++) {
 
            // Check if element is same
            // or not
            if (mat[i][j]
                != s[i + j] - '0') {
                count++;
            }
        }
    }
 
    // Return the final count
    return count;
}
 
// Driver Code
int main()
{
    // Given Matrix
    vector > mat
        = { { 1, 0, 1 },
            { 0, 1, 1 },
            { 0, 0, 0 } };
 
    // Given path as a string
    string s = "10001";
 
    // Function Call
    cout << minFlips(mat, s);
 
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
    // Function to count the minimum
    // number of flips required
    static int minFlips(int mat[][],
                        String s)
    {
        // Dimensions of matrix
        int N = mat.length;
        int M = mat[0].length;
 
        // Stores the count the flips
        int count = 0;
 
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                // Check if element is same
                // or not
                if (mat[i][j] !=
                    s.charAt(i + j) - '0')
                {
                    count++;
                }
            }
        }
 
        // Return the final count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Matrix
        int mat[][] = {{1, 0, 1},
                       {0, 1, 1}, {0, 0, 0}};
 
        // Given path as a string
        String s = "10001";
 
        // Function Call
        System.out.print(minFlips(mat, s));
    }
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program for the above approach
 
# Function to count the minimum
# number of flips required
def minFlips(mat, s):
 
    # Dimensions of matrix
    N = len(mat)
    M = len(mat[0])
 
    # Stores the count the flips
    count = 0
 
    for i in range(N):
        for j in range(M):
 
            # Check if element is same
            # or not
            if(mat[i][j] != ord(s[i + j]) -
                            ord('0')):
                count += 1
 
    # Return the final count
    return count
 
# Driver Code
 
# Given Matrix
mat = [ [ 1, 0, 1 ],
        [ 0, 1, 1 ],
        [ 0, 0, 0 ] ]
 
# Given path as a string
s = "10001"
 
# Function call
print(minFlips(mat, s))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the minimum
// number of flips required
static int minFlips(int [,]mat,
                    String s)
{
     
    // Dimensions of matrix
    int N = mat.GetLength(0);
    int M = mat.GetLength(1);
 
    // Stores the count the flips
    int count = 0;
 
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
             
            // Check if element is same
            // or not
            if (mat[i, j] !=
                  s[i + j] - '0')
            {
                count++;
            }
        }
    }
 
    // Return the readonly count
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Matrix
    int [,]mat = { { 1, 0, 1 },
                   { 0, 1, 1 },
                   { 0, 0, 0 } };
 
    // Given path as a string
    String s = "10001";
 
    // Function call
    Console.Write(minFlips(mat, s));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
4

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

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