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;
}


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