zoukankan      html  css  js  c++  java
  • hdu 2822 Dogs

    题目连接

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

    Dogs

    Description

    Prairie dog comes again! Someday one little prairie dog Tim wants to visit one of his friends on the farmland, but he is as lazy as his friend (who required Tim to come to his place instead of going to Tim's), So he turn to you for help to point out how could him dig as less as he could.

    We know the farmland is divided into a grid, and some of the lattices form houses, where many little dogs live in. If the lattices connect to each other in any case, they belong to the same house. Then the little Tim start from his home located at (x0, y0) aim at his friend's home ( x1, y1 ). During the journey, he must walk through a lot of lattices, when in a house he can just walk through without digging, but he must dig some distance to reach another house. The farmland will be as big as 1000 * 1000, and the up left corner is labeled as ( 1, 1 ).

    Input

    The input is divided into blocks. The first line in each block contains two integers: the length m of the farmland, the width n of the farmland (m, n ≤ 1000). The next lines contain m rows and each row have n letters, with 'X' stands for the lattices of house, and '.' stands for the empty land. The following two lines is the start and end places' coordinates, we guarantee that they are located at 'X'. There will be a blank line between every test case. The block where both two numbers in the first line are equal to zero denotes the end of the input.

    Output

    For each case you should just output a line which contains only one integer, which is the number of minimal lattices Tim must dig.

    Sample Input

    6 6
    ..X...
    XXX.X.
    ....X.
    X.....
    X.....
    X.X...
    3 5
    6 3

    0 0

    Sample Output

    3

    走'X'不用花时间,走'.'时间为1

     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::vector;
    17 using std::multimap;
    18 using std::priority_queue;
    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 = 1010;
    29 typedef unsigned long long ull;
    30 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
    31 bool vis[N][N];
    32 char rec[N][N];
    33 int m, n, Sx, Sy, Dx, Dy;
    34 struct Node {
    35     int x, y, s;
    36     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
    37     bool operator<(const Node &a) const {
    38         return s > a.s;
    39     }
    40 };
    41 void bfs() {
    42     cls(vis, false);
    43     priority_queue<Node> que;
    44     que.push(Node(Sx, Sy, 0));
    45     vis[Sx][Sy] = true;
    46     while (!que.empty()) {
    47         Node tmp = que.top(); que.pop();
    48         if (tmp.x == Dx && tmp.y == Dy) { printf("%d
    ", tmp.s); return; }
    49         rep(i, 4) {
    50             int nx = tmp.x + dx[i], ny = tmp.y + dy[i];
    51             if (nx < 0 || nx >= m || ny < 0 || ny >= n || vis[nx][ny]) continue;
    52             if (rec[nx][ny] == 'X') que.push(Node(nx, ny, tmp.s));
    53             else que.push(Node(nx, ny, tmp.s + 1));
    54             vis[nx][ny] = true;
    55         }
    56     }
    57 }
    58 int main() {
    59 #ifdef LOCAL
    60     freopen("in.txt", "r", stdin);
    61     freopen("out.txt", "w+", stdout);
    62 #endif
    63     while (~scanf("%d %d", &m, &n) && m + n) {
    64         rep(i, m) scanf("%s", rec[i]);
    65         scanf("%d %d %d %d", &Sx, &Sy, &Dx, &Dy);
    66         Sx--, Sy--, Dx--, Dy--;
    67         bfs();
    68     }
    69     return 0;
    70 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    15个华丽的扁平风格的登录界面设计示例
    12款很酷的使用大头照的国外名片设计作品
    高清壁纸下载:15款精美的2014年元旦桌面壁纸
    Harp – 内置常用预处理器的静态 Web 服务器
    分享245款高质量的图标素材【免费下载】
    经典网页设计:关注用户体验的20个华丽网站
    使用 CSS3 实现超炫的 Loading(加载)动画效果
    你知道网页设计中最常用的15张图片素材吗?
    Koa – 更加强大的下一代 Node.js Web 框架
    Myth – 支持变量和数学函数的 CSS 预处理器
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4606109.html
Copyright © 2011-2022 走看看