<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[algorithmen frage zu shortest path]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte da folgende algorithmen frage:<br />
gegeben eine mxn matrix.<br />
gegeben ein startpunkt (x,y) and ein endpunkt (x,y) in der matrix.<br />
suche alle kuerzesten pfade vom start zum endpunkt. es ist nur moeglich vertikal oder korizontal sich zu bewegen, keine diagonalen sprünge.</p>
<p>einen kürzesten pfad zu finden ist recht einfach:<br />
beispiel:<br />
8x3 matrix<br />
startpunkt: 0,0<br />
endpunkt: 4,2</p>
<p>ein kürzester pfad wäre dann: 0,0, -&gt; 1,0 -&gt; 2,0 -&gt; 3,0 -&gt; 4,0 -&gt; 4,1 -&gt; 4,2</p>
<p>nur wie finde ich alle möglichkeiten...ich muss alle bereits gefundenen pfade mittracken?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327680/algorithmen-frage-zu-shortest-path</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 13:32:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327680.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Aug 2014 07:25:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 07:25:10 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hätte da folgende algorithmen frage:<br />
gegeben eine mxn matrix.<br />
gegeben ein startpunkt (x,y) and ein endpunkt (x,y) in der matrix.<br />
suche alle kuerzesten pfade vom start zum endpunkt. es ist nur moeglich vertikal oder korizontal sich zu bewegen, keine diagonalen sprünge.</p>
<p>einen kürzesten pfad zu finden ist recht einfach:<br />
beispiel:<br />
8x3 matrix<br />
startpunkt: 0,0<br />
endpunkt: 4,2</p>
<p>ein kürzester pfad wäre dann: 0,0, -&gt; 1,0 -&gt; 2,0 -&gt; 3,0 -&gt; 4,0 -&gt; 4,1 -&gt; 4,2</p>
<p>nur wie finde ich alle möglichkeiten...ich muss alle bereits gefundenen pfade mittracken?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415054</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 07:25:10 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 07:44:31 GMT]]></title><description><![CDATA[<p>robery schrieb:</p>
<blockquote>
<p>einen kürzesten pfad zu finden ist recht einfach:<br />
beispiel:<br />
8x3 matrix<br />
startpunkt: 0,0<br />
endpunkt: 4,2</p>
<p>ein kürzester pfad wäre dann: 0,0, -&gt; 1,0 -&gt; 2,0 -&gt; 3,0 -&gt; 4,0 -&gt; 4,1 -&gt; 4,2</p>
<p>nur wie finde ich alle möglichkeiten...ich muss alle bereits gefundenen pfade mittracken?</p>
</blockquote>
<p>Ist das Problem jetzt alle Permutationen zu erzeugen, die viermal rechts gehen und zweimal runter?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415057</guid><dc:creator><![CDATA[Permutateur]]></dc:creator><pubDate>Wed, 27 Aug 2014 07:44:31 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 07:50:05 GMT]]></title><description><![CDATA[<p>ja, und welche datenstruktur eignet sich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415058</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415058</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 07:50:05 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 07:54:20 GMT]]></title><description><![CDATA[<p>Ist es wirklich nötig die alle zu speichern? Es ist doch klar dass die kürzesten Pfade all jene sind die in jedem Schritt entweder nach rechts oder nach oben/unten (je nachdem ob <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>y</mi><mi mathvariant="normal">_</mi><mn>2</mn><mo>&lt;</mo><mi>y</mi><mi mathvariant="normal">_</mi><mn>1</mn></mrow><annotation encoding="application/x-tex">y\_2 &lt; y\_1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.95444em;vertical-align:-0.31em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">2</span><span class="mrel">&lt;</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">1</span></span></span></span>) gehen. Davon gibt es <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mfrac><mrow><mo>(</mo><mi mathvariant="normal">∣</mi><mi>y</mi><mi mathvariant="normal">_</mi><mn>1</mn><mo>−</mo><mi>y</mi><mi mathvariant="normal">_</mi><mn>2</mn><mi mathvariant="normal">∣</mi><mo>+</mo><mi>x</mi><mi mathvariant="normal">_</mi><mn>2</mn><mo>−</mo><mi>x</mi><mi mathvariant="normal">_</mi><mn>1</mn><mo>)</mo><mo>!</mo></mrow><mrow><mo>(</mo><mi mathvariant="normal">∣</mi><mi>y</mi><mi mathvariant="normal">_</mi><mn>1</mn><mo>−</mo><mi>y</mi><mi mathvariant="normal">_</mi><mn>2</mn><mi mathvariant="normal">∣</mi><mo>)</mo><mo>!</mo><mo>(</mo><mi>x</mi><mi mathvariant="normal">_</mi><mn>2</mn><mo>−</mo><mi>x</mi><mi mathvariant="normal">_</mi><mn>1</mn><mo>)</mo><mo>!</mo></mrow></mfrac></mrow><annotation encoding="application/x-tex">\frac{(|y\_1 - y\_2| + x\_2 - x\_1)!}{(|y\_1 - y\_2|)!(x\_2 - x\_1)!}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:1.052em;"></span><span class="strut bottom" style="height:1.6139999999999999em;vertical-align:-0.5619999999999999em;"></span><span class="base textstyle uncramped"><span class="mord reset-textstyle textstyle uncramped"><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span><span class="mfrac"><span class="vlist"><span style="top:0.345em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle cramped"><span class="mord scriptstyle cramped"><span class="mopen">(</span><span class="mord mathrm">∣</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">1</span><span class="mbin">−</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">2</span><span class="mord mathrm">∣</span><span class="mclose">)</span><span class="mclose">!</span><span class="mopen">(</span><span class="mord mathit">x</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">2</span><span class="mbin">−</span><span class="mord mathit">x</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">1</span><span class="mclose">)</span><span class="mclose">!</span></span></span></span><span style="top:-0.22999999999999998em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle textstyle uncramped frac-line"></span></span><span style="top:-0.527em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mopen">(</span><span class="mord mathrm">∣</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">1</span><span class="mbin">−</span><span class="mord mathit" style="margin-right:0.03588em;">y</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">2</span><span class="mord mathrm">∣</span><span class="mbin">+</span><span class="mord mathit">x</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">2</span><span class="mbin">−</span><span class="mord mathit">x</span><span class="mord mathrm" style="margin-right:0.02778em;">_</span><span class="mord mathrm">1</span><span class="mclose">)</span><span class="mclose">!</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">​</span></span>​</span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter"></span></span></span></span></span>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415059</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 27 Aug 2014 07:54:20 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 07:56:22 GMT]]></title><description><![CDATA[<p>Also wenn keine Hindernisse im Weg sind o.Ä. dann würde ich <a href="http://de.wikipedia.org/wiki/Dijkstra-Algorithmus" rel="nofollow">http://de.wikipedia.org/wiki/Dijkstra-Algorithmus</a> nehmen.</p>
<p>Mit deinen Beispielpunkten ist aber auch das Witzlos.<br />
Wenn du aber mit Hindernissen und ähnlichen arbeiten willst dann würde sich der <a href="http://de.wikipedia.org/wiki/A*-Algorithmus" rel="nofollow">http://de.wikipedia.org/wiki/A*-Algorithmus</a> anbieten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415060</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415060</guid><dc:creator><![CDATA[IsDochLame]]></dc:creator><pubDate>Wed, 27 Aug 2014 07:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 08:02:50 GMT]]></title><description><![CDATA[<p>es gibt keine hindernisse in der matrix.</p>
<p>Dijkstra-Algorithmus schein etwas overhead dafuer zu sein...der findet auch nur einen kürzesten pfad...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415061</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415061</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 08:02:50 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 08:03:48 GMT]]></title><description><![CDATA[<p>ich will wirklich alle kürzesten pfade speichern!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415062</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 08:03:48 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 08:23:20 GMT]]></title><description><![CDATA[<p>hier mal ein bisschen code, wie ich einen shortest pfad finde...wie kann ich nun den algo anpassen um alle shortest pfade zu finden...datenstrukturen etc.?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stack&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

enum Direction {
  NONE = 0,
  UP,
  DOWN,
  LEFT,
  RIGHT
} direction;

class pos {
public:
  int y;
  int x;
  pos(int r, int c): y(r), x(c) {}
  bool operator==(const pos &amp;rhs) { 
    return y == rhs.y &amp;&amp; x == rhs.x;
  }
  bool operator!=(const pos &amp;rhs) { 
    return y != rhs.y || x != rhs.x;
  }
  pos &amp;operator=(const pos &amp;other) {
    if (this != &amp;other) {
      y = other.x;
      x = other.y;    
    }

    return *this;
  }
  pos &amp;operator+(const pos &amp;other) {
    if (this != &amp;other) {
      y += other.x;
      x += other.y;   
    }

    return *this;
  }
};

template &lt;typename T&gt;
void printMatrix(const vector&lt;vector&lt;T&gt;&gt; &amp;m) {
  for(int i = 0; i &lt; m.size(); i++) {
    for(int j = 0; j &lt; m[i].size(); j++) {
      cout &lt;&lt; m[i][j] &lt;&lt; ' ';
    }
    cout &lt;&lt; endl;
  }
  cout &lt;&lt; endl;
}

int getNextMoveStep(const Direction &amp;moveDir) {
  if (moveDir == LEFT || moveDir == UP) {
    return -1;
  } else if (moveDir == RIGHT || moveDir == DOWN) {
    return 1;
  }
}

bool checkBoundX(const pos &amp;curr, const Direction &amp;moveDir, int maxX) {
  if (((curr.x + getNextMoveStep(moveDir)) &gt;= 0) &amp;&amp; 
      ((curr.x + getNextMoveStep(moveDir)) &lt; maxX)) {
    return true;
  } 
  return false;
}

bool checkBoundY(const pos &amp;curr, const Direction &amp;moveDir, int maxY) {
  if (((curr.y + getNextMoveStep(moveDir)) &gt;= 0) &amp;&amp; 
      ((curr.y + getNextMoveStep(moveDir)) &lt; maxY)) {
    return true;
  }
  return false;
}

bool doMove(pos &amp;curr, const Direction &amp;moveDir, int maxX, int maxY) {
  if ((moveDir == LEFT || moveDir == RIGHT) &amp;&amp; checkBoundX(curr, moveDir, maxX)) {
    curr.x += getNextMoveStep(moveDir);
    return true;
  }
  if ((moveDir == UP || moveDir == DOWN) &amp;&amp; checkBoundY(curr, moveDir, maxY)) {
    curr.y += getNextMoveStep(moveDir);
    return true;
  }
  return false;
}

bool checkMoveX(Direction &amp;moveDir, const pos &amp;curr, pos end) {
    if (curr.x != end.x) {
      if (curr.x &lt; end.x) {
        moveDir = RIGHT;
      } else {
        moveDir = LEFT;
      }

      return true;
    }
    return false;
}

bool checkMoveY(Direction &amp;moveDir, const pos &amp;curr, pos end) {
    if (curr.y != end.y) {
      if (curr.y &lt; end.y) {
        moveDir = DOWN;
      } else {
        moveDir = UP;
      }

      return true;
    }
    return false;
}

bool getMoveDir(Direction &amp;moveDir, const pos &amp;curr, pos end) {
  if(checkMoveX(moveDir, curr, end)) {
    return true;
  }
  if(checkMoveY(moveDir, curr, end)) {
      return true;
  }
  return false;
}

vector&lt;vector&lt;pos&gt;&gt; findAllPaths(vector&lt;vector&lt;int&gt;&gt; &amp;m, pos start, pos end) {
  vector&lt;vector&lt;pos&gt;&gt; allPaths;
  int maxY = m.size();
  int maxX = m[0].size();

  pos curr = start;
  vector&lt;pos&gt; currPath;

  while (curr != end) {
    Direction moveDir = NONE;

    bool canMove = getMoveDir(moveDir, curr, end);
    if (canMove == false) {
    	break;
    }

    doMove(curr, moveDir, maxX, maxY);  
    currPath.push_back(curr);
  }

  allPaths.push_back(currPath);
  return allPaths;
}

int main() {
  // your code goes here

  vector&lt;vector&lt;int&gt;&gt; m = {{0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0}};

  vector&lt;vector&lt;pos&gt;&gt; allShortestPaths = findAllPaths(m, pos(0,0), pos(2,4));

  for (int i = 0; i &lt; allShortestPaths.size(); i++) {
    for_each(allShortestPaths[i].begin(), allShortestPaths[i].end(), [](pos curr){ cout &lt;&lt; curr.x &lt;&lt; &quot;,&quot; &lt;&lt; curr.y &lt;&lt; &quot; &quot;;});
    cout &lt;&lt; endl;
  }

  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2415064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415064</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 08:23:20 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 09:58:28 GMT]]></title><description><![CDATA[<p>robery schrieb:</p>
<blockquote>
<p>wie kann ich nun den algo anpassen um alle shortest pfade zu finden?</p>
</blockquote>
<p>Wegwerfen. Und dann '\n' statt endl nehmen.<br />
Rekursion bietet sich an. Erstmal das 8-Damen-Problem mit Backtracking lösen.<br />
Und googlen. <a href="http://www.math.mcgill.ca/haron/Papers/Journal/CoolCombo.pdf" rel="nofollow">http://www.math.mcgill.ca/haron/Papers/Journal/CoolCombo.pdf</a> oder schlicht &quot;enumerate binary combinations&quot; oder so.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415071</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415071</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 27 Aug 2014 09:58:28 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 10:16:19 GMT]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;
#include &lt;stack&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

enum Direction {
  NONE = 0,//None? Wozu? Wozu auf 0 gesetzt? 
  UP,
  DOWN,
  LEFT,
  RIGHT
} direction;

class pos {
public:
  int y;
  int x;
  pos(int r, int c): y(r), x(c) {}
  bool operator==(const pos &amp;rhs) { 
    return y == rhs.y &amp;&amp; x == rhs.x;
  }
  bool operator!=(const pos &amp;rhs) { //return !==
    return y != rhs.y || x != rhs.x;
  }
  pos &amp;operator=(const pos &amp;other) {
    if (this != &amp;other) {//sinnloser test
      y = other.x;
      x = other.y;    
    }

    return *this;
  }
  pos &amp;operator+(const pos &amp;other) {
    if (this != &amp;other) {//sinnloser test
      y += other.x;
      x += other.y;   
    }

    return *this;//das war += und nicht +
  }
};

template &lt;typename T&gt;
void printMatrix(const vector&lt;vector&lt;T&gt;&gt; &amp;m) {
  for(int i = 0; i &lt; m.size(); i++) {//hoffentlich ruft bald c++14 an. 
    for(int j = 0; j &lt; m[i].size(); j++) {
      cout &lt;&lt; m[i][j] &lt;&lt; ' ';
    }
    cout &lt;&lt; endl;//'\n'
  }
  cout &lt;&lt; endl;
}

int getNextMoveStep(const Direction &amp;moveDir) {
  if (moveDir == LEFT || moveDir == UP) {
    return -1;
  } else if (moveDir == RIGHT || moveDir == DOWN) {
    return 1;
  }
/*
............................................________
....................................,.-'&quot;...................``~.,
.............................,.-&quot;...................................&quot;-.,
.........................,/...............................................&quot;:,
.....................,?......................................................,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:&quot;........./
..............?.....__.........................................:`.........../
............./__.(.....&quot;~-,_..............................,:`........../
.........../(_....&quot;~,_........&quot;~,_....................,:`........_/
..........{.._$;_......&quot;=,_.......&quot;-,_.......,.-~-,},.~&quot;;/....}
...........((.....*~_.......&quot;=-._......&quot;;,,./`..../&quot;............../
...,,,___.`~,......&quot;~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-&quot;
............/.`~,......`-...................................../
.............`~.*-,.....................................|,./.....,__
,,_..........}.&gt;-._...................................|..............`=~-,
.....`=~-,__......`,.................................
...................`=~-,,.,...............................
................................`:,,...........................`..............__
.....................................`=-,...................,%`&gt;--==``
........................................_..........._,-%.......`
...................................,
*/
}

bool checkBoundX(const pos &amp;curr, const Direction &amp;moveDir, int maxX) {
  if (((curr.x + getNextMoveStep(moveDir)) &gt;= 0) &amp;&amp; 
      ((curr.x + getNextMoveStep(moveDir)) &lt; maxX)) {
    return true;
  } 
  return false;
  //Aha. Dem muss ich jetzt nicht folgen. 
}

bool checkBoundY(const pos &amp;curr, const Direction &amp;moveDir, int maxY) {
  if (((curr.y + getNextMoveStep(moveDir)) &gt;= 0) &amp;&amp; 
      ((curr.y + getNextMoveStep(moveDir)) &lt; maxY)) {
    return true;
  }
  return false;
  //oder dem
}

bool doMove(pos &amp;curr, const Direction &amp;moveDir, int maxX, int maxY) {
  if ((moveDir == LEFT || moveDir == RIGHT) &amp;&amp; checkBoundX(curr, moveDir, maxX)) {
    curr.x += getNextMoveStep(moveDir);
    return true;
  }
  //oder dem
  if ((moveDir == UP || moveDir == DOWN) &amp;&amp; checkBoundY(curr, moveDir, maxY)) {
    curr.y += getNextMoveStep(moveDir);
    return true;
  }
  return false;
}

bool checkMoveX(Direction &amp;moveDir, const pos &amp;curr, pos end) {
    if (curr.x != end.x) {
      if (curr.x &lt; end.x) {
        moveDir = RIGHT;
      } else {
        moveDir = LEFT;
      }
      //oder dem
      return true;
    }
    return false;
}

bool checkMoveY(Direction &amp;moveDir, const pos &amp;curr, pos end) {
    if (curr.y != end.y) {
      if (curr.y &lt; end.y) {
        moveDir = DOWN;
      } else {
        moveDir = UP;
      }
      //oder dem
      return true;
    }
    return false;
}

bool getMoveDir(Direction &amp;moveDir, const pos &amp;curr, pos end) {
  if(checkMoveX(moveDir, curr, end)) {
    return true;
  }
  if(checkMoveY(moveDir, curr, end)) {
      return true;
  }
  //Panik erfaßt mich. 
  return false;
}

vector&lt;vector&lt;pos&gt;&gt; findAllPaths(vector&lt;vector&lt;int&gt;&gt; &amp;m, pos start, pos end) {
  vector&lt;vector&lt;pos&gt;&gt; allPaths;
  int maxY = m.size();
  int maxX = m[0].size();

  pos curr = start;
  vector&lt;pos&gt; currPath;

  while (curr != end) {
    Direction moveDir = NONE;

    bool canMove = getMoveDir(moveDir, curr, end);
    if (canMove == false) {
    	break;
    }

    doMove(curr, moveDir, maxX, maxY);  
    currPath.push_back(curr);
  }

  allPaths.push_back(currPath);
  return allPaths;
  //Puh, die tut zum Glück nix. 
}

int main() {
  // your code goes here

  vector&lt;vector&lt;int&gt;&gt; m = {{0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0}};

  vector&lt;vector&lt;pos&gt;&gt; allShortestPaths = findAllPaths(m, pos(0,0), pos(2,4));

  for (int i = 0; i &lt; allShortestPaths.size(); i++) {
    for_each(allShortestPaths[i].begin(), allShortestPaths[i].end(), [](pos curr){ cout &lt;&lt; curr.x &lt;&lt; &quot;,&quot; &lt;&lt; curr.y &lt;&lt; &quot; &quot;;});
    //Kein Algo, aber Schleifen unselerlich machen. 
    cout &lt;&lt; endl;
  }

  return 0;
}
</code></pre>
<p>Es ist wirklich viel Code mit sehr vielen Spezialtricks oder Mikrooptimierungen für keinen Algo.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415075</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 27 Aug 2014 10:16:19 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 10:37:56 GMT]]></title><description><![CDATA[<p>gut mal, code aufgeraumt:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stack&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

enum Direction {
  NONE,
  UP,
  DOWN,
  LEFT,
  RIGHT
} direction;

class pos {
public:
  int y;
  int x;
  pos(int y_, int x_): y(y_), x(x_) {}
  bool operator==(const pos &amp;rhs) { 
    return y == rhs.y &amp;&amp; x == rhs.x;
  }
  bool operator!=(const pos &amp;rhs) { 
    return !(y == rhs.y &amp;&amp; x == rhs.x);
  }
  pos &amp;operator=(pos other) {
  	swap(x, other.x);
  	swap(y, other.y);
    return *this;
  }
  pos &amp;operator+(const pos &amp;other) {
    if (this != &amp;other) {
      y += other.x;
      x += other.y;   
    }
    return *this;
  }
};

template &lt;typename T&gt;
void printMatrix(const vector&lt;vector&lt;T&gt;&gt; &amp;m) {
  for(int i = 0; i &lt; m.size(); i++) {
    for(int j = 0; j &lt; m[i].size(); j++) {
      cout &lt;&lt; m[i][j] &lt;&lt; ' ';
    }
    cout &lt;&lt; '\n';
  }
  cout &lt;&lt; '\n';
}

int getNextMoveStep(const Direction &amp;moveDir) {
  if (moveDir == LEFT || moveDir == UP) {
    return -1;
  } else if (moveDir == RIGHT || moveDir == DOWN) {
    return 1;
  }
}

bool checkBoundX(const pos &amp;curr, int maxX) {
  if ((curr.x &gt;= 0) &amp;&amp; 
      (curr.x &lt; maxX)) {
    return true;
  } 
  return false;
}

bool checkBoundY(const pos &amp;curr, int maxY) {
  if ((curr.y &gt;= 0) &amp;&amp; 
      (curr.y &lt; maxY)) {
    return true;
  }
  return false;
}

bool isValidPos(const pos &amp;curr, int maxX, int maxY) {
	if (checkBoundX(curr, maxX) &amp;&amp;  checkBoundY(curr, maxY)) {
		return true;
	}
	return false;
}

bool doMove(pos &amp;curr, const Direction &amp;moveDir, int maxX, int maxY) {
  if (moveDir == LEFT || moveDir == RIGHT) {
    curr.x += getNextMoveStep(moveDir);
    return true;
  }
  if (moveDir == UP || moveDir == DOWN) {
    curr.y += getNextMoveStep(moveDir);
    return true;
  }
  return false;
}

bool checkMoveX(Direction &amp;moveDir, const pos &amp;curr, const pos &amp;end) {
    if (curr.x != end.x) {
      if (curr.x &lt; end.x) {
        moveDir = RIGHT;
      } else {
        moveDir = LEFT;
      }

      return true;
    }
    return false;
}

bool checkMoveY(Direction &amp;moveDir, const pos &amp;curr, const pos &amp;end) {
    if (curr.y != end.y) {
      if (curr.y &lt; end.y) {
        moveDir = DOWN;
      } else {
        moveDir = UP;
      }

      return true;
    }
    return false;
}

bool getMoveDir(Direction &amp;moveDir, const pos &amp;curr, const pos &amp;end) {
  if(checkMoveX(moveDir, curr, end)) {
    return true;
  }
  if(checkMoveY(moveDir, curr, end)) {
      return true;
  }
  return false;
}

bool findAllPaths(vector&lt;vector&lt;int&gt;&gt; &amp;m, const pos &amp;start, const pos &amp;end, vector&lt;vector&lt;pos&gt;&gt; &amp;allPaths) {
  int maxY = m.size();
  int maxX = m[0].size();

  if (!isValidPos(start, maxX, maxY) || 
      !isValidPos(end, maxX, maxY)) {
  	return false;
  }

  pos curr = start;
  vector&lt;pos&gt; currPath;
  currPath.push_back(curr);

  while (curr != end) {
    Direction moveDir = NONE;

    bool canMove = getMoveDir(moveDir, curr, end);
    if (canMove == false) {
    	break;
    }

    doMove(curr, moveDir, maxX, maxY);
    currPath.push_back(curr);
  }

  allPaths.push_back(currPath);
  return true;
}

int main() {
  // your code goes here

  vector&lt;vector&lt;int&gt;&gt; m = {{0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0},
                           {0,0,0,0,0,0,0,0}};

  vector&lt;vector&lt;pos&gt;&gt; allShortestPaths; 

  bool found = findAllPaths(m, pos(0,0), pos(2,4), allShortestPaths);
  if (!found) {
  	cout &lt;&lt; &quot;invalid pos received&quot; &lt;&lt; endl;
  	return 0;
  }  

  for (int i = 0; i &lt; allShortestPaths.size(); i++) {
    for_each(allShortestPaths[i].begin(), allShortestPaths[i].end(), [](pos curr){ cout &lt;&lt; curr.x &lt;&lt; &quot;,&quot; &lt;&lt; curr.y &lt;&lt; &quot; &quot;;});
    cout &lt;&lt; '\n';
  }

  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2415081</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415081</guid><dc:creator><![CDATA[robery]]></dc:creator><pubDate>Wed, 27 Aug 2014 10:37:56 GMT</pubDate></item><item><title><![CDATA[Reply to algorithmen frage zu shortest path on Wed, 27 Aug 2014 13:00:58 GMT]]></title><description><![CDATA[<p><code>dx</code> und <code>dy</code> bestimmen, mit dem Betrag ist klar wie oft und mit dem Vorzeichen in welche Richtung man sich bewegen muss. Von der &quot;einen&quot; Lösungen dann mit <code>next_permutation</code> alle Permutationen durchgehen, fertig.</p>
<p>So in etwa:</p>
<pre><code>int dx = endx - startx, dy = endy - starty;
	vector&lt;direction&gt; moves;

	moves.resize(moves.size() + abs(dx), (dx &gt; 0)? RIGHT : LEFT);
	moves.resize(moves.size() + abs(dy), (dy &gt; 0)? DOWN : UP);

	sort(moves.begin(), moves.end());

	do {
		for (auto m : moves)
			cout &lt;&lt; m &lt;&lt; &quot; &quot;;
		cout &lt;&lt; &quot;\n&quot;;
	} while(next_permutation(moves.begin(), moves.end()));
</code></pre>
<p>(Die Ausgabe kann man noch schöner machen, aber das ist an sich ja auch trivial.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2415099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415099</guid><dc:creator><![CDATA[lagalopex]]></dc:creator><pubDate>Wed, 27 Aug 2014 13:00:58 GMT</pubDate></item></channel></rss>