题目连接

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 (优先队列+bfs)

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

  2. hdu 2822 Dogs(优先队列)

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

  3. HDU 2822 (BFS+优先队列)

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

  4. HDU 5127 Dogs' 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.Dogs' Candies-STL(vector)神奇的题,set过不了 (2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大))

    周六周末组队训练赛. 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. 【LeetCode】20. Valid Parentheses

    题目:

  2. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  3. DNS协议 实践

    根据DNS协议发送UDP请求,然后获取IP地址 头文件: #ifndef __DNS__ #define __DNS__ #include <stdio.h> #include <s ...

  4. prototype原型模式中的问题

    对于每个构造函数来说,都有一个prototype属性.对于每个对象实例来说,都有_proto_属性. 参看下面代码: function Person(){} Person.prototype={ na ...

  5. ios项目记录

    1,如何隐藏状态栏 在基类中重载UIViewController.h中的这个方法 - (BOOL)prefersStatusBarHidden { // iOS7后,[[UIApplication s ...

  6. Windbg 线程状态笔记

    1.ntdll!ZwWaitForSingleObject 线程被挂起,如果下面跟着是这样子: RetAddr : Args to Child : Call Site `7766e518 : `fff ...

  7. 要在一般处理程序中获取其他页面的session值

    1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionState, ...

  8. Question2Answer初体验

    Question2Answer初体验   高质量的问答社区十分有价值,很多无法解决的问题能通过问答社区找到解决办法,而对于站长来说,垂直的问答社区也很有潜力.最近盯上问答这一块,发现和我的一些思路很符 ...

  9. Linux之档案管理

    1:档案类型[1] d :目录 -:档案 l:链接档 b:装置文件中可存储接口设备 c:装置文件中串行设备,例如:键盘,鼠标 2:RWX: R:read (可读),W:write(可写),X:excu ...

  10. C# 中解决页面刷新后字体等变大问题

    来源:http://blog.csdn.net/wcsjsdn/article/details/5109605 我们在.net开发中时常会遇到一个问题,那就是,当点击某个按钮后,调用js语句,当点击& ...