给定一个维度为N * M的二进制矩阵mat[][]和一个长度为N + M – 1的二进制字符串S ,任务是找到使所有最短路径从左上角单元格到右下角的单元格等于给定的字符串S 。
例子:
Input: mat[][] = [[1, 0, 1, 1], [1, 1, 1, 0]], S = “10010”
Output: 3
Explanation:
Step 1: [[1, 0, 1, 1], [1, 1, 1, 0]] -> [[1, 0, 1, 1], [0, 1, 1, 0]]
Step 2: [[1, 0, 1, 1], [0, 1, 1, 0]] -> [[1, 0, 0, 1], [0, 1, 1, 0]]
Step 3: [[1, 0, 0, 1], [0, 1, 1, 0]] -> [[1, 0, 0, 1], [0, 0, 1, 0]]
Once the above steps are performed, every shortest path from the top-left to bottom-right cell are equal to S.
Therefore, the required count is 3.
Input: mat[][] = [[1, 0, 0, 1, 0]], S = “01101”
Output: 5
朴素方法:最简单的方法是递归地在给定矩阵的每个单元格中生成所有可能的翻转,并检查最小翻转的哪个组合生成满足所需条件的矩阵。
时间复杂度: O(2 N * M )
辅助空间: O(N * M)
高效的方法:为了优化上述方法,思想是遍历矩阵并观察如果(i, j)是给定矩阵的当前索引,则该位置将在索引(i + j)处的最短路径字符串中)其中, i ∈ [0, N-1]和j ∈ [0, M-1] 。
请按照以下步骤解决问题:
- 将计数器初始化为0 。
- 遍历矩阵arr[][] 的每个位置。
- 如果给定矩阵中的当前位置是(i, j),则该位置在第(i + j)个索引处的最短路径字符串中。
- 在每个位置,比较arr[i][j]和S[i + j] 。如果发现相等,则继续下一个位置。否则,将计数增加1 。
- 对整个矩阵执行上述步骤后,将计数值打印为所需的最小翻转次数。
下面是上述方法的实现:
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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。