poj 3984 迷宫问题

做这道题其实我是想看看打印路径的方法,挺水的,看了网上别人的代码,打印路径基本上都是保存一下父节点,然后

打印出它的坐标,我写的是递归一下,从后面向前找到起点,找的过程有点不一样,之前没保存,而是继续上下左右的

找,估计是因为这个时间有点多,虽然在这道题上看不出来差别。。。无意中看到了盈神的博客,我做这道题将近比她晚了一年。。。

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> 
int dist[10][10];
int visit[10][10];
int gra[10][10];
const int dx[4] = {-1,1,0,0};
const int dy[4] = {0,0,-1,1};
using namespace std;
struct node
{
	int x,y;
};
void bfs(int x,int y)
{
	int i;
	queue<node>que;
	memset(visit,0,sizeof(visit));
	memset(dist,0,sizeof(dist));
	node n1;
	n1.x = x;
	n1.y = y;
	dist[x][y] = 0;
	que.push(n1);
	visit[x][y] = 1;
	while(!que.empty())
	{
		node n1 = que.front();
		que.pop();
		if((n1.x == 5) && (n1.y == 5))
		{
			//printf("%d\n",dist[n1.x][n1.y]);
			return ;
		}
		for(i=0; i<4; i++)
		{
			int xx = n1.x + dx[i];
			int yy = n1.y + dy[i];
			if(!visit[xx][yy] && (gra[xx][yy] == 0) && (xx >= 1) && (xx <= 5) &&(yy >= 1) && (yy <= 5))
			{
				visit[xx][yy] = 1;
				dist[xx][yy] = dist[n1.x][n1.y] + 1;
				node n2;
				n2.x = xx,n2.y = yy;
				que.push(n2);
			}
		}
	}
}
void print_path(node n1,node n2)
{
	int i;
	if((n2.x == n1.x) && (n1.y == n2.y))
		return;
	for(i=0; i<4; i++)
	{
		int xx = n2.x + dx[i];
		int yy = n2.y + dy[i];
		if((dist[n2.x][n2.y] == dist[xx][yy] + 1) && visit[xx][yy])
		{
			node n3;
			n3.x =xx,n3.y =yy;
			print_path(n1,n3);
			printf("(%d, %d)\n",n3.x-1,n3.y-1);
		}
	}
	return ;
}
int main()
{
	int i,j;
	for(i=1; i<=5; i++)
		for(j=1; j<=5; j++)
			scanf("%d",&gra[i][j]);
	bfs(1,1);	
	node n1,n2;
	n1.x = 1, n1.y = 1;
	n2.x = 5, n2.y = 5;
	print_path(n1,n2);
	printf("(4, 4)");
	return 0;
}



根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值