• HDU 1728:逃离迷宫(BFS)


    http://acm.hdu.edu.cn/showproblem.php?pid=1728

     

    逃离迷宫

    Problem Description
     
      给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
     
    Input
     
      第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
      第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
     
    Output
     
      每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
     
    Sample Input
     
    2
    5 5
    ...**
    *.**.
    .....
    .....
    *....
    1 1 1 1 3
    5 5
    ...**
    *.**.
    .....
    .....
    *....
    2 1 1 1 3
     
    Sample Output
     
    no
    yes
     
    思路:比较简单的BFS,WA了两次是一开始vis数组只记录了x和y的,没有考虑不同方向和不同转次数也可能走出不同的结果,这和上次训练赛一道题悟空救唐僧有点类似,都要用多维的vis数组去记录,以后要搜索要多考虑这种情况。
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #include <algorithm>
     5 using namespace std;
     6 #define N 105
     7 
     8 struct node
     9 {
    10     int x, y, dre, t;
    11     node () {}
    12     node (int x, int y, int dre, int t) : x(x), y(y), dre(dre), t(t) {}
    13 };
    14 char maze[N][N];
    15 bool vis[N][N][15][4];
    16 int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
    17 int sx, sy, ex, ey, k, n, m;
    18 
    19 bool check(int x, int y)
    20 {
    21     if(0<=x&&x<m&&0<=y&&y<n&&maze[x][y]!='*') return true;
    22     return false;
    23 }
    24 //&&!vis[x][y]
    25 /*
    26 2
    27 5 5
    28 .....
    29 .*.*.
    30 .....
    31 .*.*.
    32 .....
    33 1 1 1 3 4
    34 */
    35 bool bfs()
    36 {
    37     queue<node> que;
    38     memset(vis ,0 ,sizeof(vis));
    39     que.push(node(sx, sy, -1, 0));
    40     while(!que.empty()) {
    41         node u = que.front(); que.pop();
    42         if(u.x == ex && u.y == ey && u.t <= k) return true;
    43         if(u.t > k) continue;
    44         if(u.dre != -1) {
    45             if(vis[u.x][u.y][u.dre][u.t]) continue;
    46             vis[u.x][u.y][u.dre][u.t] = 1;
    47         }
    48         for(int i = 0; i < 4; i++) {
    49             int nx = u.x + dx[i], ny = u.y + dy[i];
    50             if(!check(nx, ny)) continue;
    51             if(u.dre == -1) {
    52                 que.push(node(nx, ny, i, u.t));
    53                 continue;
    54             }
    55             que.push(node(nx, ny, i, u.dre == i ? u.t : u.t + 1));
    56         }
    57     }
    58     return false;
    59 }
    60 
    61 int main()
    62 {
    63     int t;
    64     scanf("%d", &t);
    65     while(t--) {
    66         scanf("%d%d", &m, &n);
    67         for(int i = 0; i < m; i++)
    68             scanf("%s", maze[i]);
    69         scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);
    70         sy--, sx--, ey--, ex--;
    71         if(maze[sx][sy] == '*' || maze[ex][ey] == '*') {
    72             puts("no"); continue;
    73         }
    74         if(bfs()) puts("yes");
    75         else puts("no");
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    Gerrit配置--用户配置
    repo+manifests+git方式管理安卓代码
    FLASH OTP
    Wireshark抓包分析TCP协议
    python 批量修改图片大小
    python 文件查找 glob
    python 统计单词个数
    python 图片上添加数字源代码
    python 删除文件和文件夹
    python 程序列表
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5764453.html
Copyright © 2020-2023  润新知