Minimize cost to cover floor using tiles of dimensions 1*1 and 1*2
Given a 2D array arr[][] of size N*M consisting of ‘.’ and ‘*’ characters representing a floor and two positive integers A and B representing the cost of a 1*1 and 1*2 tile respectively, the task is to fill all the characters having ‘.’ on the floor with the tiles of dimensions 1*1 or 1*2 such that the cost of filling the floor is minimized and rotation of the tiles is not allowed.
Examples:
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
Approach: The given problem can be solved by using the Greedy Approach. The idea is to traverse the given 2D array row-wise, and if two consecutive ‘.’ is encountered, then choose the tile with a minimum cost of placing two 1 * 1 tiles or one 1 * 2 tile. Follow the steps below to solve the problem:
- Initialize a variable, say ans as 0 to store the minimum total cost.
- Traverse the given 2D array arr[][] row-wise using i for row-index and j for column index and perform the following steps:
- If the value of arr[i][j] is equal to ‘*’, then continue the iteration.
- Otherwise, check for the following conditions:
- If the value of j is m(M – 1), then add A to the variable ans.
- Otherwise, if the value of (arr[i][j + 1]) is ‘.’, then add the minimum of the values 2*A and B to the variable ans.
- In all other cases, add the value of A to the variable ans.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minCost(vector<vector< char > > arr,
int A, int B)
{
int n = arr.size();
int m = arr[0].size();
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (arr[i][j] == '*' )
continue ;
if (j == m - 1)
ans += A;
else {
if (arr[i][j + 1] == '.' ) {
ans += min(2 * A, B);
j++;
}
else
ans += A;
}
}
}
cout << ans;
}
int main()
{
vector<vector< char > > arr = { { '.' , '.' , '*' },
{ '.' , '*' , '*' } };
int A = 2, B = 10;
minCost(arr, A, B);
return 0;
}
|
Java
import java.util.*;
public class MyClass
{
static void minCost( char arr[][], int A, int B)
{
int n = arr.length;
int m = arr[ 0 ].length;
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (arr[i][j] == '*' )
continue ;
if (j == m - 1 )
ans += A;
else {
if (arr[i][j + 1 ] == '.' ) {
ans += Math.min( 2 * A, B);
j++;
}
else
ans += A;
}
}
}
System.out.println(ans);
}
public static void main(String args[])
{
char [][]arr = { { '.' , '.' , '*' },
{ '.' , '*' , '*' } };
int A = 2 , B = 10 ;
minCost(arr, A, B);
}
}
|
Python3
def minCost(arr, A, B):
n = len (arr)
m = len (arr[ 0 ])
ans = 0
for i in range (n):
j = 0
while j < m:
if (arr[i][j] = = '*' ):
j + = 1
continue
if (j = = m - 1 ):
ans + = A
else :
if (arr[i][j + 1 ] = = '.' ):
ans + = min ( 2 * A, B)
j + = 1
else :
ans + = A
j + = 1
print (ans)
if __name__ = = '__main__' :
arr = [ [ '.' , '.' , '*' ],
[ '.' , '*' , '*' ] ]
A, B = 2 , 10
minCost(arr, A, B)
|
C#
using System;
public class GFG{
static void minCost( char [,] arr, int A, int B)
{
int n = arr.GetLength(0);
int m = arr.GetLength(1);
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (arr[i,j] == '*' )
continue ;
if (j == m - 1)
ans += A;
else {
if (arr[i,j + 1] == '.' ) {
ans += Math.Min(2 * A, B);
j++;
}
else
ans += A;
}
}
}
Console.WriteLine(ans);
}
static public void Main ()
{
char [,] arr = { { '.' , '.' , '*' },
{ '.' , '*' , '*' } };
int A = 2, B = 10;
minCost(arr, A, B);
}
}
|
Javascript
<script>
function minCost(arr, A, B)
{
let n = arr.length;
let m = arr[0].length;
let ans = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (arr[i][j] == '*' )
continue ;
if (j == m - 1)
ans += A;
else {
if (arr[i][j + 1] == '.' ) {
ans += Math.min(2 * A, B);
j++;
}
else
ans += A;
}
}
}
document.write(ans);
}
var arr = [[ '.' , '.' , '*' ],
[ '.' , '*' , '*' ]];
let A = 2, B = 10;
minCost(arr, A, B);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Last Updated :
05 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...