老鼠在迷宫问题没有打印解决方案。

huangapple go评论66阅读模式
英文:

Rat in a maze problem not printing the solution

问题

I am trying to solve the backtracking problem rat in a maze, but it's not printing the solution. Please look into the code and help me out. The code is below:

#include <bits/stdc++.h>
using namespace std;

vector<string> ans;

bool isSafe(vector<vector<int>> &matrix, vector<vector<int>> &visited, int size, int srcx, int srcy)
{
    if (srcx >= 0 && srcy >= 0 && srcy < size && srcx < size && matrix[srcx][srcy] == 1 && !visited[srcx][srcy])
    {
        return true;
    }
    else
    {
        return false;
    }
}

void findPathUtiltity(vector<vector<int>> &matrix, vector<vector<int>> &visited, int size, int srcx, int srcy, string temp)
{
    // check if we are at (3,3)
    if (srcx == size - 1 && srcy == size - 1)
    {
        ans.push_back(temp);
        return;
    }
    // mark visited as true
    visited[srcx][srcy] = 1;
    // DOWN
    if (isSafe(matrix, visited, srcx + 1, srcy, size))
    {
        findPathUtiltity(matrix, visited, size, srcx + 1, srcy, temp + "D");
    }
    // LEFT
    if (isSafe(matrix, visited, srcx, srcy - 1, size))
    {
        findPathUtiltity(matrix, visited, size, srcx, srcy - 1, temp + "L");
    }
    // Right
    if (isSafe(matrix, visited, srcx, srcy + 1, size))
    {
        findPathUtiltity(matrix, visited, size, srcx, srcy + 1, temp + "R");
        // UP
        if (isSafe(matrix, visited, srcx - 1, srcy, size))
        {
            findPathUtiltity(matrix, visited, size, srcx - 1, srcy, temp + "U");
        }
    }
    // mark all 0 to backtrack and check all other path
    visited[srcx][srcy] = 0;
    return;
}

vector<string> findPath(vector<vector<int>> &matrix, int size)
{
    if (matrix[0][0] == 0)
    {
        return ans;
    }

    vector<vector<int>> visited(size, vector<int>(size, 0));
    findPathUtiltity(matrix, visited, size, 0, 0, "");
    return ans;
}

int main()
{

    vector<vector<int>> matrix = {
        {1, 0, 0, 0, 0},
        {1, 1, 1, 1, 1},
        {1, 1, 1, 0, 1},
        {0, 0, 0, 0, 1},
        {0, 0, 0, 0, 1}};
    vector<string> path = findPath(matrix, matrix.size());

    for (int i = 0; i < path.size(); i++)
    {
        cout << path[i] << " ";
    }

    return 0;
}

I am trying to solve the backtracking problem rat in a maze, but it's not printing the solution. I have used a matrix of vectors for this. When I try to dry run it, I get the correct output. Please look into the code and help me out.

英文:

I am trying to solve the backtracking problem rat in a maze, but it's not printing the solution, Please look into code and help me out.

The code is below:

#include &lt;bits/stdc++.h&gt;
using namespace std;
vector&lt;string&gt; ans;
bool isSafe(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, vector&lt;vector&lt;int&gt;&gt; &amp;visited, int size, int srcx, int srcy)
{
if (srcx &gt;= 0 &amp;&amp; srcy &gt;= 0 &amp;&amp; srcy &lt; size &amp;&amp; srcx &lt; size &amp;&amp; matrix[srcx][srcy] == 1 &amp;&amp; !visited[srcx][srcy])
{
return true;
}
else
{
return false;
}
}
void findPathUtiltity(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, vector&lt;vector&lt;int&gt;&gt; &amp;visited, int size, int srcx, int srcy, string temp)
{
// check if we are at (3,3)
if (srcx == size - 1 &amp;&amp; srcy == size - 1)
{
ans.push_back(temp);
return;
}
// mark visited as true
visited[srcx][srcy] = 1;
// DOWN
if (isSafe(matrix, visited, srcx + 1, srcy, size))
{
findPathUtiltity(matrix, visited, size, srcx + 1, srcy, temp + &quot;D&quot;);
}
// LEFT
if (isSafe(matrix, visited, srcx, srcy - 1, size))
{
findPathUtiltity(matrix, visited, size, srcx, srcy - 1, temp + &quot;L&quot;);
}
// Right
if (isSafe(matrix, visited, srcx, srcy + 1, size))
{
findPathUtiltity(matrix, visited, size, srcx, srcy + 1, temp + &quot;R&quot;);
// UP
if (isSafe(matrix, visited, srcx - 1, srcy, size))
{
findPathUtiltity(matrix, visited, size, srcx - 1, srcy, temp + &quot;U&quot;);
}
}
// mark all 0 to backtrack and check all other path
visited[srcx][srcy] = 0;
return;
}
vector&lt;string&gt; findPath(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, int size)
{
if (matrix[0][0] == 0)
{
return ans;
}
vector&lt;vector&lt;int&gt;&gt; visited(size, vector&lt;int&gt;(size, 0));
findPathUtiltity(matrix, visited, size, 0, 0, &quot;&quot;);
return ans;
}
int main()
{
vector&lt;vector&lt;int&gt;&gt; matrix = {
{1, 0, 0, 0, 0},
{1, 1, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 0, 0, 0, 1},
{0, 0, 0, 0, 1}};
vector&lt;string&gt; path = findPath(matrix, matrix.size());
for (int i = 0; i &lt; path.size(); i++)
{
cout &lt;&lt; path[i] &lt;&lt; &quot; &quot;;
}
return 0;
}

I am trying to solve the backtracking problem rat in a maze, but it's not printing the solution, i taken matrix of vector&lt;vector&lt;int&gt;&gt;matrix; . I try to DRY run there i'm getting correct output. Please look into code and help me out.

答案1

得分: 1

以下是翻译好的内容:

有几个问题,都与 isSafe 函数有关:

  • 对 y 坐标的测试过于严格,永远不会允许路径达到迷宫的右下角。应该使用 srcy &lt; size 而不是 srcy &lt; size - 1。没有理由为什么你会针对 x 和 y 坐标以不同的方式执行条件。因此,将 srcx 的条件与此模式对齐。

  • 在函数头部,size 参数出现在 srcxsrcy 参数之前,但在函数调用中,你将 size 作为最后一个参数传递。

  • if (&lt;boolean expr&gt;) return true; else return false; 结构添加了一个不必要的执行层。只需返回布尔表达式。

以下是更正后的内容:

bool isSafe(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, vector&lt;vector&lt;int&gt;&gt; &amp;visited, 
            int srcx, int srcy, int size)
{
    return srcx &gt;= 0 &amp;&amp; srcy &gt;= 0 &amp;&amp; srcy &lt; size &amp;&amp; srcx &lt; size 
                     &amp;&amp; matrix[srcx][srcy] == 1 &amp;&amp; !visited[srcx][srcy];
}
英文:

There are several issues; all related to the isSafe function:

  • The test for the y-coordinate is too strict and will never allow the path to reach the bottom-right corner of the maze. Instead of srcy &lt; size - 1 you should have srcy &lt; size. There is no good reason why you would perform the conditions differently for x and y coordinate. So align the srcx condition to this pattern.

  • In the function header, the size parameter comes before the srcx and srcy parameters, but in the function calls you pass size as the last argument.

  • The if (&lt;boolean expr&gt;) return true; else return false; construct is adding a layer of execution that is unnecessary. Just return the boolean expression.

Here is the correction:

bool isSafe(vector&lt;vector&lt;int&gt;&gt; &amp;matrix, vector&lt;vector&lt;int&gt;&gt; &amp;visited, 
int srcx, int srcy, int size)
{
return srcx &gt;= 0 &amp;&amp; srcy &gt;= 0 &amp;&amp; srcy &lt; size &amp;&amp; srcx &lt; size 
&amp;&amp; matrix[srcx][srcy] == 1 &amp;&amp; !visited[srcx][srcy];
}

huangapple
  • 本文由 发表于 2023年3月31日 02:30:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定