gpt4 book ai didi

c++ - 通过回溯在迷宫中寻找路径

转载 作者:行者123 更新时间:2023-12-02 10:11:09 25 4
gpt4 key购买 nike

我想制作一个程序,通过回溯来查找迷宫中从右上角到左下角的路径是否存在。输入数字为n和m,它们是矩形迷宫和迷宫的尺寸,字符“。”。表示您可以通过的图块,字符“x”表示您无法通过的图块。我已经编写了代码,它相当简单,但是什么都没有显示,而它应该显示“da”(在塞尔维亚"is")和“ne”(在塞尔维亚“否”)。

#include <bits/stdc++.h>

using namespace std;

bool maze[20][20]; //defined maze of maximum size 20x20

//checking if a position is viable for moving through
bool Safe(int n, int m, int x, int y)
{
if(x >= 0 && x < n && y >= 0 && y < m)
{
if(maze[x][y] == 1) return true;
}
return false;
}

bool Utility(int n, int m, int x, int y) //main utility function
{
if(x == n - 1 && y == m - 1 && maze[x][y] == 1) // base case, end of maze
{
return true;
}

if(Safe(n, m, x, y))
{
if(Safe(n, m, x + 1, y)) // checking if it is viable to move down
{
if(Utility(n, m, x + 1, y))
{
return true;
}
}
if(Safe(n, m, x, y + 1))
{
if(Utility(n, m, x, y + 1)) // checking if it is viable to move right
{
return true;
}
}
if(Safe(n, m, x - 1, y))
{
if(Utility(n, m, x - 1, y)) // checking if it is viable to move up
{
return true;
}
}
if(Safe(n, m, x, y - 1))
{
if(Utility(n, m, x, y - 1)) // checking if it is viable to move left
{
return true;
}
}
}

return false; // returning false
}

int main()
{
int n, m;

cin >> n >> m; // input dimensions of the maze

for(int i = 0; i < n; i++) // input maze
{
for(int j = 0; j < m; j++)
{
char c;
cin >> c;
if(c == '.') //character '.' means a tile which you can go through
{
maze[i][j] = 1;
}
else //character 'x' means a tile which you cannot go through
{
maze[i][j] = 0;
}
}
}

if(Utility(n, m, 0, 0)) //printing yes or no
{
cout << "da";
}
else
{
cout << "ne";
}


return 0;
}
输入样例:
8 8 
.x.....x
.x.x.x.x
.x.x.x.x
.x.x.x.x
.x.x.x.x
.x.x.x.x
.x.x.x.x
...x.x..
样本输出: da

最佳答案

问题是,假设您从(0, 0) -> (1, 0)转到(1, 0),您可以再次返回(0, 0),这将永远循环。为了避免这种情况,我创建了一个visited数组,如果已经访问了单元格true,则它将具有(x, y)值,否则将是false
我已经用///////////// change here /////////////注释标记了在哪里进行更改

#include <bits/stdc++.h>

using namespace std;

bool maze[20][20]; //defined maze of maximum size 20x20
///////////// change here /////////////
bool visited[20][20];

bool Safe(int n, int m, int x, int y) //checking if a position is viable for moving through
{
if(x >= 0 && x < n && y >= 0 && y < m)
{
if(maze[x][y] == 1) return true;
}
return false;
}

bool Utility(int n, int m, int x, int y) //main utility function
{
if(x == n - 1 && y == m - 1 && maze[x][y] == 1) // base case, end of maze
{
return true;
}

///////////// change here /////////////
if(!visited[x][y] && Safe(n, m, x, y))
{
///////////// change here /////////////
visited[x][y] = true;

if(Safe(n, m, x + 1, y)) // checking if it is viable to move down
{
if(Utility(n, m, x + 1, y))
{
return true;
}
}
if(Safe(n, m, x, y + 1))
{
if(Utility(n, m, x, y + 1)) // checking if it is viable to move right
{
return true;
}
}
if(Safe(n, m, x - 1, y))
{
if(Utility(n, m, x - 1, y)) // checking if it is viable to move up
{
return true;
}
}
if(Safe(n, m, x, y - 1))
{
if(Utility(n, m, x, y - 1)) // checking if it is viable to move left
{
return true;
}
}
}

return false; // returning false
}

int main()
{
int n, m;

cin >> n >> m; // input dimensions of the maze

for(int i = 0; i < n; i++) // input maze
{
for(int j = 0; j < m; j++)
{
char c;
cin >> c;
if(c == '.') //character '.' means a tile which you can go through
{
maze[i][j] = true;
}
else //character 'x' means a tile which you cannot go through
{
maze[i][j] = false;
}
///////////// change here /////////////
visited[i][j] = false;
}
}

if(Utility(n, m, 0, 0)) //printing yes or no
{
cout << "da";
}
else
{
cout << "ne";
}


return 0;
}
这是我对其进行测试的链接: https://ideone.com/vVqAjF

关于c++ - 通过回溯在迷宫中寻找路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63534633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com