给定一个维度为N * M的矩阵arr[][] ,其中每个单元格由字符‘R’或‘D’ 组成,除了包含‘F’的单元格arr[N][M] 。 ‘R’和‘D’表示玩家可以从当前单元格分别向右和向下移动。任务是找到从‘R’到‘D’或从‘D’到‘R’翻转所需的最少字符数,以便可以到达完成单元,即arr[N][M]从每个细胞。
例子:
Input: N = 2, M = 3, arr[][] = {{D, D, R}, {R, R, F}}
Output: 1
Explanation: After changing the direction of (1, 3) to ‘D’, each cell can reach the finishing point.
Input: N = 1, M = 3, arr[1][3] = {{D, D, F}}
Output: 2
处理方法:通过观察每个单元格在更改以下单元格后可以到达终点来解决问题:
- 将最后一行中的所有“D”更改为“R” 。
- 将最后一列中的所有“R”更改为“D” 。
请按照以下步骤解决问题:
- 初始化一个变量,比如ans ,以存储最小翻转次数。
- 从i = 0遍历到N – 1并计算包含‘R’的单元格arr[i][M-1]的数量。
- 从i = 0遍历到 M – 1并计算包含‘D’的单元格arr[N – 1][i]的数量。
- 打印在上述步骤中获得的两个计数的总和作为所需答案。
下面是上述方法的实现:
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
Javascript
输出:
2
时间复杂度: O(N*M)
辅助空间:O(N*M)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live