POJ 3984 迷宫问题 (路径记录)

K - 迷宫问题

Crawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit
Status

Description

定义一个二维数组:

int maze[5][5] = {

 0, 1, 0, 0, 0,

 0, 1, 0, 1, 0,

 0, 0, 0, 0, 0,

 0, 1, 1, 1, 0,

 0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

刚学的记录路径。

不过BFS的部分不是我以前那种写法,还是感觉有点生涩。

其他大体上差不多吧。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int map[6][6];
int rear=1;
int front=0;
struct node
{
	int x,y;
	int pre;
}
;
node q[36];
void output(int i)
{
	if(q[i].pre!=-1)
	{
		output(q[i].pre);//用递归输出路径。
		printf("(%d, %d)\n",q[i].x,q[i].y);
	}
}
void bfs(int x,int y)
{
	int i;
	q[front].x=x;
	q[front].y=y;
	q[front].pre=-1;
	map[x][y]=1;
	while(front<rear)
	{
		for(i=0;i<4;i++)
		{
			int nx=q[front].x+dir[i][0];
			int ny=q[front].y+dir[i][1];
			if(nx>=5 ||nx<0 ||ny>=5 ||ny<0 ||map[nx][ny])
				continue;
			map[nx][ny]=1;
			q[rear].x=nx;
			q[rear].y=ny;
			q[rear++].pre=front;//入队+记录路径。
			if(nx==4 &&ny==4)
				output(front);
		}
		front++;//出队
	}
}
int main()
{
    int i,j;
	for(i=0;i<5;i++)
		for(j=0;j<5;j++)
			scanf("%d",&map[i][j]);
		printf("(0, 0)\n");
		bfs(0,0);
		printf("(4, 4)\n");
}
时间: 2024-08-24 09:03:06

POJ 3984 迷宫问题 (路径记录)的相关文章

BFS(最短路+路径打印) POJ 3984 迷宫问题

题目传送门 1 /* 2 BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. 3 dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-4 9:02:06 8 File Name :POJ_3984.cpp 9 ********************

POJ 3984 迷宫问题 搜索题解

本题可以使用BFS和DFS解题,也可以构建图,然后利用Dijsktra解题. 不过因为数据很少,就没必要使用Dijsktra了. BFS和DFS效率都是一样的,因为都需要搜索所有可能的路径,并记录最短路径和当前路径. 推荐使用DFS,感觉会方便很多,BFS会麻烦很多,因为需要记录并比较路径. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 5; const int

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

POJ 3984 迷宫问题(简单bfs+路径打印)

传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33105   Accepted: 18884 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个

迷宫bfs+路径记录

给定5*5的地图,1表示墙,0表示空地,从左上角走到左下角 是把所有的可走路径都记录下来了,但是 搜索有递归与非递归形式 本代码使用非递归形式 bfs+路径记录 对于num[i].pre=head的理解是他存在很多条路,每个点是从上一个点走过来的,但总存在一条路是到达终点的,所以,只需要得到到终点的一个head就可以顺着这个路径标记回去了 #include <iostream> #include <cstdio> using namespace std; char a[10][10

poj 3984 迷宫问题 (BFS+记录路径)

题目连接:http://poj.org/problem?id=3984 题解:简单的BFS+记录路径,具体题解看代码注释. #include <iostream> #include <queue> #include <cstdio> using namespace std; struct point { int x; int y; }; queue<point>q; int map[5][5]; int vis[5][5];//标记走过的路 int g[4]

POJ 3984:迷宫问题(BFS+路径记录)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7560   Accepted: 4426 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

POJ 3984 迷宫问题【BFS/路径记录】

迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编

poj(3984)——迷宫问题(输出路径)

题目的大致意思是:给你一个5*5的迷宫,然后你只能往上,下,左,右四个方向走,然后0代表的是可以走的路,1代表的是墙不可以走.然后让我们求出从左上角到右下角的最短路线及其长度. 求长度是好做的,但是输出路径这个我还是第一次碰到. 这里我们使用的队列不可以是STL中的queue了,要用数组来写,因为我们在这里需要头尾两个指针. 然后我们这里还要用到一个保存前驱节点的数组pre,这样在我们输出路径的时候就可以回溯上去. #include<stdio.h> #include<string.h&