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
#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的更多相关文章
- hdu - 2822 Dogs (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2822 给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到 . 才需要挖一次. #includ ...
- hdu 2822 Dogs(优先队列)
题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- 【HDOJ】2822 Dogs
bfs. /* 2822 */ #include <iostream> #include <cstdio> #include <cstring> #include ...
- HDU 2822
Dogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 5127.Dogs' Candies-STL(vector)神奇的题,set过不了 (2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大))
周六周末组队训练赛. Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K ( ...
- hdu 2822 ~!!!!!!坑死我
首先 在此哀悼... 为我逝去的时间哀悼... 每一步都确定再去写下一步吧...日狗 不过还是有点收获的.. 对优先队列的使用 有了进一步的理解 先上代码 #include<iostrea ...
- DP的优化总结
一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...
随机推荐
- SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...
- 【练习】增加日志组数至4组,且每组日志成员大小为50M,每组2个成员。
1.查看日志组成员路径及日志组大小.状态 SQL> select group#,member from v$logfile; GROUP# MEMBER ---------- --------- ...
- 一目了然c++虚基类!
#include <IOSTREAM.H> //基类 class CBase ...{ protected: int a; public: CBase(int na) ...{ a=na; ...
- 两个正在运行的activity之间的通信
在android应用程序开发的时候,从一个activity启动另一个activity并传递一些数据到新的activity非常的简单,但是当您需要让后台运行的activity回到前台并传递一些数据可能就 ...
- 关于union的那些事儿
自从当年明月的<明朝的那些事儿>爆红之后,以***那些事儿命名的文章便层出不穷.个人认为,这样的命名通俗但具有吸引力,容易接地气.哈哈,所以我也写了几篇以<***那些事儿>的文 ...
- Win8系统安装NET Framework 3.5的方法
1)找到下载过的Win8系统盘,解压到某个目录下,例如 F:, 找到解压的系统盘目录中"sources\sxs"文件夹 2)以管理员身份运行cmd命令,执行下面的命令: dism. ...
- SQL Server 之登录
1:SQL Server 是Microsoft 公司推出的关系型数据库管理系统. 安装好后,开始登录. 2:几种登录方式: (1):服务器名称: ①: . :代表本地服务器 ②: local :代表本 ...
- XSS测试语句大全
'><script>alert(document.cookie)</script> ='><script>alert(document.cookie)& ...
- 011OK6410开发板介绍
1.系统资源: (1)处理器:三星ARM11,S3C6410A,主频533MHz/667MHz (2)nor flash (3)nand flash:1G字节NAND Flash (4)RAM:128 ...
- 面向对象的 JavaScript
面向对象的javascript 一.创建对象 创建对象的几种方式: var obj = {}; var obj = new Object(); var obj = Object.create(fath ...