You are given a n x m grid of 0s and 1s and you need to find the maximum number of pairs of adjacent 0s that can be formed if the 0s can be paired either vertically or horizontally. Two zeroes included in one pair cannot be used in another.
Answers
Answered by
7
Answer:
Solution : It is very easy to note that if you reach a position (i,j) in the grid, you must have come from one cell higher, i.e. (i-1,j) or from one cell to your left , i.e. (i,j-1). This means that the cost of visiting cell (i,j) will come from the following recurrence relation:
MinCost(i,j) = min(MinCost(i-1,j),MinCost(i,j-1)) + Cost[i][j]
The above statement means that to reach cell (i,j) wit minimum cost, first reach either cell(i-1,j) or cell (i,j-1) in as minimum cost as possible. From there, jump to cell (i,j). This brings us to the two important conditions which need to be satisfied for a dynamic programming problem:
Similar questions
Business Studies,
6 months ago
Hindi,
6 months ago
Chemistry,
6 months ago
Math,
1 year ago
Physics,
1 year ago
Social Sciences,
1 year ago
Biology,
1 year ago