走迷宫(maze) 难度**

题目描述

有一个 m×n 格的迷宫(表示有 m 行、n 列),其中有可走的也有不可走的,如果用 11 表示可以走,00 表示不可以走。

文件读入这 m×n 个数据和起 始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。

现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应 信息(用-l表示无路)。

输入格式

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

输出格式

所有可行的路径,描述一个点时用 (x,y)的形式,除开始点外,其他的都要用 “一>” 表示方向。 如果没有一条可行的路则输出 -1 。

样例

样例输入

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

样例输出

(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)

来源

搜索

#include<bits/stdc++.h>
using namespace std;
int m,n,a[20][20],endx,endy;
bool no=true;
int dx[4]={0,-1,1,0},dy[4]={-1,0,0,1};
struct Route{
	int x,y;
}route[5001];
void DFS(int x,int y,int num){
	if(x==endx&&y==endy){
		for(int i=0;i<num;i++){
			cout<<"("<<route[i].x<<","<<route[i].y<<")->";
		}
		cout<<"("<<x<<","<<y<<")"<<endl;
		no=false;
		return;
	}
	route[num].x=x,route[num].y=y;
	for(int i=0;i<4;i++){
		if(a[x+dx[i]][y+dy[i]]==1 && 1<=x+dx[i]<=m && 1<=y+dy[i]<=n){
			a[x][y]=0;
			DFS(x+dx[i],y+dy[i],num+1);
			a[x][y]=1;
		}
	}
}
int main(){
	while(cin>>m>>n){
		for(int i=1;i<=m;i++){
			for(int j=1;j<=n;j++){
				cin>>a[i][j];
			}
		}
		int sx,sy;
		cin>>sx>>sy;
		cin>>endx>>endy;
		DFS(sx,sy,0);
		if(no) printf("-1\n");
	}
	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
A*算法是一种基于启发式搜索的寻路算法,常用于解决迷宫寻路问题。下面是一个简单的迷宫寻路A*算法Matlab代码实现: ``` function [path,visited] = A_star(maze,start,goal) % Initialize the open and closed lists open_list = start; closed_list = []; % Initialize the g and h scores of the start node start.g = 0; start.h = heuristic(start,goal); while ~isempty(open_list) % Find the node with the lowest f score in the open list current_node = open_list(1); for i = 2:length(open_list) if open_list(i).f < current_node.f current_node = open_list(i); end end % If the goal is reached, return the path and visited nodes if isequal(current_node,goal) path = reconstruct_path(current_node); visited = closed_list; return; end % Move the current node from open to closed list open_list(open_list == current_node) = []; closed_list(end+1) = current_node; % Generate successor nodes successors = generate_successors(current_node,maze); for i = 1:length(successors) successor = successors(i); % If the successor is already in the closed list, skip it if any(ismember(closed_list,successor,'rows')) continue; end % Calculate the tentative g score tentative_g = current_node.g + 1; % If the successor is not in the open list, add it and calculate its f and g scores if ~any(ismember(open_list,successor,'rows')) successor.g = tentative_g; successor.h = heuristic(successor,goal); successor.f = successor.g + successor.h; successor.parent = current_node; open_list(end+1) = successor; else % If the successor is already in the open list, update its g score if the tentative g score is lower index = find(ismember(open_list,successor,'rows')); if tentative_g < open_list(index).g open_list(index).g = tentative_g; open_list(index).f = open_list(index).g + open_list(index).h; open_list(index).parent = current_node; end end end end % If no path is found, return an empty path and visited list path = []; visited = []; end function successors = generate_successors(node,maze) [row,col] = size(maze); successors = []; % Generate all possible successor nodes for i = -1:1 for j = -1:1 if abs(i) == abs(j) || node.x+i < 1 || node.x+i > row || node.y+j < 1 || node.y+j > col || maze(node.x+i,node.y+j) == 1 continue; end successors(end+1,:) = [node.x+i,node.y+j]; end end % Convert the successor nodes to node objects and set their parent nodes for i = 1:size(successors,1) successors(i) = Node(successors(i,1),successors(i,2)); successors(i).parent = node; end end function h = heuristic(node,goal) % Calculate the Manhattan distance between the node and the goal h = abs(node.x - goal.x) + abs(node.y - goal.y); end function path = reconstruct_path(node) % Reconstruct the path from the goal node to the start node path = [node.x,node.y]; while ~isempty(node.parent) node = node.parent; path(end+1,:) = [node.x,node.y]; end % Reverse the path to get it from start to goal path = flipud(path); end classdef Node % Node class to represent a node in the A* search algorithm properties x % X-coordinate of the node in the maze y % Y-coordinate of the node in the maze g % Cost of the path from the start node to this node h % Heuristic cost of this node to the goal node f % Estimated total cost of this node (f = g + h) parent % Parent node of this node in the search tree end methods function obj = Node(x,y) obj.x = x; obj.y = y; end end end ``` 使用该代码实现A*算法的迷宫寻路,需要提供一个二维的maze矩阵,其中0表示可通过的空间,1表示障碍物。同时还需要提供起点start和终点goal。函数会返回最短路径path和访问过的节点visited。你可以根据需要对其进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值