给定一个大小为N*M的二维数组arr[][]由‘.’组成和‘*’字符代表一个楼层,两个正整数A和B 分别代表1*1和1*2瓷砖的成本,任务是填充所有带有‘.’的字符。在地板上铺有1*1或1*2尺寸的瓷砖,这样可以最大限度地降低填充地板的成本,并且不允许瓷砖旋转。
例子:
Input: A = 2, B = 10, arr[][] = {{‘.’, ‘.’, ‘*’}, {‘.’, ‘*’, ‘*’}}
Output: 6
Explanation:
Cover arr[0][0] with 1*1 tile, arr[0][1] with 1*1 tile and arr[1][0] with 1*1 tile.
Therefore, the minimum cost is 2 + 2 +2 = 6.
Input: A = 2, B = 6, arr[][] = {{‘.’, ‘.’, ‘.’}, {‘*’, ‘*’, ‘.’}, {‘.’, ‘.’, ‘*’}}
Output: 12
方法:可以使用贪心方法解决给定的问题。这个想法是按行遍历给定的二维数组,如果两个连续的‘.’遇到,然后选择放置两个1 * 1 瓷砖或一个1 * 2 瓷砖的成本最低的瓷砖。请按照以下步骤解决问题:
- 初始化一个变量,比如ans为0来存储最小总成本。
- 使用i作为行索引和j作为列索引逐行遍历给定的二维数组arr[][]并执行以下步骤:
- 如果arr[i][j] 的值等于‘*’ ,则继续迭代。
- 否则,请检查以下条件:
- 如果j 的值为m(M – 1) ,则将A添加到变量ans 。
- 否则,如果(arr[i][j + 1]) 的值是‘.’ ,然后将值2*A和B 中的最小值添加到变量ans 。
- 在所有其他情况下,将A的值添加到变量ans 。
- 完成以上步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
// of flooring with the given tiles
void minCost(vector > arr,
int A, int B)
{
// Store the size of the 2d array
int n = arr.size();
int m = arr[0].size();
// Stores the minimum cost of
// flooring
int ans = 0;
// Traverse the 2d array row-wise
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// If the current character
// is '*', then skip it
if (arr[i][j] == '*')
continue;
// Choose the 1*1 tile if
// j is m-1
if (j == m - 1)
ans += A;
// If consecutive '.' are
// present, the greedily
// choose tile with the
// minimum cost
else {
if (arr[i][j + 1] == '.') {
ans += min(2 * A, B);
j++;
}
// Otherwise choose
// the 1*1 tile
else
ans += A;
}
}
}
// Print the minimum cost
cout << ans;
}
// Driver Code
int main()
{
vector > arr = { { '.', '.', '*' },
{ '.', '*', '*' } };
int A = 2, B = 10;
minCost(arr, A, B);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class MyClass
{
// Function to find the minimum cost
// of flooring with the given tiles
static void minCost(char arr[][], int A, int B)
{
// Store the size of the 2d array
int n = arr.length;
int m = arr[0].length;
// Stores the minimum cost of
// flooring
int ans = 0;
// Traverse the 2d array row-wise
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// If the current character
// is '*', then skip it
if (arr[i][j] == '*')
continue;
// Choose the 1*1 tile if
// j is m-1
if (j == m - 1)
ans += A;
// If consecutive '.' are
// present, the greedily
// choose tile with the
// minimum cost
else {
if (arr[i][j + 1] == '.') {
ans += Math.min(2 * A, B);
j++;
}
// Otherwise choose
// the 1*1 tile
else
ans += A;
}
}
}
// Print the minimum cost
System.out.println(ans);
}
// Driver Code
public static void main(String args[])
{
char [][]arr = { { '.', '.', '*' },
{ '.', '*', '*' } };
int A = 2, B = 10;
minCost(arr, A, B);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to find the minimum cost
# of flooring with the given tiles
def minCost(arr, A, B):
# Store the size of the 2d array
n = len(arr)
m = len(arr[0])
# Stores the minimum cost of
# flooring
ans = 0
# Traverse the 2d array row-wise
for i in range(n):
j = 0
while j < m:
# If the current character
# is '*', then skip it
if (arr[i][j] == '*'):
j += 1
continue
# Choose the 1*1 tile if
# j is m-1
if (j == m - 1):
ans += A
# If consecutive '.' are
# present, the greedily
# choose tile with the
# minimum cost
else:
if (arr[i][j + 1] == '.'):
ans += min(2 * A, B)
j += 1
# Otherwise choose
# the 1*1 tile
else:
ans += A
j += 1
# Print the minimum cost
print (ans)
# Driver Code
if __name__ == '__main__':
arr = [ [ '.', '.', '*' ],
[ '.', '*', '*' ] ]
A, B = 2, 10
minCost(arr, A, B)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find the minimum cost
// of flooring with the given tiles
static void minCost(char[,] arr, int A, int B)
{
// Store the size of the 2d array
int n = arr.GetLength(0);
int m = arr.GetLength(1);
// Stores the minimum cost of
// flooring
int ans = 0;
// Traverse the 2d array row-wise
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// If the current character
// is '*', then skip it
if (arr[i,j] == '*')
continue;
// Choose the 1*1 tile if
// j is m-1
if (j == m - 1)
ans += A;
// If consecutive '.' are
// present, the greedily
// choose tile with the
// minimum cost
else {
if (arr[i,j + 1] == '.') {
ans += Math.Min(2 * A, B);
j++;
}
// Otherwise choose
// the 1*1 tile
else
ans += A;
}
}
}
// Print the minimum cost
Console.WriteLine(ans);
}
// Driver Code
static public void Main ()
{
char[,] arr = { { '.', '.', '*' },
{ '.', '*', '*' } };
int A = 2, B = 10;
minCost(arr, A, B);
}
}
// This code is contributed by patel2127.
Javascript
输出:
6
时间复杂度: O(N * M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。