Thread: C fastest path problem

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have a lazy version that I think works. Total up your current row and your current column. Go whatever direction is less. Repeat until you are at the goal.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by quzah View Post
    I have a lazy version that I think works. Total up your current row and your current column. Go whatever direction is less. Repeat until you are at the goal.


    Quzah.
    Nah, this wont work for wacky cases like the following
    Code:
     1  1  1  1 20
     2  1  1  1 20
     2  1  1  1 20
     2  1  1  1 20
     2  1  1  1  1
    Your algo would head down instead of right, which will cost you more in the long run.

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What's with all the bazaar solutions?!
    Is this problem not screaming "Use the A* algorithm" loud enough?

    You most definitely need to be comfortable with linked-lists and dynamic memory allocation, however you solve this.

    By the way, have fun solving this one:
    Code:
     1  1  1  1  1 99
    14 12  8  2  1 89
     2  2  2  4  1 79
     2  8  1  1  1 69
     2  4 15 17 21 59
     2  2  2  2  2  1
    Last edited by iMalc; 03-01-2011 at 12:35 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    By the way, have fun solving this one:
    Code:
     1  1  1  1  1 99
    14 12  8  2  1 89
     2  2  2  4  1 79
     2  8  1  1  1 69
     2  4 15 17 21 59
     2  2  2  2  2  1
    Mine solves that. Straight down the left side, straight across the bottom.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by quzah View Post
    Mine solves that. Straight down the left side, straight across the bottom.

    Quzah.
    It gets there, but still fails to find the fastest path. iMalc highlighted the fasted path in red text. Yours gets to array[2][0] with a cost of 1 + 14 + 2 = 17. iMalc's path gets there with a cost of 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 2 = 16. After that, you both follow the same path, meaning he beats you with a cost of 31 to your 32.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I did the math in my head. Too tired I guess.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah I made that particular maze to demonstrate that the optimal path doesn't necessarily involve only moving right or down, but also may involve moving left or upwards.

    I also picked several of the constants such that it was only just more efficient to follow the hilighted path. I could have made the hilighted path all 1's and the other spaces all 99's, but that might fool some people into thinking that a "move towards the lowest number" algorithm (a.k.a Hill Climbing) would always work. Change the 14 to 11 and you'd certainly be best going straight down though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It also makes it look like the only solution though is to take whatever number is lowest from your immediate square. Which means this would get you stuck:
    Code:
    1 1 5
    2 3 5
    1 5 5
    1 5 5
    I was getting hung up on trying to make it only check east and south of you, since he had mentioned working from 0,0 to N,N. For some reason I got it in my head to try to work a solution that only was checking two directions of travel.

    Quzah.
    Last edited by quzah; 03-01-2011 at 02:30 AM.
    Hope is the first step on the road to disappointment.

  9. #24
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    The classical solution to this problem: Dijkstra's algorithm. A* would also work, but without a good heuristic, it wouldn't be much of an improvement.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Can't figure out what keeps hanging up my program
    By shays in forum C Programming
    Replies: 7
    Last Post: 11-12-2007, 02:59 PM
  3. Shortest path problem
    By Digitalxero in forum C++ Programming
    Replies: 0
    Last Post: 10-25-2005, 05:32 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM