zoukankan      html  css  js  c++  java
  • 杭电1728--逃离迷宫(Bfs)

    逃离迷宫

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 19687    Accepted Submission(s): 4796


    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
     

     

    Source
     

     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1253 1175 1072 1026 1372 
     
     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 const int INF = 0x3f3f3f3f;
     7 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
     8 int r, c, sx, sy, ex, ey, t, k; 
     9 char map[110][110]; int vis[110][110];
    10 struct pos
    11 {
    12     int x, y;
    13 };
    14 void Bfs ()
    15 {
    16     int i, j;
    17     for(i = 0; i < r; i++)
    18         for(j = 0; j < c; j++)
    19             vis[i][j] = INF;
    20     pos ft, tp;
    21     ft.x = sx; ft.y = sy;
    22     vis[ft.x][ft.y] = -1;
    23     queue<pos> q;
    24     q.push(ft);
    25     while(!q.empty()) 
    26     {
    27         ft = q.front();
    28         q.pop();
    29         if(ft.x == ex && ft.y == ey && vis[ft.x][ft.y] <= k)
    30         {
    31             puts("yes");
    32             return;
    33         }
    34         for(int i = 0; i < 4; i++)
    35         {
    36             tp.x = ft.x + ac[i][0];
    37             tp.y  =ft.y + ac[i][1];
    38         while (!(tp.x < 0 || tp.y < 0 || tp.x >= r || tp.y >= c))
    39             {
    40                 if (map[tp.x][tp.y] == '*')
    41                     break;
    42                 if (vis[tp.x][tp.y] < vis[ft.x][ft.y] + 1)
    43                     break;
    44                 vis[tp.x][tp.y] = vis[ft.x][ft.y] + 1;
    45                 if (vis[tp.x][tp.y] > k)
    46                     break;
    47                 if (vis[tp.x][tp.y] == k && tp.x != ex && tp.y != ey)
    48                     break;
    49                 q.push (tp);
    50                 tp.x += ac[i][0];
    51                 tp.y += ac[i][1];   
    52             }
    53         }
    54     }
    55     puts("no");
    56 }
    57 int main()
    58 {
    59     int t, i;
    60     scanf("%d", &t);
    61     while(t--)
    62     {
    63         scanf("%d %d", &r, &c);
    64         for(i = 0; i < r; i++)
    65             cin >> map[i];
    66         
    67         scanf("%d %d %d %d %d", &k, &sy, &sx, &ey, &ex);
    68         sx--; sy--; ey--; ex--;
    69         Bfs(); 
    70     }
    71     return 0;
    72 } 

    //懵了。

  • 相关阅读:
    性能测试总结(一)测试流程
    WSDL入门
    Python中的while循环和for循环
    python中的变量
    吴恩达《机器学习》章节2单变量线性回归
    吴恩达《机器学习》章节1绪论:初识机器学习
    新式类多继承的查找顺序
    python2x和python3x的一些区别
    类方法和静态方法
    @property的使用
  • 原文地址:https://www.cnblogs.com/soTired/p/4723847.html
Copyright © 2011-2022 走看看