📌  相关文章
📜  所需的最小翻转次数,以便可以从任何其他单元格到达矩阵的最后一个单元格

📅  最后修改于: 2021-05-04 16:02:27             🧑  作者: Mango

给定尺寸为N * M的矩阵arr [] [] ,其中每个单元格均由字符‘R’‘D’组成,但单元格arr [N] [M]包含‘F’“ R”“ D”表示玩家可以分别从当前单元格向右和向下移动。的任务是找到所需的字符的最小数目从“R”翻转到“d”“d”“R”,使得其可能达到精加工细胞即ARR [N] [M]从每个单元格。

例子:

方法:可以通过观察更改以下单元格的每个单元格可以达到终点来解决该问题:

  • 在最后一行中将所有的“ D”更改为“ R”
  • 在最后一列中将所有的“ R”更改为“ D”

请按照以下步骤解决问题:

  1. 初始化一个变量,例如ans ,以存储最少的翻转次数。
  2. i = 0遍历到N – 1并计数包含‘R’的像元arr [i] [M-1]的数量
  3. i = 0遍历到M – 1并计算包含‘D’的像元arr [N – 1] [i]的数量
  4. 打印在上一步中获得的两个计数之和作为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the minimum
// number of flips required
int countChanges(vector > mat)
{
    // Dimensions of mat[][]
    int n = mat.size();
    int m = mat[0].size();
 
    // Initialize answer
    int ans = 0;
 
    // Count all 'D's in the last row
    for (int j = 0; j < m - 1; j++) {
        if (mat[n - 1][j] != 'R')
            ans++;
    }
 
    // Count all 'R's in the last column
    for (int i = 0; i < n - 1; i++) {
        if (mat[i][m - 1] != 'D')
            ans++;
    }
 
    // Print answer
    return ans;
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > arr = { { 'R', 'R', 'R', 'D' },
                                  { 'D', 'D', 'D', 'R' },
                                  { 'R', 'D', 'R', 'F' } };
 
    // Function call
    int cnt = countChanges(arr);
 
    // Print answer
    cout << cnt << endl;
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to calculate the minimum
// number of flips required    
public static int countChanges(char mat[][])
{
     
    // Dimensions of mat[][]
    int n = mat.length;
    int m = mat[0].length;
 
    // Initialize answer
    int ans = 0;
 
    // Count all 'D's in the last row
    for(int j = 0; j < m - 1; j++)
    {
        if (mat[n - 1][j] != 'R')
            ans++;
    }
 
    // Count all 'R's in the last column
    for(int i = 0; i < n - 1; i++)
    {
        if (mat[i][m - 1] != 'D')
            ans++;
    }
     
    // Print answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    char arr[][] = { { 'R', 'R', 'R', 'D' },
                     { 'D', 'D', 'D', 'R' },
                     { 'R', 'D', 'R', 'F' } };
 
    // Function call
    int cnt = countChanges(arr);
 
    // Print answer
    System.out.println(cnt);
}
}
 
// This code is contributed by Manu Pathria


Python3
# Python3 program for the above approach
  
# Function to calculate the minimum
# number of flips required
def countChanges(mat):
     
    # Dimensions of mat[][]
    n = len(mat)
    m = len(mat[0])
  
    # Initialize answer
    ans = 0
  
    # Count all 'D's in the last row
    for j in range(m - 1):
        if (mat[n - 1][j] != 'R'):
            ans += 1
             
    # Count all 'R's in the last column
    for i in range(n - 1):
        if (mat[i][m - 1] != 'D'):
            ans += 1
 
    # Print answer
    return ans
 
# Driver Code
 
# Given matrix
arr = [ [ 'R', 'R', 'R', 'D' ] ,
        [ 'D', 'D', 'D', 'R' ],
        [ 'R', 'D', 'R', 'F' ] ]
  
# Function call
cnt = countChanges(arr)
  
# Print answer   
print(cnt)
 
# This code is contributed by susmitakundugoaldanga


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to calculate the minimum
// number of flips required    
public static int countChanges(char [,]mat)
{
     
    // Dimensions of [,]mat
    int n = mat.GetLength(0);
    int m = mat.GetLength(1);
     
    // Initialize answer
    int ans = 0;
     
    // Count all 'D's in the last row
    for(int j = 0; j < m - 1; j++)
    {
        if (mat[n - 1,j] != 'R')
            ans++;
    }
 
    // Count all 'R's in the last column
    for(int i = 0; i < n - 1; i++)
    {
        if (mat[i,m - 1] != 'D')
            ans++;
    }
     
    // Print answer
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    char [,]arr = { { 'R', 'R', 'R', 'D' },
                    { 'D', 'D', 'D', 'R' },
                    { 'R', 'D', 'R', 'F' } };
 
    // Function call
    int cnt = countChanges(arr);
 
    // Print answer
    Console.WriteLine(cnt);
}
}
 
// This code is contributed by Princi Singh


输出:
2

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