zoukankan      html  css  js  c++  java
  • 挑战程序设计竞赛-2.1最基础的“穷竭搜索”-宽度优先搜索-迷宫的最短路径

    给定一个大小为N*M的迷宫,由通道('.')和墙壁('#')组成,其中通道S表示起点,通道G表示终点,每一步移动可以达到上下左右中不是墙壁的位置。试求出起点到终点的最小步数。

    (N,M<=100)

    样例输入:

    10 10

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

     
    样例输出:

    22

    代码:

     1 /*
     2 * 给定一个大小为N x M的迷宫。迷宫由通道和墙壁组成,每一步可以像邻接的上下左右四格的通道移动。
     3 * 请求出从起点到终点所需的最小步数
     4 * 
     5 */
     6 
     7 #include <cstdio>
     8 #include <algorithm>   //包含pair
     9 #include <queue>
    10 
    11 using namespace std;
    12 
    13 const int INF = 2 << 27;
    14 typedef pair<int, int> p;
    15 
    16 char maze[110][110];
    17 int d[110][110];   //到各个点的距离
    18 
    19 int n, m;
    20 int sx, sy,ex,ey;
    21 
    22 int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };
    23 
    24 //求(sx,sy)到(ex,ey)的最短距离
    25 //如果无法到达,则是INF
    26 
    27 int bfs() {
    28 
    29 
    30     for(int i = 0; i < n;i++)
    31         for (int j = 0; j < m; j++) {
    32             d[i][j] = INF;
    33 
    34     }
    35 
    36     queue<p> que;
    37     que.push(p(sx, sy));
    38     
    39     d[sx][sy] = 0;
    40 
    41 
    42     while (que.size()) {
    43 
    44         p p1 = que.front();
    45         que.pop();
    46 
    47         //如果取出的点就是终点,直接退出
    48         if (p1.first == ex && p1.second == ey)
    49             break;
    50 
    51         int nx, ny;
    52         for (int i = 0; i < 4; i++) {
    53             nx = p1.first + dx[i], ny = p1.second + dy[i];
    54             
    55             if (nx < n && nx >= 0 && ny < m && ny >= 0 && maze[nx][ny] != '#' && d[nx][ny] == INF) {
    56                 que.push(p(nx, ny));   //将没有被访问(不为INF)和可以到达(不为 #)的点加入队列
    57                 d[nx][ny] = d[p1.first][p1.second] + 1;
    58                 
    59             }
    60         }
    61     }
    62     return d[ex][ey];
    63 }
    64 
    65 
    66 int main() {
    67 
    68 
    69     scanf("%d %d", &n, &m);
    70 
    71     getchar();
    72     for (int i = 0; i < n; i++) {
    73         for (int j = 0; j < m; j++) {
    74             char ch = getchar();
    75             if (ch == 'S') {
    76                 sx = i;
    77                 sy = j;
    78             }else if(ch == 'G'){
    79                 ex = i;
    80                 ey = j;
    81             }
    82             maze[i][j] = ch;
    83         }
    84         getchar();
    85     }
    86 
    87     int res = bfs();
    88 
    89     if (res != INF)
    90         printf("%d
    ", res);
    91     else
    92         puts("INF");
    93         
    94     return 0;
    95 }
  • 相关阅读:
    基于脚本的nodemanager管理器
    SSH 等效性问题 总提示输入密码问题
    增量检查点【概念】
    【ORA错误大全】 ORA-19527
    DataGuard 配置须知
    rhel5.4+oracle 10g rac
    microg,google新推的一个计划
    [转]Android ListView 与 RecyclerView 对比浅析—缓存机制
    android studio的Beta, Canary, Dev, Stable四种Channel版本介绍、分析与选择
    android studio增量更新
  • 原文地址:https://www.cnblogs.com/--CYH--/p/6511758.html
Copyright © 2011-2022 走看看