hdu 2822 Dogs

时间:2023-01-03 13:40:09

题目连接

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

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
const int dx[] = { , , -, }, dy[] = { -, , , };
bool vis[N][N];
char rec[N][N];
int m, n, Sx, Sy, Dx, Dy;
struct Node {
int x, y, s;
Node(int i = , int j = , int k = ) :x(i), y(j), s(k) {}
bool operator<(const Node &a) const {
return s > a.s;
}
};
void bfs() {
cls(vis, false);
priority_queue<Node> que;
que.push(Node(Sx, Sy, ));
vis[Sx][Sy] = true;
while (!que.empty()) {
Node tmp = que.top(); que.pop();
if (tmp.x == Dx && tmp.y == Dy) { printf("%d\n", tmp.s); return; }
rep(i, ) {
int nx = tmp.x + dx[i], ny = tmp.y + dy[i];
if (nx < || nx >= m || ny < || ny >= n || vis[nx][ny]) continue;
if (rec[nx][ny] == 'X') que.push(Node(nx, ny, tmp.s));
else que.push(Node(nx, ny, tmp.s + ));
vis[nx][ny] = true;
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d %d", &m, &n) && m + n) {
rep(i, m) scanf("%s", rec[i]);
scanf("%d %d %d %d", &Sx, &Sy, &Dx, &Dy);
Sx--, Sy--, Dx--, Dy--;
bfs();
}
return ;
}

hdu 2822 Dogs的更多相关文章

  1. hdu - 2822 Dogs &lpar;优先队列&plus;bfs&rpar;

    http://acm.hdu.edu.cn/showproblem.php?pid=2822 给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到  . 才需要挖一次. #includ ...

  2. hdu 2822 Dogs(优先队列)

    题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue&gt ...

  3. HDU 2822 &lpar;BFS&plus;优先队列&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  4. HDU 5127 Dogs&&num;39&semi; Candies

    Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...

  5. 【HDOJ】2822 Dogs

    bfs. /* 2822 */ #include <iostream> #include <cstdio> #include <cstring> #include ...

  6. HDU 2822

    Dogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. HDU 5127&period;Dogs&&num;39&semi; Candies-STL&lpar;vector&rpar;神奇的题,set过不了 &lpar;2014ACM&sol;ICPC亚洲区广州站-重现赛(感谢华工和北大)&rpar;

    周六周末组队训练赛. Dogs' Candies Time Limit: 30000/30000 MS (Java/Others)    Memory Limit: 512000/512000 K ( ...

  8. hdu 2822 ~!!!!!!坑死我

    首先 在此哀悼...  为我逝去的时间哀悼...  每一步都确定再去写下一步吧...日狗 不过还是有点收获的..  对优先队列的使用 有了进一步的理解 先上代码 #include<iostrea ...

  9. DP的优化总结

    一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...

随机推荐

  1. iOS 源代码管理工具之SVN

    源代码管理工具之SVN 源代码管理工具SVN是一款非常强大的源代码管理工具,现在国内70%-90%的公司都在使用SVN来管理源代码,下面就让小编给大家着重介绍一下SVN的使用,SVN的使用主要分为下面 ...

  2. WebBrowser 禁用右键

    禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...

  3. Hosting Multiple Service Implementations On The Same Port With WCF

    Hosting Multiple Service Implementations On The Same Port With WCF Recently I have been playing arou ...

  4. Directx3d绘制包围体并控制光照效果

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. luogu2402 奶牛隐藏

    题目描述 在一个农场里有n块田地.某天下午,有一群牛在田地里吃草,他们分散在农场的诸多田地上,农场由m条无向的路连接,每条路有不同的长度. 突然,天降大雨,奶牛们非常混乱,想要快点去躲雨.已知每个田地 ...

  6. JMeter学习(十二)分布式部署(转载)

    转载自 http://www.cnblogs.com/yangxia-test Jmeter 是java 应用,对于CPU和内存的消耗比较大,因此,当需要模拟数以千计的并发用户时,使用单台机器模拟所有 ...

  7. EXISTS 和 IN 的区别

    exists子句的用法 select * from 表1 where exists (select * from 表2 where 表1.列名=表2.列名); exists子句返回的结果并不是从数据库 ...

  8. Python入门之python可变对象与不可变对象

    本文分为如下几个部分 概念 地址问题 作为函数参数 可变参数在类中使用 函数默认参数 类的实现上的差异 概念 可变对象与不可变对象的区别在于对象本身是否可变. python内置的一些类型中 可变对象: ...

  9. Linq排序方式与Lambda排序方式比较以及OrderBy、ThenBy的使用

    沿用之前某一篇文章的实体类与EF操作类代码.数据库中增加几条数据 Linq 的排序方式,下面例子是根据RoleId 升序,Name降序 EFContext<Member> efMember ...

  10. eclipse Java代码折叠工具

      eclipse Java代码折叠工具 CreateTime--2018年5月17日15点09分 Author:Marydon 1.问题描述 eclipse自带的代码折叠工具,无法折叠try{}ca ...