Leetcode problem BFS

Problem link>> https://leetcode.com/problems/as-far-from-land-as-possible/

My solution is giving TLE on one last test case can u help me to optimize my solution
my code >> https://ide.codingblocks.com/s/486434
I know can use DP but most of the people did it with just BFS so I thought if something is wrong in this ??

hello @joshisanjay8467

try using unorder map our 2d matrix for visited cell.

also read about multisource bfs. this problem can be easily solved using that

Floyd warshall multisource bfs ??

no its almost similar to bfs

okay I will read it and I guess issue with my code is that I am always running a new BFS from each cell ? Isnt’t it ?

yeah…thats the issue

okay bhaiya thanks for the help I will try to solve it using multisource

bhaiya I used little dp but don’t know getting absurd result

memset is used only if value used for initilisation is -1 or 0

okay got to learn something new thankyou !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

bhaiya I have did this ques. using multisource BFS thanks for suggesting but now I have a small confusion . In bfs source is from which I have to find the shortest path to target so here if I make 0 as source node bcz from them I want to find land but gets wrong answer . Can u plz fix this confusion of mine

my updated code if I run from 0 as source nodes >>
https://ide.codingblocks.com/s/486678

u have to make bfs call from 1 (land only).

becuase we know {i,j} is 1 then nearest land distance is 0.this doesnot holds if we expand from water

yes bhaiya got that !!