My solution was to begin from the cell whose height is maximum and finding all the cells which can be visited by using simple dfs from that cell, and continue this process until all the cells are visited.
Thoughts behind this greedy approach : You cannot visit the cell with max height from any other cell whose height is less than it, and can be visited only by using cells with same height so in order to visit this cell you can start from any cell with same max height and do this process continuously.
Hi guys, I know this might be a silly question but my submission gets TLE and I cant figure out why…I have implemented BFS in C++ , almost identical to the Editorialist’s solution. Can anyone help me? Code here …
Thanks in advance!
My implementation is much easier but complexity is more. Still got AC in 0.12s.
I have made a structure having height, row and col for each cell. Then I have sorted vector of structures according to height(decreasing order) to get the cell which has max height first. Then if the cell is not previously visited then I have applied DFS on that cell.
Can some one tell me what is the problem here. This code works fine with the testcases but give a NZEC when I submit it. I used a DFS in java.Here’s my code: https://www.codechef.com/viewsolution/16348375
in Editorialist’s solution why has been two array dx[4] = {-1, 1, 0, 0} and dy[4] = {0, 0, -1, 1} used and sort(vals.rbegin(), vals.rend()) sorts which part of the vector?
can anyone plzz tell me in order to solve this question i have to learn KOSARAJU’S ALGO or not , i know nothing about SCC and very weak graph thory(i.e BFS and DFS)
The runtime error is due to stack overflow. Apparently the stack size parameter for Java has been reduced, I’m not sure why
Here is a submission I made just now with a simple recursive function upto depth 105: 16323256. It gives RE.
And here is another submission I made in January of this year where recursion goes upto 106 without any error: 12408377.
you first mark vertices as visited and then push them into stack, this is important when there are 2-length cycle in graphs. See, my exact code for details.
“dx” and “dy” array together form the possible adjacent vertices in grid while “sort(vals.rbegin(), vals.rend())” sorts the given array in descending order.
@coder_ishmeet, SCC is used to find the directed acyclic decomposition of graph as mentioned in editorial. The other approach without SCC is mentioned in author’s solution and comments above. like this one