Maze Problem (bfs)

Labyrinth question poj3984
View submission statistics question
Total time limit: 1000ms Memory limit: 65536kB
Description
Define a two-dimensional array:

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,

};

It represents a maze, in which 1 represents a wall, and 0 represents a path that can be walked. You can only walk horizontally or vertically, and you cannot walk diagonally. It requires programming to find the shortest route from the upper left corner to the lower right corner.

Input
A 5 × 5 two-dimensional array representing a maze. The data is guaranteed to have a unique solution.
Output
The shortest path from the upper left corner to the lower right corner, in the format shown in the example.
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)
This question outputs the path traveled by bfs, how to save the passed nodes is really a problem, I I don't know how to save it. I found a great god's code on the Internet, but when his ideas are put in my code, there is a problem. I don't know how to modify the code below and paste the great god.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int mp[5][5],vis[5][5];
int dir[4][2]={
   
   {1,0},{-1,0},{0,1},{0,-1}};
struct node{
    int x,y;
};
node pre[10][10];
void BFS(){
    queue<node> que;
    node str;
    str.x=str.y=0;
    que.push(str);
    vis[0][0]=1;
    while(!que.empty()){
        node now = que.front();
        que.pop();
        if(now.x == 4 && now.y == 4)
            return;
        for(int i = 0;i < 4;i++){
            node next;
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            if(next.x >= 0 && next.x < 5 && next.y >= 0 && next.y < 5 && !mp[next.x][next.y] && !vis[next.x][next.y]){
                vis[next.x][next.y] = 1;
                que.push(next);
                pre[next.x][next.y] = now;
            }
        }
    }
}
void print(node cur){
    if(cur.x == 0 && cur.y == 0){
        printf("(0, 0)\n");
        return;
    }
    print(pre[cur.x][cur.y]);   //逆序输出
    printf("(%d, %d)\n",cur.x,cur.y);
}
int main(){
    for(int i = 0;i < 5;i++){
        for(int j = 0;j < 5;j++){
        	scanf("%d",&mp[i][j]);	
        }    	
    }       
    BFS();
    node ed;
    ed.x = ed.y = 4;
    print(ed);
    return 0;
}

Because I think his output is a bit troublesome and I didn't understand it very well, I read it myself and wrote a simple one. There is a problem that the hierarchical traversal will output all the points around the center point. I don't know how to solve the problem of the smallest path. Points are stored in the queue.
Below is the code I wrote that results in the error

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
int cross[5] = {0,0,-1,0,1};
int stra[5] = {0,1,0,-1,0};
int mapp[50][50];
bool vis[50][50];
stack <int> s1,s2;
struct node{
	int x,y;
	node(int a,int b){//构造方法 
		x = a;
		y = b;
	}
};
queue<node> q,q1;
void bfs(int x,int y){
	q.push(node(x,y));
	vis[x][y] = 1;
	while(!q.empty()){
		node now = q.front();
		q.pop();
		if(now.x == 4 && now.y == 4){
			return;
		}
		for(int i = 1;i <= 4;i++){
			int pre = now.x + cross[i];
			int pry = now.y + stra[i];
			if(mapp[pre][pry] != 1 && pry >=0 && pry < 5 && pre >= 0 && pre < 5){
				vis[pre][pry] = 1;
				q.push(node(pre,pry));	
				q1.push(now);
				//s1.push(pre);s2.push(pry);ll
			}
		}
	}
}
void print(){ 
	while(!q1.empty()){
	  node a = q1.front();
	  q1.pop();
	  printf("(%d, %d)\n",a.x,a.y);
	}
	printf("(4, 4)\n");
}
int main(){
	for(int i = 0;i < 5;i++){
		for(int j = 0;j < 5;j++){
			cin >> mapp[i][j];
		}
	}
	memset(vis,0,sizeof(vis));
	bfs(0,0);
	print();
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686181&siteId=291194637