Note:- I am NOT asking you to write the program for me. I am NOT asking you to do the homework for me either. All I am asking you is if you could suggest an algorithm for me.
Present below are two algorithms. Can you please suggest me an algorithm to make the c++ program for them.Thanks in advance.
Problem 1 : Grid game
You are given a square grid of positive and negative numbers. You have to start at the top left corner of the grid and find a path to the bottom-right corner. In each step, you are only allowed to move one position to the right or one position down. As a special bonus, you are allowed at most one move that can be either to the left or up. Note that you are allowed to visit a position more than once as a result of this special move.
Your aim is to find such a path that has the maximum weight, where the weight of a path is the sum of all the numbers visited along the path.
Input format
The first line of the input contains one integer N, the number of rows and columns in the grid. Lines 2 to N+1 describe the N rows of the grid. Each of these N lines of input consists of N space separated integers, corresponding to the numbers in one row.
Notes
In all cases, N ≤ 2000. Each number in the grid is guaranteed to be less than 1000.
Santa Claus has lined up a row of bowls, each containing some chocolates. Nikhil has been told that he can pick up as many of these bowls as he wants, provided that he never picks two consecutive bowls.
Your aim is to help Nikhil choose a set of bowls so that he maximizes the number of chocolates that he picks up.
Input format
The first line of the input contains one integer N, the number of bowls. This is followed by a line containing N integers, describing the number of chocolates in each of the N bowls.
Notes
Yeah, backtracking seems like a good idea, (...unless it is explicitly forbidden).
If you have access to some bibliography, check Brassard & Bratley's "Fundamentals of Algorithmics". The backtracking section should be marked somewhere in the index, so check it out.
These are examples of dynamic algorithm. Basically dynamic algorithm is backtracking with memory, i.e. if you know that one of the partial solution is always better than the other partial solution, dont consider the first one further.
e.g. in first case we know that path -12,10,-14 is better that -12,7,-14. So there is no need to consider -12,7,-14,-15.
in second we know that if we include 30 and 25, then the best set is 30 20 25, then no need to consider 30 8 11 25 20.
The classical example is matrix chain multiplication. http://en.wikipedia.org/wiki/Matrix_chain_multiplication
Dynamic programming sounds like a good idea, but only if you've already done some backtracking exercises before. Also, Dynamic Programming is NOT backtracking with memory. Only problems that verify Bell's optimality principle can be solved through this technique.
Nikhar if you feel adventurous enough, you can give a try to this technique, but it's more difficult than backtracking.
Hey guys......Thanks everyone for the reply. But I'm not at all familiar with backtracking. Can anyone suggest me a few backtracking exercises? A site with backtracking examples.