<?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[maze solving algo]]></title><description><![CDATA[<p>Hi,</p>
<blockquote>
<p>kann mir jemand kurzes feedback zu meinem maze solving algo geben? ich weiss man koennte auch sowas wie A* implementieren... ich hatte mich fuer BFS entschieden um auch den shortest path zu bekommen...</p>
<p>beschreibung:<br />
A Maze is given as M*N binary matrix of blocks with a source block a and destination block. A rat starts from source and has to reach destination. The rat can move only in two directions: left, right, up and down.<br />
In the maze matrix, 0 means the block is dead end and 1 means the block can be used in the path from source to destination.</p>
</blockquote>
<p>#include &lt;iostream&gt;<br />
#include &lt;string&gt;<br />
#include &lt;cassert&gt;<br />
#include &lt;vector&gt;<br />
#include &lt;queue&gt;<br />
using namespace std;</p>
<p>class pos {<br />
public:<br />
int row;<br />
int col;<br />
pos(): row(0), col(0) {}<br />
pos(int r, int c): row(r), col(c) {}<br />
pos &amp;operator=(pos p) {<br />
swap(row, p.row);<br />
swap(col, p.col);<br />
return *this;<br />
}<br />
pos operator+(const pos p) {<br />
pos result = *this;<br />
result.row += p.row;<br />
result.col += p.col;<br />
return result;<br />
}<br />
bool operator==(const pos &amp;p) {<br />
return row == p.row &amp;&amp; col == p.col;<br />
}<br />
};</p>
<p>void print(vector&lt;vector&lt;int&gt;&gt; &amp;vec) {<br />
for (int i = 0; i &lt; vec.size(); i++) {<br />
for (int j = 0; j &lt; vec[0].size(); j++) {<br />
cout &lt;&lt; vec[i][j] &lt;&lt; &quot; &quot;;<br />
}<br />
cout &lt;&lt; '\n';<br />
}<br />
}</p>
<p>bool check_boundary(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos curr) {<br />
if (curr.row &lt; 0 || (curr.row &gt;= vec.size())) {<br />
return false;<br />
}<br />
if (curr.col &lt; 0 || (curr.col &gt;= vec[0].size())) {<br />
return false;<br />
}</p>
<p>return true;<br />
}</p>
<p>bool is_valid_move(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos curr) {<br />
if (!check_boundary(vec, curr)) {<br />
return false;<br />
}</p>
<p>if (vec[curr.row][curr.col] == 0) {<br />
return false;<br />
}</p>
<p>return true;<br />
}</p>
<p>void cleanup_path(vector&lt;pos&gt; &amp;path, pos curr, pos start) {<br />
if (path.size() &lt; 2) {<br />
return;<br />
}</p>
<p>pos prev_pos = curr;<br />
auto it = path.end() - 2;</p>
<p>while (it != path.begin()) {<br />
bool can_move = false;<br />
vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};</p>
<p>for (auto &amp;m: moveVec) {<br />
pos next_pos = prev_pos + m;</p>
<p>if (*it == next_pos) {<br />
can_move = true;<br />
}<br />
}</p>
<p>if (!can_move) {<br />
auto next = it-1;<br />
path.erase(it);<br />
it = next;<br />
}<br />
else {<br />
prev_pos = *it;<br />
it--;<br />
}<br />
}<br />
}</p>
<p>bool bfs(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos start, pos dest, vector&lt;pos&gt; &amp;path) {<br />
queue&lt;pos&gt; q;<br />
q.push(start);</p>
<p>while (!q.empty()) {<br />
pos curr = q.front();<br />
q.pop();</p>
<p>visited[curr.row][curr.col] = 1;</p>
<p>path.push_back(curr);</p>
<p>if (curr == dest) {<br />
cleanup_path(path, curr, start);<br />
return true;<br />
}</p>
<p>bool can_move = false;<br />
// left right up down<br />
vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};<br />
for (auto &amp;m: moveVec) {<br />
pos next_pos = curr + m;</p>
<p>if (is_valid_move(vec, visited, next_pos)) {<br />
can_move = true;<br />
q.push(next_pos);<br />
}<br />
}</p>
<p>if (!can_move) {<br />
//cout &lt;&lt; &quot;pop_back&quot; &lt;&lt; endl;<br />
path.pop_back();<br />
visited[curr.row][curr.col] = 0;<br />
}<br />
}</p>
<p>return false;<br />
}</p>
<p>bool find_shortest_path(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos start, pos dest) {<br />
vector&lt;pos&gt; path;<br />
vector&lt;vector&lt;int&gt;&gt; visited;<br />
visited.resize(vec.size(), vector&lt;int&gt;(vec[0].size(), 0));</p>
<p>bool res = bfs(vec, visited, start, dest, path);<br />
for (auto &amp;p: path) {<br />
cout &lt;&lt; &quot;row: &quot; &lt;&lt; p.row &lt;&lt; &quot;,col: &quot; &lt;&lt; p.col &lt;&lt; endl;<br />
}<br />
return res;<br />
}</p>
<p>int main() {</p>
<p>vector&lt;vector&lt;int&gt;&gt; vec = {{1,0,1,1,1},<br />
{1,1,1,0,1},<br />
{1,0,0,1,1},<br />
{1,0,0,1,0},<br />
{1,0,0,1,1}};</p>
<p>cout &lt;&lt; find_shortest_path(vec, pos(0,0), pos(4,4)) &lt;&lt; endl;</p>
<p>return 0;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/335845/maze-solving-algo</link><generator>RSS for Node</generator><lastBuildDate>Mon, 20 Apr 2026 03:50:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/335845.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 15 Dec 2015 10:42:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to maze solving algo on Tue, 15 Dec 2015 10:42:24 GMT]]></title><description><![CDATA[<p>Hi,</p>
<blockquote>
<p>kann mir jemand kurzes feedback zu meinem maze solving algo geben? ich weiss man koennte auch sowas wie A* implementieren... ich hatte mich fuer BFS entschieden um auch den shortest path zu bekommen...</p>
<p>beschreibung:<br />
A Maze is given as M*N binary matrix of blocks with a source block a and destination block. A rat starts from source and has to reach destination. The rat can move only in two directions: left, right, up and down.<br />
In the maze matrix, 0 means the block is dead end and 1 means the block can be used in the path from source to destination.</p>
</blockquote>
<p>#include &lt;iostream&gt;<br />
#include &lt;string&gt;<br />
#include &lt;cassert&gt;<br />
#include &lt;vector&gt;<br />
#include &lt;queue&gt;<br />
using namespace std;</p>
<p>class pos {<br />
public:<br />
int row;<br />
int col;<br />
pos(): row(0), col(0) {}<br />
pos(int r, int c): row(r), col(c) {}<br />
pos &amp;operator=(pos p) {<br />
swap(row, p.row);<br />
swap(col, p.col);<br />
return *this;<br />
}<br />
pos operator+(const pos p) {<br />
pos result = *this;<br />
result.row += p.row;<br />
result.col += p.col;<br />
return result;<br />
}<br />
bool operator==(const pos &amp;p) {<br />
return row == p.row &amp;&amp; col == p.col;<br />
}<br />
};</p>
<p>void print(vector&lt;vector&lt;int&gt;&gt; &amp;vec) {<br />
for (int i = 0; i &lt; vec.size(); i++) {<br />
for (int j = 0; j &lt; vec[0].size(); j++) {<br />
cout &lt;&lt; vec[i][j] &lt;&lt; &quot; &quot;;<br />
}<br />
cout &lt;&lt; '\n';<br />
}<br />
}</p>
<p>bool check_boundary(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos curr) {<br />
if (curr.row &lt; 0 || (curr.row &gt;= vec.size())) {<br />
return false;<br />
}<br />
if (curr.col &lt; 0 || (curr.col &gt;= vec[0].size())) {<br />
return false;<br />
}</p>
<p>return true;<br />
}</p>
<p>bool is_valid_move(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos curr) {<br />
if (!check_boundary(vec, curr)) {<br />
return false;<br />
}</p>
<p>if (vec[curr.row][curr.col] == 0) {<br />
return false;<br />
}</p>
<p>return true;<br />
}</p>
<p>void cleanup_path(vector&lt;pos&gt; &amp;path, pos curr, pos start) {<br />
if (path.size() &lt; 2) {<br />
return;<br />
}</p>
<p>pos prev_pos = curr;<br />
auto it = path.end() - 2;</p>
<p>while (it != path.begin()) {<br />
bool can_move = false;<br />
vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};</p>
<p>for (auto &amp;m: moveVec) {<br />
pos next_pos = prev_pos + m;</p>
<p>if (*it == next_pos) {<br />
can_move = true;<br />
}<br />
}</p>
<p>if (!can_move) {<br />
auto next = it-1;<br />
path.erase(it);<br />
it = next;<br />
}<br />
else {<br />
prev_pos = *it;<br />
it--;<br />
}<br />
}<br />
}</p>
<p>bool bfs(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos start, pos dest, vector&lt;pos&gt; &amp;path) {<br />
queue&lt;pos&gt; q;<br />
q.push(start);</p>
<p>while (!q.empty()) {<br />
pos curr = q.front();<br />
q.pop();</p>
<p>visited[curr.row][curr.col] = 1;</p>
<p>path.push_back(curr);</p>
<p>if (curr == dest) {<br />
cleanup_path(path, curr, start);<br />
return true;<br />
}</p>
<p>bool can_move = false;<br />
// left right up down<br />
vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};<br />
for (auto &amp;m: moveVec) {<br />
pos next_pos = curr + m;</p>
<p>if (is_valid_move(vec, visited, next_pos)) {<br />
can_move = true;<br />
q.push(next_pos);<br />
}<br />
}</p>
<p>if (!can_move) {<br />
//cout &lt;&lt; &quot;pop_back&quot; &lt;&lt; endl;<br />
path.pop_back();<br />
visited[curr.row][curr.col] = 0;<br />
}<br />
}</p>
<p>return false;<br />
}</p>
<p>bool find_shortest_path(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos start, pos dest) {<br />
vector&lt;pos&gt; path;<br />
vector&lt;vector&lt;int&gt;&gt; visited;<br />
visited.resize(vec.size(), vector&lt;int&gt;(vec[0].size(), 0));</p>
<p>bool res = bfs(vec, visited, start, dest, path);<br />
for (auto &amp;p: path) {<br />
cout &lt;&lt; &quot;row: &quot; &lt;&lt; p.row &lt;&lt; &quot;,col: &quot; &lt;&lt; p.col &lt;&lt; endl;<br />
}<br />
return res;<br />
}</p>
<p>int main() {</p>
<p>vector&lt;vector&lt;int&gt;&gt; vec = {{1,0,1,1,1},<br />
{1,1,1,0,1},<br />
{1,0,0,1,1},<br />
{1,0,0,1,0},<br />
{1,0,0,1,1}};</p>
<p>cout &lt;&lt; find_shortest_path(vec, pos(0,0), pos(4,4)) &lt;&lt; endl;</p>
<p>return 0;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2479606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2479606</guid><dc:creator><![CDATA[timmyy]]></dc:creator><pubDate>Tue, 15 Dec 2015 10:42:24 GMT</pubDate></item><item><title><![CDATA[Reply to maze solving algo on Tue, 15 Dec 2015 10:44:32 GMT]]></title><description><![CDATA[<p>nun mit code formatierung:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cassert&gt;
#include &lt;vector&gt;
#include &lt;queue&gt;
using namespace std;

class pos {
public:
    int row;
    int col;
    pos(): row(0), col(0) {}
    pos(int r, int c): row(r), col(c) {}
    pos &amp;operator=(pos p) {
        swap(row, p.row);
	swap(col, p.col);
        return *this;
    }
    pos operator+(const pos p) {
        pos result = *this;
    	result.row += p.row;
    	result.col += p.col;
    	return result;
    }
    bool operator==(const pos &amp;p) {
        return row == p.row &amp;&amp; col == p.col;
    }
};

void print(vector&lt;vector&lt;int&gt;&gt; &amp;vec) {
    for (int i = 0; i &lt; vec.size(); i++) {
        for (int j = 0; j &lt; vec[0].size(); j++) {
            cout &lt;&lt; vec[i][j] &lt;&lt; &quot; &quot;;
        }
        cout &lt;&lt; '\n';
    }
}

bool check_boundary(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos curr) {
    if (curr.row &lt; 0 || (curr.row &gt;= vec.size())) {
        return false;
    }
    if (curr.col &lt; 0 || (curr.col &gt;= vec[0].size())) {
        return false;
    }

    return true;
}

bool is_valid_move(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos curr) {
    if (!check_boundary(vec, curr)) {
        return false;
    }

    if (vec[curr.row][curr.col] == 0) {
        return false;
    }

    return true;
}

void cleanup_path(vector&lt;pos&gt; &amp;path, pos curr, pos start) {
    if (path.size() &lt; 2) {
    	return;
    }

    pos prev_pos = curr;
    auto it = path.end() - 2;

    while (it != path.begin()) {
        bool can_move = false;
        vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};

        for (auto &amp;m: moveVec) {
            pos next_pos = prev_pos + m;

            if (*it == next_pos) {
                can_move = true;
            }
        }

        if (!can_move) {
            auto next = it-1;
            path.erase(it);
            it = next;
        }
        else {
            prev_pos = *it;
            it--;
        }
    }
}

bool bfs(vector&lt;vector&lt;int&gt;&gt; &amp;vec, vector&lt;vector&lt;int&gt;&gt; &amp;visited, pos start, pos dest, vector&lt;pos&gt; &amp;path) {
    queue&lt;pos&gt; q;
    q.push(start);

    while (!q.empty()) {
        pos curr = q.front();
        q.pop();

        if (visited[curr.row][curr.col] == 1) {
            continue;
        }
        visited[curr.row][curr.col] = 1;

        path.push_back(curr);

        if (curr == dest) {
            cleanup_path(path, curr, start);
            return true;
        }

        bool can_move = false;
                              // left     right     up         down
        vector&lt;pos&gt; moveVec = {pos(0,-1), pos(0,1), pos(-1,0), pos(1,0)};
        for (auto &amp;m: moveVec) {
            pos next_pos = curr + m;

            if (is_valid_move(vec, visited, next_pos)) {
                can_move = true;
                q.push(next_pos);
            }
        }

        if (!can_move) { 
            //cout &lt;&lt; &quot;pop_back&quot; &lt;&lt; endl;
            path.pop_back();
            visited[curr.row][curr.col] = 0;
        }
    }

    return false;
}

bool find_shortest_path(vector&lt;vector&lt;int&gt;&gt; &amp;vec, pos start, pos dest) {
    vector&lt;pos&gt; path;
    vector&lt;vector&lt;int&gt;&gt; visited;
    visited.resize(vec.size(), vector&lt;int&gt;(vec[0].size(), 0));

    bool res = bfs(vec, visited, start, dest, path);
    for (auto &amp;p: path) {
        cout &lt;&lt; &quot;row: &quot; &lt;&lt; p.row &lt;&lt; &quot;,col: &quot; &lt;&lt; p.col &lt;&lt; endl;
    }
    return res;
}

int main() {

    vector&lt;vector&lt;int&gt;&gt; vec = {{1,0,1,1,1}, 
     			       {1,1,1,0,1}, 
     			       {1,0,0,1,1},
			       {1,0,0,1,0},
			       {1,0,0,1,1}};

    cout &lt;&lt; find_shortest_path(vec, pos(0,0), pos(4,2)) &lt;&lt; endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2479607</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2479607</guid><dc:creator><![CDATA[timmyy]]></dc:creator><pubDate>Tue, 15 Dec 2015 10:44:32 GMT</pubDate></item></channel></rss>