zoukankan      html  css  js  c++  java
  • hdu 1728 逃离迷宫

    题目连接

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

    逃离迷宫

    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。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<queue>
     8 #include<map>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::map;
    15 using std::pair;
    16 using std::queue;
    17 using std::vector;
    18 using std::multimap;
    19 #define pb(e) push_back(e)
    20 #define sz(c) (int)(c).size()
    21 #define mp(a, b) make_pair(a, b)
    22 #define all(c) (c).begin(), (c).end()
    23 #define iter(c) decltype((c).begin())
    24 #define cls(arr,val) memset(arr,val,sizeof(arr))
    25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    28 const int N = 110;
    29 typedef unsigned long long ull;
    30 char G[N][N];
    31 bool vis[N][N][11][4]; // 访问标志
    32 int H, W, K, Sx, Sy, Dx, Dy;
    33 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
    34 struct Node{
    35     // x坐标,y坐标 方向 转弯次数
    36     int x, y, dir, turn;
    37     Node() {}
    38     Node(int i, int j, int k, int l) :x(i), y(j), dir(k), turn(l) {}
    39 };
    40 bool bfs() {
    41     cls(vis, false);
    42     queue<Node> q;
    43     rep(i, 4) {
    44         int x = Sx + dx[i], y = Sy + dy[i];
    45         if (x < 0 || x >= H || y < 0 || y >= W || G[x][y] == '*') continue;
    46         q.push(Node(x, y, i, 0));
    47         vis[x][y][0][i] = true;
    48     }
    49     while (!q.empty()) {
    50         Node t = q.front(); q.pop();
    51         if (t.x == Dx && t.y == Dy) return true;
    52         rep(i, 4) {
    53             int x = t.x + dx[i], y = t.y + dy[i];
    54             if (x < 0 || x >= H || y < 0 || y >= W || G[x][y] == '*') continue;
    55             if (t.dir == i) {
    56                 bool &p = vis[x][y][t.turn][i];
    57                 if (!p) {
    58                     q.push(Node(x, y, t.dir, t.turn));
    59                     p = true;
    60                 }
    61             } else {
    62                 bool &p = vis[x][y][t.turn + 1][i];
    63                 if (t.turn + 1 <= K && !p) {
    64                     q.push(Node(x, y, i, t.turn + 1));
    65                     p = true;
    66                 }
    67             }
    68         }
    69     }
    70     return false;
    71 }
    72 int main() {
    73 #ifdef LOCAL
    74     freopen("in.txt", "r", stdin);
    75     freopen("out.txt", "w+", stdout);
    76 #endif
    77     int t;
    78     scanf("%d", &t);
    79     while (t--) {
    80         scanf("%d %d", &H, &W);
    81         rep(i, H) scanf("%s", G[i]);
    82         scanf("%d %d %d %d %d", &K, &Sy, &Sx, &Dy, &Dx);
    83         Sx--, Sy--, Dx--, Dy--;
    84         puts(bfs() ? "yes" : "no");
    85     }
    86     return 0;
    87 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    maria-developers 开发者邮件
    Parallel Programming--perfbook
    面向对象设计模式中类与类关系
    binlog 轻松的找到没有及时提交的事物(infobin工具
    deeplearningbook-chinese
    Introduction to the Optimizer --cbo
    dell T420热插拔安装过程
    MySQL是如何利用索引的
    BTrace housemd TProfiler
    杨建荣的学习笔记
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4620606.html
Copyright © 2011-2022 走看看