📜  使用尺寸为 1*1 和 1*2 的瓷砖将覆盖地板的成本降至最低

📅  最后修改于: 2021-10-26 02:39:31             🧑  作者: Mango

给定一个大小为N*M的二维数组arr[][]‘.’组成和‘*’字符代表一个楼层,两个正整数AB 分别代表1*11*2瓷砖的成本,任务是填充所有带有‘.’的字符在地板上铺有1*11*2尺寸的瓷砖,这样可以最大限度地降低填充地板的成本,并且不允许瓷砖旋转。

例子:

方法:可以使用贪心方法解决给定的问题。这个想法是按行遍历给定的二维数组,如果两个连续的‘.’遇到,然后选择放置两个1 * 1 瓷砖或一个1 * 2 瓷砖的成本最低的瓷砖。请按照以下步骤解决问题:

  • 初始化一个变量,比如ans0来存储最小总成本。
  • 使用i作为行索引和j作为列索引逐行遍历给定的二维数组arr[][]并执行以下步骤:
    • 如果arr[i][j] 的值等于‘*’ ,则继续迭代。
    • 否则,请检查以下条件:
      • 如果j 的值为m(M – 1) ,则将A添加到变量ans
      • 否则,如果(arr[i][j + 1]) 的值‘.’ ,然后将值2*AB 中的最小值添加到变量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 现场工作专业课程学生竞争性编程现场课程