Computer Science Canada Dynamic Programming Problem. |
Author: | Cinjection [ Tue Feb 12, 2008 6:54 pm ] |
Post subject: | Dynamic Programming Problem. |
Today, I wrote the Dwite contest and tried to get #4 (http://dwite.org/questions/train_ride.html) complete, which I understand is a dynamic programming type problem. I've never written a solution to such a problem, but I gave it a go. Here is what I got up to, in terms of a definition: Let A[r][c] = The grid Let D[r][c] = Least number of steps between [r][c] and 'S' (This is initialized to 999) Let M(r, c) return the least number of steps to reach r,c My recursive definition was: M(r, c): if (r || c are out of bounds) return 1000 if ( D[r][c] != 999) return D[r][c] if ( A[r][c] = 'x' || ' ' ) return 1000; if ( A[r][c] = 'S' ) return 0; else: return Min( M(r+1, c), M(r, c+1), M(r+1,c+1), M(r-1, c), M(r, c-1), M(r-1,c-1), M(r+1, c-1), M(r-1, c+1) ) + 1; And I Fill the array (D) as such: for (int i(0); i < 5; i++) { for(int j(0); j <10; j++) D[i][j] = M(i,j); } And of course the final answer would be: D[i][j], where i and j are the row and column positions of the ending point, respectively. I really don't know if I'm going about this the correct way. I did not get to code to work for the contest, so I was wondering if someone could tell me if I'm at all close a valid DP solution.[/url] |
Author: | HeavenAgain [ Tue Feb 12, 2008 6:57 pm ] |
Post subject: | RE:Dynamic Programming Problem. |
its not really a DP problem, more of a BFS |
Author: | Cinjection [ Tue Feb 12, 2008 6:58 pm ] |
Post subject: | Re: RE:Dynamic Programming Problem. |
HeavenAgain @ Tue Feb 12, 2008 6:57 pm wrote: its not really a DP problem, more of a BFS
BFS? In any case, is this possible to do with DP? I research this for next time though. |
Author: | HeavenAgain [ Tue Feb 12, 2008 6:59 pm ] |
Post subject: | RE:Dynamic Programming Problem. |
http://en.wikipedia.org/wiki/Breadth-first_search or http://en.wikipedia.org/wiki/Depth-first_search both can lead to your solution |
Author: | Cinjection [ Tue Feb 12, 2008 8:26 pm ] |
Post subject: | Re: RE:Dynamic Programming Problem. |
HeavenAgain @ Tue Feb 12, 2008 6:59 pm wrote: http://en.wikipedia.org/wiki/Breadth-first_search
or http://en.wikipedia.org/wiki/Depth-first_search both can lead to your solution Yeah, I've read both of those already. I want a specific example to see how close I was to it. |
Author: | HeavenAgain [ Tue Feb 12, 2008 8:32 pm ] |
Post subject: | RE:Dynamic Programming Problem. |
first define a 2-d array, with default value thats very high and get your starting position, and ending position, and just walk all the possible path, and take the shortest one try download the top few submissions, and understand their code? Guppy Attack and kill -9 had a pretty clear solution (in my opinion, and for top 5 only, i didnt look at others yet) |
Author: | Cinjection [ Wed Feb 13, 2008 7:53 am ] |
Post subject: | Re: RE:Dynamic Programming Problem. |
HeavenAgain @ Tue Feb 12, 2008 8:32 pm wrote: first define a 2-d array, with default value thats very high
and get your starting position, and ending position, and just walk all the possible path, and take the shortest one try download the top few submissions, and understand their code? Guppy Attack and kill -9 had a pretty clear solution (in my opinion, and for top 5 only, i didnt look at others yet) Hmm, we the process you described above seems more like a brute-force method, and I'd like to try to solve this with DP. As for checking out the other submissions, I have, but I really just want to see how close my particular solution is. |
Author: | OneOffDriveByPoster [ Wed Feb 13, 2008 1:27 pm ] | ||
Post subject: | Re: RE:Dynamic Programming Problem. | ||
Hopefully a correct BFS implementation...
|