P34: 迷宫的最短路径(宽度优先搜索求最短路径最少操作)

 

/*
    P34: 迷宫的最短路径

    核心:用队列实现宽度优先搜索,队列里面放的都是将要走的位置,
          队列pop出来的位置是当前要走的位置
          队列push进去的位置是周围所有可走但还未走的位置
          到终点或队列为空时退出。分别表示可达和不可达.

    技巧:可以用大小相同的flag[][]来标记一些信息。这里我用-1表示未走,0表示0步后可达,1表示1步后可达,2表示。。。。。
          用pre[][]表示路径信息,找到终点后可以打印出所有路径

    注意:方向向量的方向,左上角为坐标原点,(1,0)为下,(-1,0)为上,(0,1)为右,(0,-1)为左

运行结果:
*************************************************************************************
            10 10

            #S######.#
            ......#..#
            .#.##.##.#
            .#........
            ##.##.####
            ....#....#
            .#######.#
            ....#.....
            .####.###.
            ....#...G#
            22
            墙 起 墙 墙 墙 墙 墙 墙 |` 墙
            <- |. -> -> -> -> 墙 <- |` 墙
            |. 墙 |. 墙 墙 |. 墙 墙 |` 墙
            |. 墙 |. -> -> |. -> -> -> ->
            墙 墙 |. 墙 墙 |. 墙 墙 墙 墙
            <- <- |. -> 墙 |. -> -> -> 墙
            |. 墙 墙 墙 墙 墙 墙 墙 |. 墙
            |. -> -> -> 墙 <- <- <- |. ->
            |. 墙 墙 墙 墙 |. 墙 墙 墙 |.
            |. -> -> -> 墙 |. -> -> 终 墙

****************************************************************************************
*/
#include <utility>
#include <queue>
#include <iostream>
#include <iterator>
#include <vector>
#include <memory.h>
using namespace std;
const int MAX = 110;
int N, M;
char maze[MAX][MAX];

queue< pair<int, int> > que;//用队列实现bfs
int dx[] = {-1, 0, 0, 1};//行向量
int dy[] = {0, -1, 1, 0};//列向量,四个方向向量, 左上角为原点,所以方向为上,左,右,下
int flag[MAX][MAX];//用做标记(i,j)走多少步可达
string pre[MAX][MAX];
int bfs(int i, int j)
{
    //起点
    pair<int, int> start(i, j);
    que.push(start);
    flag[i][j] = 0;
    pre[i][j] = "";

    while (que.size())
    {
        //队列pop出来的是当前要走的位置
        pair<int, int> que_top = que.front();
        que.pop();

        //要走的位置是终点
        if (maze[que_top.first][que_top.second] == 'G')
        {
            pre[que_top.first][que_top.second] = "";
            return flag[que_top.first][que_top.second];
        }


        //队列push进去的是周围可走但未走的位置
        for (int i = 0; i < 4; i++)
        {
            int nx = dx[i] + que_top.first;
            int ny = dy[i] + que_top.second;

            if (nx >= 0 && nx < N && ny >= 0 && ny < M && maze[nx][ny] != '#' && flag[nx][ny] == -1)
            {
                que.push(pair<int, int>(nx, ny));
                flag[nx][ny] = flag[que_top.first][que_top.second]+1;
                switch(i)
                {
                case 0:
                    pre[nx][ny] = "|`";
                    break;
                case 1:
                    pre[nx][ny] = "<-";
                    break;
                case 2:
                    pre[nx][ny] = "->";
                    break;
                case 3:
                    pre[nx][ny] = "|.";
                    break;
                }
            }
        }
    }
    return -1;

}
int main(void)
{
    memset(flag, -1, sizeof(flag));
    cin >> N >> M;
    for (int i  = 0; i <  N; i++)
        for (int j = 0; j < M; j++)
        {
            cin>>maze[i][j];
            pre[i][j] = "";
        }


    for (int i  = 0; i <  N; i++)
        for (int j = 0; j < M; j++)
        {
            int temp;
            if (maze[i][j] == 'S')
            {
                if ((temp = bfs(i, j)) == -1)
                    cout << "can't reach \n";
                else
                    cout << temp <<"\n";

            }

        }

    for (int i  = 0; i <  N; i++)
        for (int j = 0; j < M; j++)
        {
            cout << pre[i][j] << " ";
            if(j == M-1)
                cout << "\n";
        }

    return 0;
}

 

posted on 2018-04-03 09:17  jkn1234  阅读(263)  评论(0编辑  收藏  举报