HDU
来源:互联网 发布:linux gpio中断 编辑:程序博客网 时间:2024/06/06 01:27
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 ).
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 ).
6 6..X...XXX.X.....X.X.....X.....X.X...3 56 30 0
3
Hint: Three lattices Tim should dig: ( 2, 4 ), ( 3, 1 ), ( 6, 2 ).题意:两只狗去约会,然后都比较懒,怎么办呢?谁追谁谁去呗 ,这不Prairie要去啦()。
还有这事怎么能公开呢,必须偷偷的进行。所以Prairie喜欢穿过房子,但是到了其他地方只好钻洞,不能让别人看见对不对~~~。挖洞要时间啊,那就加1.
但Prairie想更早的见到……。求最小时间。
这个呢·这里时间为上,所以要以时间优先排列。。。时间小得排前面(因为每个节点具有的权值不一样,普通的队列只是按简
单的步数,所以要用优先队列)
#include<stdio.h>#include<queue>#include<string.h>#include<algorithm>using namespace std;int n,m;char mapp[1100][1100];int vis[1100][1100];int sx,sy,ex,ey;struct note{ int x,y,s; friend bool operator<(note a,note b) { return a.s>b.s; // 注意:( 大于< ) ( 小于> ) }};int dir[4][2]= { {0,1},{-1,0},{0,-1},{1,0}};int bfs(){ priority_queue<note>Q; note p,q; p.x=sx; p.y=sy; p.s=0; vis[sx][sy]=1; Q.push(p); while(!Q.empty()) { p=Q.top(); Q.pop(); if(p.x==ex&&p.y==ey) return p.s; for(int i=0; i<4; i++) { q=p; q.x+=dir[i][0]; q.y+=dir[i][1]; if(q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&!vis[q.x][q.y]) { vis[q.x][q.y]=1; if(mapp[q.x][q.y]=='.') q.s++; Q.push(q); } } }}int main(){ while(~scanf("%d%d",&n,&m)&&(n||m)) { memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) scanf("%s",mapp[i]); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); sx--,sy--,ex--,ey--; printf("%d\n",bfs()); } return 0;}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 位运算
- hdu 6077-Time To Get Up
- oracle 常用操作和入门基本语法
- HDU 6075 Questionnaire
- 最简单的IOS 分类
- HDU
- AI机器学习(二)让机器学会“异或”(C#)
- Python——常见错误备忘
- ci框架用active_record模型实现分页的技巧
- table边框
- 关于网站域名优化与使用需要注意的
- matlab2016b 安装与破解
- python 中 os 和 shutil 模块的常见用法
- Hibernate知识概要