Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Wednesday, April 9, 2014

SPOJ 6828. Lineup (LINEUP)

http://www.spoj.com/problems/LINEUP/
 By looking at the constraints I can tell that it is enough to just look over all the possible configurations and pick the best one, the hardest problem is the implementation. We can do it recursively with backtracking which means every time we try something we keep our steps so that we can return to some point and continue with other configuration. Check the source code for a good understanding.


       DOWNLOAD THE FULL SOURCE CODE




Tuesday, March 25, 2014

SPOJ 101. Fishmonger

http://www.spoj.com/problems/FISHER/

 This task is possible to solve with both dynamic and recursive approach. Here I will present you the recursive one. We can have an obvious DFS(you can find more info about DFS here approach, start with the 1st and take every single path, I am not sure whether the simple DG We can use some optimizations to reduce the time. Our DFS should have 3 parameters: N,M,T. N shows the node we are currently in , T shows the time it took us to reach the current node, and M shows the money we paid so far. Here are some optimizations you should do. We also have 2 parameters ansT and ansM which show the answer ansT stands for time and ansM stands for money.
1. Marking the node we already visited, (of course don't forget to unmark it after the recursive call).  
2. If the current money we have already paid is bigger then the answer we have got previously then here is no need in continuing our recursion because we are not going to get a better answer.
3. If the time which we have spent is greater then the given maximum time we need to break our recursion immediately.
 Also check the source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE