Number of days until all chocolates become unhealthy

Pablo has square chocolate Box of size n x n in which a variety of healthy chocolates are present denoted by ‘H’ initially but he finds out that some of the chocolates are rotten and are unhealthy denoted by ‘U’. In one day the rotten chocolates make all its neighbouring chocolates as unhealthy. This goes on and on until all chocolates present in the chocolate box becomes Unhealthy to eat. Find out the number of days in which the whole chocolate box becomes Unhealthy.
(Note : It is guaranteed that atleast one of the chocolate is Unhealthy)

Brute Force Approach:
Initialize a flag = 1. Use a while loop, inside that while search for an H (searching requires O(n^2) time complexity if we are unable to find a H in the 2-D character array stop incrementing the day counter and set flag as 0 to break the loop.

Below is the implementation of above approach:

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP program to find number of days before

// all chocolates become unhealthy.

#include <bits/stdc++.h>

usingnamespacestd;

// Validates out of bounds indexing

boolisValid(inti, intj, intn)

{

if(i < 0 || j < 0 || i >= n || j >= n)

returnfalse;

returntrue;

}

// function for returning number of days

intnumdays(chararr[][4], intn)

{

intnumdays = 0;

while(true)

{

// Traverse matrix to look for unhealthy

// chocolates and mark their neighbors.

for(inti = 0; i < n; i++)

{

for(intj = 0; j < n; j++)

{

if(arr[i][j] == 'U')

{

if(isValid(i - 1, j - 1, n) &&

arr[i - 1][j - 1] == 'H')

arr[i - 1][j - 1] = 'V';

if(isValid(i - 1, j, n) &&

arr[i - 1][j] == 'H')

arr[i - 1][j] = 'V';

if(isValid(i - 1, j + 1, n) &&

arr[i - 1][j + 1] == 'H')

arr[i - 1][j + 1] = 'V';

if(isValid(i, j - 1, n) &&

arr[i][j - 1] == 'H')

arr[i][j - 1] = 'V';

if(isValid(i, j + 1, n) &&

arr[i][j + 1] == 'H')

arr[i][j + 1] = 'V';

if(isValid(i + 1, j - 1, n) &&

arr[i + 1][j - 1] == 'H')

arr[i + 1][j - 1] = 'V';

if(isValid(i + 1, j, n) &&

arr[i + 1][j] == 'H')

arr[i + 1][j] = 'V';

if(isValid(i + 1, j + 1, n) &&

arr[i + 1][j + 1] == 'H')

arr[i + 1][j + 1] = 'V';

}

/*Here we are assigning the neighbours of U

with the character V because we don't want

these neighbours to be counted in that

particular day. If we do not do so, in the

next iteration that neighbour will also get

counted which was supposed to be counted in

the next day. */

}

}

// Mark chocolates unhealthy which are made

// unhealthy in current day.

boolHflag = false;

for(inti = 0; i < n; i++)

{

for(intj = 0; j < n; j++)

{

if(arr[i][j] == 'V')

{

arr[i][j] = 'U';

Hflag = true;

}

}

}

// Check if there was any chocoloate

// marked unhealthy in current day

if(Hflag)

numdays++;

else

break;

}

returnnumdays;

}

// Driver function

intmain()

{

intn = 4;

chararr[4][4] = { 'H', 'H', 'H', 'U',

'H', 'H', 'H', 'H',

'H', 'U', 'H', 'H',

'H', 'H', 'H', 'H'

};

intans = numdays(arr, n);

cout << "number of days taken : "

<< ans << "\n";

return0;

}

chevron_right

filter_none

Output:

number of days taken : 2

Efficient Approach (Uses BFS)
In this approach, declare a queue which inputs pairs which corresponds to the index of the unhealthy chocolates and then as soon as the index (-1, -1) is reached we increment the numdays counter. This Solution is basically based on calculating levels in level order traversal (Iterative version) of a binary tree in which we push the initial indexes of the unhealthy chocolates instead of root node and incrementing numdays instead of level counter as soon as the index (-1, -1) is reached instead of NULL. As soon as the counter of the flag reaches 2 we break the loop denoting that queue has encoutered two consecutive (-1, -1) pair.

Always code as if the guy who ends up maintaining your code will be violent psychopath who knows where you live
visit wwwsahilkhoslacoin

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.