英文:
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 <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 taken matrix of vector<vector<int>>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 < size
而不是srcy < size - 1
。没有理由为什么你会针对 x 和 y 坐标以不同的方式执行条件。因此,将srcx
的条件与此模式对齐。 -
在函数头部,
size
参数出现在srcx
和srcy
参数之前,但在函数调用中,你将size
作为最后一个参数传递。 -
if (<boolean expr>) return true; else return false;
结构添加了一个不必要的执行层。只需返回布尔表达式。
以下是更正后的内容:
bool isSafe(vector<vector<int>> &matrix, vector<vector<int>> &visited,
int srcx, int srcy, int size)
{
return srcx >= 0 && srcy >= 0 && srcy < size && srcx < size
&& matrix[srcx][srcy] == 1 && !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 < size - 1
you should havesrcy < size
. There is no good reason why you would perform the conditions differently for x and y coordinate. So align thesrcx
condition to this pattern. -
In the function header, the
size
parameter comes before thesrcx
andsrcy
parameters, but in the function calls you passsize
as the last argument. -
The
if (<boolean expr>) 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<vector<int>> &matrix, vector<vector<int>> &visited,
int srcx, int srcy, int size)
{
return srcx >= 0 && srcy >= 0 && srcy < size && srcx < size
&& matrix[srcx][srcy] == 1 && !visited[srcx][srcy];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论