Skip to content

Commit 3c2597f

Browse files
committed
Added cost of tiling
1 parent ede7c4d commit 3c2597f

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

algorithms/greedy/cost_of_tiles.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Find the min cost of tiles to cover a floor.
3+
Floor is represented by 2D array where -
4+
* = tile already placed
5+
. = no tile
6+
7+
tiles available are 1*1 and 1*2 and their costs
8+
are A and B
9+
10+
Source - https://www.geeksforgeeks.org/minimize-cost-to-cover-floor-using-tiles-of-dimensions-11-and-12/
11+
"""
12+
13+
def cost(arr, A, B):
14+
n = len(arr)
15+
m = len(arr[0])
16+
17+
ans = 0
18+
19+
for i in range(n):
20+
j = 0
21+
22+
while j < m:
23+
if arr[i][j] == '*': # tile is already there
24+
j += 1
25+
continue
26+
27+
if j == m - 1: # if j is pointing to last tile, you can use only 1*1 tile
28+
ans += A
29+
else:
30+
if arr[i][j+1] == '.':
31+
ans += min(2 * A, B)
32+
j += 1
33+
else:
34+
ans += A
35+
36+
j += 1
37+
38+
print('Cost of tiling is - ', ans)
39+
40+
arr = [ [ '.', '.', '*' ],
41+
[ '.', '*', '*' ] ]
42+
43+
A, B = 2, 10
44+
45+
cost(arr, A, B)

0 commit comments

Comments
 (0)