• 迷宫问题


    问题 C: 迷宫问题

    时间限制: 1 Sec  内存限制: 32 MB
    [提交][状态][讨论版]

    题目描述

    小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程。
    小明只能向上下左右四个方向移动。

    输入

    输入包含多组测试数据。输入的第一行是一个整数T,表示有T组测试数据。
    每组输入的第一行是两个整数N和M(1<=N,M<=100)。
    接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格。
    字符的含义如下:
    ‘S’:起点
    ‘E’:终点
    ‘-’:空地,可以通过
    ‘#’:障碍,无法通过
    输入数据保证有且仅有一个起点和终点。

    输出

    对于每组输入,输出从起点到终点的最短路程,如果不存在从起点到终点的路,则输出-1。

    样例输入

    1

    5 5

    S-###

    -----

    ##---

    E#---

    ---##

    样例输出

    9

    题意概括:

    算出从起点到终点最少需多少步。

    解题分析:

    用广度优先搜索从起点开始搜索,当到达终点时结束搜索并输出步数,同时当无法到达终点时输出-1。

    测试样例:

    2

    5 5

    S-###

    -----

    #####

    E#---

    ---##

    5 5

    S-###

    ---##

    ###--

    E#---

    ---##

    测试样例输出:

    -1

    -1

    代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 
     5 using namespace std;
     6 
     7 struct note{
     8     int x, y;
     9 };
    10 int book[102][102], n, m;
    11 char a[102][102];
    12 
    13 void bfs(note h, note t)
    14 {
    15     int tx, ty, i;
    16     int Next[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
    17     note head, tail;
    18     queue<note> q;
    19     q.push(h);
    20     book[h.x][h.y] = 1;
    21     while(!q.empty()){
    22         head = q.front();
    23         q.pop();
    24         for(i = 0; i < 4; i++){
    25             tx = head.x + Next[i][0];
    26             ty = head.y + Next[i][1];
    27             if(tx < 0 || ty < 0 || ty > m || tx > n)
    28                 continue;
    29             if(book[tx][ty] == 0 && a[tx][ty] == '-' || a[tx][ty] == 'E'){
    30                 tail.x = tx;
    31                 tail.y = ty;
    32                 q.push(tail);
    33                 book[tx][ty] = book[head.x][head.y] + 1;
    34             }
    35             if(tx == t.x && ty == t.y)
    36                 return ;
    37         }
    38     }
    39 }
    40 
    41 int main()
    42 {
    43     int T, i, j;
    44     note head, tail;
    45     scanf("%d", &T);
    46     while(T--){
    47         memset(book, 0, sizeof(book));
    48         scanf("%d%d", &n, &m);
    49         getchar();
    50         for(i = 0; i < n; i++){
    51             for(j = 0; j < m; j++){
    52                 scanf("%c", &a[i][j]);
    53                 if(a[i][j] == 'S'){
    54                     head.x = i;
    55                     head.y = j;  
    56                 }
    57                 else if(a[i][j] == 'E'){
    58                     tail.x = i;
    59                     tail.y = j;
    60                 }
    61                }
    62                getchar();
    63         }
    64         bfs(head, tail);
    65         
    66         printf("%d
    ", book[tail.x][tail.y]-1);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    null和undefined的区别
    百度小程序组件引用问题
    hbase优化操作与建议
    Hbase Rowkey设计原则
    kafka容器报内存不足异常(failed; error='Cannot allocate memory' (errno=12))
    Hbase安装
    四、hive安装
    一、linux安装mysql
    三、hadoop、yarn安装配置
    linux下磁盘进行分区、文件系统创建、挂载和卸载
  • 原文地址:https://www.cnblogs.com/didideblog/p/7224936.html
Copyright © 2020-2023  润新知