• 迷宫最短路径问题 题解 《挑战程序设计竞赛》


    问题:给定一个大小为N×M的迷宫。迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动。请求出从起点到终点所需的最小步数。请注意,本题假定从起点一定可以移动到终点。

    限制条件:N, M ≦100

    示例输入:

    10 10
    #S######.# 
    ......#..#
    .#.##.##.#
    .#........
    ##.##.####
    ....#....#
    .#######.#
    ....#.....
    .####.###.
    ....#...G#

    示例输出:

    22

    #include <stdio.h>
    #include <iostream>
    #include <queue>
    
    using namespace std;
    int m;
    int n;
    const int INF = 0x3f3f3f3f;
    char maze[100][100];
    int d[100][100];
    int sx, sy;
    int gx, gy;
    int dx[4] = { -1, 0, 1, 0 };
    int dy[4] = { 0, 1, 0, -1 };
    typedef pair<int, int> P;
    
    void bfs() {
        queue<P> q;
        q.push(P(sx, sy));
        d[sx][sy] = 0;
    
        while (q.size()) {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            if (x == gx && y == gy) {
                break;
            }
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && d[nx][ny] == INF) {
                    q.push(P(nx, ny));
                    d[nx][ny] = d[x][y] + 1;
                }
            }
    
        }
    
    }
    
    int main() {
        cin >> m >> n;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cin >> maze[i][j];
                d[i][j] = INF;
                if (maze[i][j] == 'S') {
                    sx = i;
                    sy = j;
                }
                if (maze[i][j] == 'G') {
                    gx = i;
                    gy = j;
                }
            }
        }
        bfs();
        cout << d[gx][gy];
        return 0;
    }

     DFS一般直接利用系统栈(代码中使用递归),而BFS需要自己维护一个队列

  • 相关阅读:
    hdu 2546 饭卡
    poj 2262 Goldbach's Conjecture
    poj 1287 Networking
    poj 2377 Bad Cowtractors
    poj 1789 Truck History
    poj 2349 Arctic Network
    poj 1258 Agri-Net
    acdream 20140730 D题
    hdu 1012 素数判定
    hdu 2098 分拆素数和
  • 原文地址:https://www.cnblogs.com/carolunar/p/6353889.html
Copyright © 2020-2023  润新知