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 ...
随机推荐
- SQL基本语句(2)
使用Insert语句插入新数据 语法:INSERT [INTO] tbl_name [(col_name,...)] VALUES (pression,...),… INSERT [INTO] tbl ...
- Cnetos7下,已经能访问tomcat
进入/usr/local/apache-tomcat-8.0.24/bin 中 执行:./startup.sh开启tomcat 再执行如下 systemctl stop firewalld.servi ...
- 洛谷P1472 奶牛家谱 Cow Pedigrees
P1472 奶牛家谱 Cow Pedigrees 102通过 193提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 农民约翰准备 ...
- 运用Ntop监控网络流量(视频Demo)
运用Ntop监控网络流量 ____网络流量反映了网络的运行状态,是判别网络运行是否正常的关键数据,在实际的网络中,如果对网络流量控制得不好或发生网络拥塞,将会导致网络吞吐量下降.网络性能降低.通过流量 ...
- MongoDB 1: NoSQL 和 SQL的区别
导读:本篇博客,主要是结合自己在项目中的使用,简单的阐述一下NoSQL和SQL的区别.那么,根据自己的应用,NoSQL这边,选择的是MongoDB(Redis虽然也是,但属于内存存储,这里不予说明). ...
- centreon 降低rrd磁盘读写
参考 https://documentation.centreon.com/docs/centreon/en/2.7.x/faq/performance.html 修改rrdcached配置 vim ...
- php获取文件创建时间、修改时间
filemtime ( string filename ) 返回文件上次被修改的时间,出错时返回 FALSE.时间以 Unix 时间戳的方式返回,可用于 date(). 例如:$a=filemtime ...
- CSS3文字描边 CSS3字体外部描边
给需要实现文字描边的元素添加如下CSS3的属性 text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0; -webkit-t ...
- new_things
'NapCat' in AppStore for reading codes from github. and the ----minibufexpl | + nerd ...
- Promise机制
Promise的诞生与Javascript中异步编程息息相关,js中异步编程主要指的是setTimout/setInterval.DOM事件机制.ajax,通过传入回调函数实现控制反转.异步编程为js ...