【BFS】The shortest path out of the maze

Question: Given a M*N maze, 0 represents the road and 1 represents the wall. S and G represent the initial position and exit, respectively. Find the shortest path out of the maze. If it can't get out, return -1.

Idea: Classic breadth-first search.

/*
initialize queue q;
initialize marker matrix mask;
The starting point start joins the team;
while(q is not empty){
		The first element of the q team, tmp, is dequeued;
		if(tmp==target state){
				Find the solution, exit the loop;
		};
		for(){
				All points adjacent to tmp and not visited are enqueued;
		}
		mark tmp as visited;
}
*/

C++ code


Guess you like

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