C++迷宫问题(BFS)

也不确定是不是BFS,下面是题目,最后是解析,小白程序员,不喜勿喷。

Description:

You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And you can only walk in four directions:up, down,left, right.

There will only be 5 kinds of characters in the maze.The meaning of each is as followed.

"#" is for rock, which you can not walk through.

"S" is for start.

"E" is for end.

"." is for road.

"!" is for magma(岩浆),which you can not walk through.

Input

n,m represent the maze is a nxm maze.(n rows, m columnsm,0 <n,m <= 21).

Then is the maze.

e.g.

5 5

#####

#S..#

#.!.#

#.#E#

#####

 

Output

You need to give the least steps to walk from start to the end.If it doesn't exist, then output -1.

e.g.(for the example in input)

4

    #include<iostream>
#include<queue>
using namespace std;

typedef struct pos {
    int x, y;
} pos;
queue<pos> maze;
int position[3] = {-1, 1, 0};
int find(char** my_graph, int row, int col) {
    int len[row][col];
    pos begin;
    pos end;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (my_graph[i][j] == 'S') {
                begin.x = i;
                begin.y = j;
                goto out;
            }
        }
    }
    out:
// 很抱歉用一下goto偷懒
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            if (my_graph[i][j] == 'E') {
                end.x = i;
                end.y = j;
                goto out1;
            }
        }
    }
    out1:
    maze.push(begin);
// 判断是否为空,若有空的话,则表明遍历结束,结束循环
    while (!maze.empty()) {
        pos pointer = maze.front();
        maze.pop();
// 若为E,则结束并返回值
        if (end.x == pointer.x && end.y == pointer.y) {
            return len[pointer.x][pointer.y];
        }
        pos temp;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                // repair it
                if (pointer.x + position[i] < 0 ||
                    pointer.x + position[i] >= row || pointer.y + position[j] < 0
                    || pointer.y + position[j] >= col)
                    continue;
// 四个方向
                if (position[i] + position[j] == 0 || position[i] == position[j])
                    continue;
// 能走的路进队列
                if (my_graph[pointer.x + position[i]][pointer.y + position[j]]
                    == '.' ||
                    my_graph[pointer.x + position[i]][pointer.y + position[j]] == 'E') {
                    temp.x = pointer.x + position[i];
                    temp.y = pointer.y + position[j];
                    len[temp.x][temp.y] = len[pointer.x][pointer.y] + 1;
// 走过的路标记,不再走这条路
                    my_graph[temp.x][temp.y] = '*';
                    maze.push(temp);
                }
            }
        }
    }
    return -1;
}

int main() {
    int row, col;
    cin >> row >> col;
// 构建迷宫
    char **my_graph = new char*[row];
    for (int i = 0; i < row; i++) {
        my_graph[i] = new char[col];
    }
    for (int i = 0; i < row; i++) {
      for (int j = 0; j < col; j++)
        cin >> my_graph[i][j];
    }
    int least_len = find(my_graph, row, col);
    cout << least_len << endl;
    for (int i = 0; i < row; i++)
      delete []my_graph[i];
    delete my_graph;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章