Maze problem - bfs

Keywords: Programming

Define a 2D 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 the wall, 0 represents the path that can be walked, can only walk horizontally or vertically, can't walk obliquely, and requires programming to find the shortest path from the upper left corner to the lower right corner. Input a 5 × 5 two-dimensional array, representing a maze. Data guarantees a unique solution. Output the shortest path from the top left corner to the bottom right corner, in the format shown in the example. Sample Input0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0Sample Output(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

I've done this problem with depth first before, but today I'll do it with breadth first.
The trickier part is how to output after finding the shortest path. You can write a structure in which coordinate points, steps and subscripts of the previous point are stored. In this way, after the shortest path is found, the former coordinates can be inversely deduced through the end coordinates, all the way to the starting point.
You can reverse the coordinate points by stack.

#include<iostream>
#include<stack>
using namespace std;
struct note {
 int x, y;       //x, y are coordinates
 int last;  //last is the subscript value of the previous point
 int step;  //step record steps
};
int maze[5][5];
int book[5][5];   //sign
note que[9999];   //Array of structures to form a queue structure
void print(note xy) { //xy is the end point. According to last.step, you can find the previous point until the start point
 stack<note> sta;
 sta.push(xy);
 while (sta.top().last!=-1) { //Put them in the stack in turn
  sta.push(que[sta.top().last]);
 }
 while (!sta.empty()) {  //Then pop, you can get the reverse order
  cout << "(" << sta.top().x << ", " << sta.top().y << ")" << endl;
  sta.pop();
 }
}
note bfs() {
 int next[4][2] = { 1,0,-1,0,0,1,0,-1 }; //Directional array
 int flag = 0;
 int head = 0, tail = 0, tx, ty;
 que[tail].x = 0;  //Put the starting point coordinates in the queue first
 que[tail].y = 0;
 que[tail].step = 0;
 que[tail].last = -1;
 tail++;
 while (head<tail) {  //Queue empty exit loop
  for (int i = 0; i < 4; i++) {
   tx = que[head].x + next[i][0];
   ty = que[head].y + next[i][1];
   if (tx < 0 || ty < 0 || tx>4 || ty>4) { //Coordinate out of bounds looking for other coordinates
    continue;
   }
   if (maze[tx][ty] == 0 && book[tx][ty] == 0) { //The coordinates are not out of bounds and there are no obstacles and no passing
    book[tx][ty] = 1;   //Put this point in the queue
    que[tail].x = tx;
    que[tail].y = ty;
    que[tail].last = head;
    que[tail].step = que[head].step + 1;
    tail++;
   }
   if (tx == 4 && ty == 4) { //Exit the loop if the end point is reached
    flag = 1;
    break;
   }
  }
  if (flag) {
   break;
  }
  head++;
 }
 return que[tail-1];  //Return to the finish line
}
int main() {
 for (int i = 0; i < 5; i++) {
  for (int j = 0; j < 5; j++) {
   cin >> maze[i][j];
  }
 }
 print(bfs());
 return 0;
}

Posted by D on Sat, 30 Nov 2019 12:16:08 -0800