470,599 Members | 1,471 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,599 developers. It's quick & easy.

C++ Dijkstra's algorithm on a simple 10x10 map help please.

SwissProgrammer
213 128KB
Hello. I have been studying Dijkstra's algorithm. It looks like every example of its use (that I found) is in a CLI program.

Is there an example, that I can study and modify, of a small map (10 squares by 10 squares)? I would like to be able to weight each square and graphically see the result.

Just C++ please. No Visual C++.

Thank you.

Using the following graphic.
Attached Images
File Type: bmp Path10x10.bmp (541.3 KB, 98 views)
Jan 11 '21 #1

✓ answered by Banfa

Or try this

7 5632
Banfa
9,065 Expert Mod 8TB
Have you considered trying to implement it directly from its description or pseudo code e.g. as given at Wikipedia?
Jan 13 '21 #2
Banfa
9,065 Expert Mod 8TB
Or try this
Jan 13 '21 #3
SwissProgrammer
213 128KB
Banfa,

It looks like you supplied mathematical logic that I can use for this.

It looks like you supplied C++ code that I can learn from.

I am studying this now.

Best answer.

Thank you.
Jan 14 '21 #4
SwissProgrammer
213 128KB
I am still lost in the logic of how to apply Dijkstra's algorithm in C++ via a GUI.

I have been going through this various different ways and I decided to write it out one step at a time then convert that to C++.

I posted the following example elsewhere, and I am waiting on a reply.

I do not like to post to two sites the same question, but here it is. Maybe you guys could help:



I have a 10 by 10 grid that I want to use for a game and I have the following that I have written. It has become complicated. Now I want to use vectors in simplifying this code.

I have been studying vectors and arrays. I tried using arrays and it became so complicated that I am now trying to use vectors.

I can set up my WinMain and use CreateWindowExW to put text into for seeing a report of what I am doing. I can do that. I do not want to use CLI. I want help with placing the following into vectors and simplifying the code.

Would you please clean this up and use vectors? I can then try to use your logic further in the program.

C++11 please.

I want to be able to expand this to a larger size maybe 50 by 50, or 100 by 100, etc. Thus I want to automate it and not have to write out every one of the lines again and again.

Thank you.

Expand|Select|Wrap|Line Numbers
  1. // I wrote this in WordPad and then used CodeBlocks 17.12 for formatting to show on [...].
  2.  
  3. // I am Very Much a Beginner !
  4.  
  5. // This is in a GUI, not a CLI program.
  6.  
  7. // 10 by 10 grid.
  8.  
  9. // Each grid square is one pixel by one pixel (for mathematical simplicity for now). This can be enlarged later.
  10.  
  11. // Each grid square has a weight to resist game character movement.
  12.  
  13. // Each move can be in 8 different directions
  14.     // up left      x = x -1    y = y -1    diagonal move
  15.     // up           x = x       y = y -1     
  16.     // up right     x = x +1    y = y -1    diagonal move
  17.     // right        x = x +1    y = y
  18.     // down right   x = x +1    y = y +1    diagonal move
  19.     // down         x = x       y = y +1
  20.     // down left    x = x -1    y = y +1    diagonal move
  21.     // left         x = x -1    y = y
  22.  
  23. // There are conditions that must be met BEFORE being allowed to move.
  24.  
  25. /// After each one move is made, it is to be saved to a string that contains
  26. ///    a listing of each square that was passed through (x and y positions).
  27. /// Or if someone suggests a better way and shows me some code to do that, thank you.
  28.  
  29.  
  30.    //Step 1.) initialize x and y starting point and ending point.
  31.  
  32. int x = startingX;   //Starting X
  33. int y = startingY;   //Starting Y
  34. int Ex = endingX;    //Ending X
  35. int Ey = endingY;    //Ending Y
  36.  
  37. // Until changed set the initial Weight =1
  38. int Weight =1;
  39.  
  40. int MaximumX = 10;
  41. int MaximumY = 10;
  42.  
  43. int ThisTotaledWeight = 0;
  44.  
  45. int ShortestPath=9999;
  46.  
  47.  
  48.       // IF x = Ex and if y = Ey
  49.       // {
  50.             // No path to follow.
  51.             // Beginning and Ending locations are the same.
  52.             // Maybe put a notice of something like "no path to follow.".
  53.             // Maybe have the worker or person or etc. to respond verbally.
  54.             // Maybe return notification that path length is zero.
  55.             // Get out of this process.
  56.       // }
  57.  
  58.  
  59.  
  60.    //Step 2.) move distance 0 from beginning.
  61.    //Get the weight of   (startingX),   (startingY).
  62.    //Since it is the beginning and no moves have been made, weight of   (startingX),   (startingY) = 0 .
  63.    //Therefore ThisTotaledWeight = 0;
  64.    //MovesSoFar = 0;
  65. string s0000 = "MovesSoFar=0, ThisTotaledWeight,    (startingX),   (startingY)";
  66.  
  67.  
  68. /*
  69.    //Step 3.) move distance 1 from beginning.
  70.    //   Apply these conditions:
  71.       //   Can NOT move into a 999 square.
  72.       //   Can NOT move into a previously used square.
  73.          //Previously used squares:
  74.          //   (startingX, startingY)
  75.       //   Can NOT move into a square that is beside of a previously used square.
  76.          //Squares that is beside of a previously used square:
  77.          //   None.
  78.  
  79.  
  80.       // IF the previous conditions are not a problem, then do the following:
  81.          {
  82.             //Get the weight of    (startingX)-1,   (startingY)-1
  83.             //   Add that weight to ThisTotaledWeight from string s000.
  84.             //   Since a move of x-1 and y-1 is a diagonal move, add .0001 to the ThisTotaledWeight .
  85.          }
  86.  
  87.       // IF   (startingX),   (startingY)-1   is the end location
  88.             //      meaning that   (startingX)=Ex   and   (startingY)-1=Ey,
  89.          {
  90.             // IF ShortestPath>ThisTotaledWeight,
  91.                {
  92.                   // then
  93.                   // ShortestPath=ThisTotaledWeight;
  94.                }
  95.             // ELSE IF ShortestPath=ThisTotaledWeight,
  96.                {
  97.                   // then
  98.                   string s000_0001 = "MovesSoFar=1 & ThisTotaledWeight &   Move0 (startingX)   (startingY)   &   Move1 (startingX)-1   (startingY)-1 ";
  99.                }
  100.          }
  101.       // ELSE IF   (startingX),   (startingY)-1   is NOT the end location
  102.             //      meaning that both   (startingX) != Ex   and   (startingY)-1 != Ey,
  103.          {
  104.             // IF ShortestPath<ThisTotaledWeight,
  105.                {
  106.                   // then
  107.                   string s000_0001 = "999";
  108.                }
  109.             // ELSE IF ShortestPath=ThisTotaledWeight,
  110.                {
  111.                   // then
  112.                   string s000_0001 = "999";
  113.                }
  114.             // ELSE IF ShortestPath>ThisTotaledWeight,   such as it being still at 9999.
  115.                {
  116.                   // then
  117.                   string s000_0001 = "MovesSoFar=1 & ThisTotaledWeight &   Move0 (startingX)   (startingY)   &   Move1 (startingX)-1   (startingY)-1 ";
  118.                }
  119.          }
  120.  
  121. */
  122.  
  123.  
  124.  
  125.  
  126.  
  127.       //   IF the previous conditions are not a problem, then do the following:
  128.          //Get the weight of    (startingX),      (startingY)-1
  129.          //   Add that weight to ThisTotaledWeight from string s000.
  130.          // Etc.
  131.          string s000_0002 = "MovesSoFar=1, ThisTotaledWeight,   (startingX),      (startingY)-1";
  132.  
  133.       //   If the previous conditions are not a problem, then do the following:
  134.          //Get the weight of    (startingX)+1,  (startingY)-1
  135.          //   Add that weight to ThisTotaledWeight from string s000.
  136.          //   Since a move of x+1 and y-1 is a diagonal move, add .0001 to the ThisTotaledWeight .
  137.          // Etc.
  138.          string s000_0003 = "MovesSoFar=1, ThisTotaledWeight,   (startingX)+1,  (startingY)-1";
  139.  
  140.       //   If the previous conditions are not a problem, then do the following:
  141.          //Get the weight of    (startingX)+1,  (startingY)
  142.          //   Add that weight to ThisTotaledWeight from string s000.
  143.          // Etc.
  144.          string s000_0004 = "MovesSoFar=1, ThisTotaledWeight,   (startingX)+1,  (startingY)";
  145.  
  146.       //   If the previous conditions are not a problem, then do the following:
  147.          //Get the weight of    (startingX)+1,  (startingY)+1
  148.          //   Add that weight to ThisTotaledWeight from string s000.
  149.          //   Since a move of x+1 and y+1 is a diagonal move, add .0001 to the ThisTotaledWeight .
  150.          // Etc.
  151.          string s000_0005 = "MovesSoFar=1, ThisTotaledWeight,   (startingX)+1,  (startingY)+1";
  152.  
  153.       //   If the previous conditions are not a problem, then do the following:
  154.          //Get the weight of    (startingX),      (startingY)+1
  155.          //   Add that weight to ThisTotaledWeight from string s000.
  156.          // Etc.
  157.          string s000_0006 = "MovesSoFar=1, ThisTotaledWeight,   (startingX),      (startingY)+1";
  158.  
  159.       //   If the previous conditions are not a problem, then do the following:
  160.          //Get the weight of    (startingX)-1,   (startingY)+1
  161.          //   Add that weight to ThisTotaledWeight from string s000.
  162.          //   Since a move of x-1 and y+1 is a diagonal move, add .0001 to the ThisTotaledWeight .
  163.          // Etc.
  164.          string s000_0007 = "MovesSoFar=1, ThisTotaledWeight,   (startingX)-1,   (startingY)+1";
  165.  
  166.       //   If the previous conditions are not a problem, then do the following:
  167.          //Get the weight of    (startingX)-1,   (startingY)
  168.          //   Add that weight to ThisTotaledWeight from string s000.
  169.          // Etc.
  170.          string s000_0008 = "MovesSoFar=1, ThisTotaledWeight,   (startingX)-1,   (startingY)";
  171.  
  172.  
  173.  
  174.    //Step 4.) move distance 2 from beginning.
  175.    //   These all start at s0001   which is  x=(startingX)-1   and   y=(startingY)-1
  176.    //   Can NOT move into a square that either x or y are less than zero.
  177.    //   Can NOT move into a square that either x or y are larger than the maximum value for each.
  178.    //   Can NOT move into a 999 square.
  179.    //   Can NOT move into a previously used square.
  180.       //Previously used squares:
  181.       //   (startingX, startingY)
  182.    //   Can NOT move into a square that is beside of a previously used square.
  183.       //Squares that is beside of a previously used square:
  184.       //   (startingX),   (startingY);
  185.       //   (startingX)-1,   (startingY)-1;
  186.       //   (startingX),      (startingY)-1;
  187.       //   (startingX)+1,  (startingY)-1;
  188.       //   (startingX)+1,  (startingY);
  189.       //   (startingX)+1,  (startingY)+1;
  190.       //   (startingX),      (startingY)+1;
  191.       //   (startingX)-1,   (startingY)+1;
  192.       //   (startingX)-1,   (startingY);
  193.  
  194.  
  195. // Un-modified for conditionals:
  196. // string s000_0001_0001 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1-1,   (startingY)-1-1";
  197. // string s000_0001_0002 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1,      (startingY)-1-1";
  198. // string s000_0001_0003 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1+1,  (startingY)-1-1";
  199. // string s000_0001_0004 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1+1,  (startingY)-1";
  200. // string s000_0001_0005 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1+1,  (startingY)-1+1";
  201. // string s000_0001_0006 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1,      (startingY)-1+1";
  202. // string s000_0001_0007 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1-1,   (startingY)-1+1";
  203. // string s000_0001_0008 = "MovesSoFar=2,   (startingX)-1,   (startingY)-1   &   (startingX)-1-1,   (startingY)-1";
  204.  
  205.  
  206. // The following IS Modified for conditionals:
  207.  
  208.    ///   Can NOT move into a square that either x or y are less than zero.
  209.       //      If the location   x=(startingX)-1-1   is   >=0
  210.       //      and
  211.       //      If the location   y=(startingY)-1-1   is   >=0
  212.       //         Then proceed to next conditional.
  213.  
  214.  
  215.    ///   Can NOT move into a square that either x or y are larger than the maximum value for each.
  216.       //      If the location   x=(startingX)-1-1   is   <=MaximumX
  217.       //      and
  218.       //      If the location   y=(startingY)-1-1   is   <=MaximumY
  219.       //         Then proceed to next conditional.
  220.  
  221.  
  222.    ///   Can NOT move into a 999 square.
  223.       //      If the location   x=(startingX)-1-1   and   y=(startingY)-1-1   has a Weight < 999
  224.       //         Then proceed to next conditional.
  225.  
  226.  
  227.    ///   Can NOT move into a previously used square.
  228.       //   If the location   x=(startingX)-1-1   and   y=(startingY)-1-1   is not a part of the list of
  229.          //   From Move 0:
  230.             //   (startingX),   (startingY);
  231.          //   From Move 1:
  232.             //   (startingX)-1,   (startingY)-1;
  233.             //   (startingX),      (startingY)-1;
  234.             //   (startingX)+1,  (startingY)-1;
  235.             //   (startingX)+1,  (startingY);
  236.             //   (startingX)+1,  (startingY)+1;
  237.             //   (startingX),      (startingY)+1;
  238.             //   (startingX)-1,   (startingY)+1;
  239.             //   (startingX)-1,   (startingY)";
  240.  
  241.    ///   Then do this:
  242.  
  243.       //   If the previous conditions are not a problem, then do the following:
  244.          //Get the weight of    (startingX),      (startingY)-1
  245.          //   Add that weight to ThisTotaledWeight from string s000_0001.
  246.          //   Since a move of x-1 and y-1 is a diagonal move, add .0001 to the ThisTotaledWeight .
  247.          string s000_0001_0001 = "MovesSoFar=2, ThisTotaledWeight=2,   Move0 (startingX),   (startingY)   &   Move1 (startingX)-1,   (startingY)-1   &   Move2 (startingX)-1-1,   (startingY)-1-1";
  248.  
  249.    //   Else   string s000_0001_0001="999";.
  250.  
  251. // Continue...
  252.  
  253. //   When complete, save all of this into a text file to be able to read and to examine.
  254.  
  255. //   When complete, list all of the strings that have a ThisTotaledWeight as lowest value and then save this into another text file to be able to read and to examine.
  256.  
  257.  
  258.  
I have been struggling with this, occasionally, for weeks and it is interrupting my life. I think that I now have at least the conditionals in place. But, I am having great difficulty in condensing it in such a manner that the map can be other than 10x10.

Please help !
Jan 29 '21 #5
SwissProgrammer
213 128KB
Glory to God now I can use C++ vectors.

Wow! And to think that at some time Stroustrup held all that stuff (new!) in his mind and had to explain it as new concepts step by step to some consortium that came up with programming rules for code that they did not even design.

Some C++ concepts I have had difficulty with. (Ok, lots of them!)

I read previous posts here on bytes.com and more at https://www.bitdegree.org/learn/c-plus-plus-vector and https://www.tutorialspoint.com/cpp_s...ary/vector.htm and other pages. Using an online compiler I now have an example of vector applications with notes that I can reference.

But, I still have had difficulty condensing the previous code.

I really am new at C++ and it really has been intensely difficult for me.


If I get help with condensing the previous post's code thank you.
Jan 31 '21 #6
SwissProgrammer
213 128KB
I am still struggling with condensing that code.

Help. Please help.
Feb 22 '21 #7
SwissProgrammer
213 128KB
Here is my latest attempt.


Example:
Grid x and y
x = 10 horizontal count.
y = 10 vertical count.

weights as follows with A and B representing the Start A and the Finish B:

1.....1.....1.....5.....1.....1.....1.....1.....1. ....1

1.....1.....1.....5.....1.....1.....1.....1.....1. ....1

1.....1.....1.....5.....1.....1.....1.....1.....1. ....1

1.....1.....1.....5.....1.....1.....1.....1.....1. ....1

1.....1.....A.....2.....1.....1.....B.....1.....1.....1

1.....1.....1.....1.....1.....1.....1.....1.....1. ....1

1.....1.....1.....1.....1.....1.....1.....1.....1. ....1

1.....1.....1.....1.....1.....1.....1.....1.....1. ....1

1.....1.....1.....1.....1.....1.....1.....1.....1. ....1

1.....1.....1.....1.....1.....1.....1.....1.....1. ....1

Position = x coordinate, y coordinate
A = 3 , 5
B = 7 , 5


Using separator " ^ " in the path represented by a string.


Using in a string form and placing that string in a vector array to be analyzed as I go and later.


Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3. Beginning at 3,5 which is "A"
  4. Move to     Total distance      Total moves     Represented by x and y      In string form      Using?
  5. 2,4         1                   1               3,5 to 2,4                  1 ^ 1 ^ 3,5to2,4    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  6. 3,4         1                   1               3,5 to 3,4                  1 ^ 1 ^ 3,5to3,4    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  7. 4,4         5                   1               3,5 to 4,4                  5 ^ 1 ^ 3,5to4,4    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  8. 4,5         2                   1               3,5 to 4,5                  2 ^ 1 ^ 3,5to4,5    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  9. 4,6         1                   1               3,5 to 4,6                  1 ^ 1 ^ 3,5to4,6    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  10. 3,6         1                   1               3,5 to 3,6                  1 ^ 1 ^ 3,5to3,6    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  11. 2,6         1                   1               3,5 to 2,6                  1 ^ 1 ^ 3,5to2,6    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  12. 2,5         1                   1               3,5 to 2,5                  1 ^ 1 ^ 3,5to2,5    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  13.  
  14. Beginning at 2,4
  15. Move to     Total distance      Total moves     Represented by x and y              In string form                 Using?
  16. 1,3         2                   2               (3,5 to 2,4) then (2,4 to 1,3)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to1,3    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  17. 2,3         2                   2               (3,5 to 2,4) then (2,4 to 2,3)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to2,3    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  18. 3,3         2                   2               (3,5 to 2,4) then (2,4 to 3,3)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to3,3    maybe, for some reference. So, save it to the array as a new entry to be analyzed later.
  19. 3,4         2                   2               (3,5 to 2,4) then (2,4 to 3,4)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to3,4    NO since it is already being used in 3,5 to 3,4 and it has a shorter path of 1.
  20. 3,5         2                   2               (3,5 to 2,4) then (2,4 to 3,5)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to3,5    NO since it is the origin 3,4 and it has a shorter path of 0.
  21. 2,5         2                   2               (3,5 to 2,4) then (2,4 to 2,5)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to2,5    NO since it is already being used in 3,5 to 2,5 and it has a shorter path of 1.
  22. 1,5         2                   2               (3,5 to 2,4) then (2,4 to 1,5)      2 ^ 2 ^ 3,5to2,4 ^ 2,4to1,5    NO since it is already being used in 3,5 to 2,5 and it has a shorter path of 1.
  23.  
  24. Beginning at 3,4
  25. Move to     Total distance      Total moves     Represented by x and y              In string form                Using?
  26. ... etc. 
  27.  
  28. */
  29.  
  30.  
I want to put the results into a string array, a vector array consisting of strings.
I can sort through that later.


If I use this process, then I might have multiple variations of the shortest path to choose from.


If I could get this part to work, then I can place other conditionals (if needed) in also.

But, I am having difficulty with this part so far.

I can use a vector array of strings. But, this is more than I have been able to wrap my mind around and actually code in. I feel exasperated. Overwhelmed. etc.

Help please.
Mar 3 '21 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Rob | last post: by
4 posts views Thread by thomasau | last post: by
dmjpro
5 posts views Thread by dmjpro | last post: by
6 posts views Thread by Bobby Knight | last post: by
5 posts views Thread by onkardesai | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.