Minimize cost to cover floor using tiles of dimensions 1 * 1 and 1 * 2 | Function to find the minimum cost of flooring with the given tiles ; Store the size of the 2d array ; Stores the minimum cost of flooring ; Traverse the 2d array row - wise ; If the current character is ' * ' , then skip it ; Choose the 1 * 1 tile if j is m - 1 ; If consecutive ' . ' are present , the greedily choose tile with the minimum cost ; Otherwise choose the 1 * 1 tile ; Print the minimum cost ; Driver Code
def minCost ( arr , A , B ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE m = len ( arr [ 0 ] ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = 0 NEW_LINE while j < m : NEW_LINE INDENT if ( arr [ i ] [ j ] == ' * ' ) : NEW_LINE INDENT j += 1 NEW_LINE continue NEW_LINE DEDENT if ( j == m - 1 ) : NEW_LINE INDENT ans += A NEW_LINE DEDENT else : NEW_LINE INDENT if ( arr [ i ] [ j + 1 ] == ' . ' ) : NEW_LINE INDENT ans += min ( 2 * A , B ) NEW_LINE j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += A NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ ' . ' , ' . ' , ' * ' ] , [ ' . ' , ' * ' , ' * ' ] ] NEW_LINE A , B = 2 , 10 NEW_LINE minCost ( arr , A , B ) NEW_LINE DEDENT