输出迷宫的所有路径 dfs

有一个nm格的迷宫(表示有n行、m列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这nm个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-l表示无路)。

第一行是两个数n,m( 1 < n , m < 15 ),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点
输入:
5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6

学深搜好累,自己写了一下

#include<iostream>
#include<vector>
using namespace std;

struct Point
{
	int px;
	int py;
};

int maze[20][20];
int r, c;
int dic[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0,1}};
int sx, sy;
int fx, fy;
int ans;
bool vis[20][20];
vector<Point> vec;

bool in(int x, int y)
{
	return x > 0 && y > 0 && x <= r && y <= c;
}

void Search(int x, int y)
{
	if(x == fx && y == fy){
		for(int i = 0; i < vec.size(); i++){
			cout << '(' << vec[i].px << ',' << vec[i].py << ')' << "->";
		}
		cout << '(' << fx << ',' << fy << ')' << endl;
		ans++;
		return;
	}
	
	
	for(int i = 0; i < 4; i++){
		int tx = x + dic[i][0];
		int ty = y + dic[i][1];
		if(in(tx, ty) && maze[tx][ty] && !vis[tx][ty]){
			vis[x][y] = true;
			Point a;
			a.px = x;
			a.py = y;
			vec.push_back(a);
			Search(tx, ty);
			vis[x][y] = false;
			vec.pop_back();
		}
	}
	return;
}

int main()
{
	cin >> r >> c;
	for(int i = 1; i <= r; i++){
		for(int j = 1; j <= c; j++)
			cin >> maze[i][j];
	}
	cin >> sx >> sy;
	cin >> fx >> fy;
	
	Search(sx, sy);
	
	if(ans == 0)
		cout << -1;
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,我可以提供一个简单的深度优先搜索(DFS算法来解决迷宫问题,并输出所有可行的路径。以下是基本的 Python 代码实现: ```python # 定义一个函数,输入迷宫矩阵maze,起点位置start,终点位置end def solveMaze(maze, start, end): # 定义一个空列表用于存储所有路径 allPaths = [] # 定义一个列表用于存储当前路径 currentPath = [] # 调用DFS函数进行搜索,并将结果存储在allPaths中 DFS(maze, start, end, currentPath, allPaths) # 返回搜索结果 return allPaths # 定义深度优先搜索函数DFS def DFS(maze, current, end, currentPath, allPaths): # 将当前位置加入当前路径 currentPath.append(current) # 如果当前位置等于终点位置,则将当前路径加入所有路径 if current == end: allPaths.append(currentPath.copy()) else: # 否则,遍历当前位置的相邻节点 for neighbor in getNeighbors(maze, current): # 如果相邻节点不在当前路径中,则递归调用DFS函数 if neighbor not in currentPath: DFS(maze, neighbor, end, currentPath, allPaths) # 将当前位置从当前路径中删除 currentPath.pop() # 定义一个函数,获取迷宫中某个位置的相邻节点 def getNeighbors(maze, current): neighbors = [] row, col = current # 检查上下左右四个方向的相邻节点 if row > 0 and maze[row-1][col] == 0: neighbors.append((row-1, col)) if row < len(maze)-1 and maze[row+1][col] == 0: neighbors.append((row+1, col)) if col > 0 and maze[row][col-1] == 0: neighbors.append((row, col-1)) if col < len(maze[0])-1 and maze[row][col+1] == 0: neighbors.append((row, col+1)) # 返回所有可行的相邻节点 return neighbors ``` 上述代码中,`solveMaze` 函数接收一个迷宫矩阵 `maze`,起点位置 `start` 和终点位置 `end`,并返回所有可行的路径。其中,`DFS` 函数使用深度优先搜索算法进行迷宫的遍历,并将所有可行的路径存储在 `allPaths` 列表中。`getNeighbors` 函数用于获取某个位置的相邻节点,以便进行搜索。 你可以将上述代码复制到 Python 环境中运行,或者将其集成到你的项目中,以实现在迷宫输出所有路径的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值