Logic is simple.
As explained in the pictures you have to maintain count of the trees which are on fire at a given time (increment them) and take care not to waste time on already burnt trees(solving same problems) or water areas and also forgot not to cross the array bounds.At last traverse the matrix and find the maximum count… that’s it sounds simple!!
Here for your code to get all accepted,use of QUEUE is crucial. Since you start up with a tree and the remaining 8 trees surrounding… remember trees not burnt and is not water area will be pushed back in a queue and a count+=1 is alloted to all 8(if max) of them by maintaining a count array and then process them accordingly by popping it out.
Here’s the link to my solution : http://ide.geeksforgeeks.org/DeYFTW
Hope it helps … Happy Coding
my logic is to traverse matrix row wise and visit unvisited places & update counter at all adjacent places.print counter at last,how was this?