题目链接:hdu2822

会优先队列话这题很容易AC。。。。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 1005
using namespace std;
char map[N][N];
int v[N][N],d[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
int begin_x,begin_y,end_x,end_y,n,m;
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step > b.step;
}
};
void bfs()
{
memset(v,0,sizeof(v));
priority_queue <node> q;
node s,temp;
s.x = begin_x;
s.y = begin_y;
s.step = 0;
v[s.x][s.y] = 1;
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(s.x == end_x && s.y == end_y)
{
printf("%d\n",s.step);
return ;
}
for(int i= 0 ; i < 4 ; i ++)
{
s = temp;
s.x += d[i][0];
s.y += d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || v[s.x][s.y])
continue;
v[s.x][s.y] = 1;
if(map[s.x][s.y] == '.') s.step ++;
q.push(s);
}
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&m) && (n + m))
{
for(i = 0 ; i < n ; i ++)
scanf("%s",map[i]);
scanf("%d%d%d%d",&begin_x,&begin_y,&end_x,&end_y);
begin_x -- ; begin_y --; end_x -- ; end_y --;//题目给出的坐标都是从1开始计算的
bfs();
}
return 0;
}

hdu 2822 Dogs(优先队列)的更多相关文章

  1. hdu - 2822 Dogs (优先队列+bfs)

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

  2. hdu 2822 Dogs

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2822 Dogs Description Prairie dog comes again! Someda ...

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

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

  4. hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...

  5. HDU 5875 Function 优先队列+离线

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...

  6. HDU 2822

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

  7. 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列

    网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...

  8. hdu 5360 Hiking(优先队列+贪心)

    题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...

  9. HDU 5289 Assignment [优先队列 贪心]

    HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...

随机推荐

  1. 网页搜索功能 多表搜索sql

    SELECT ID, Title, FromTableFROM (SELECT ID, ArticleName AS Title, 'Article' AS FromTable        FROM ...

  2. C# 扩展方法克隆实体类

    using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Ru ...

  3. 初识CLR

    眨眼间我已经实习了半年时间并且转正了,身份也正式从一个学生转变为一个职场人,这个博客自从开始实习以来就一直没有更新过= =没错,就是我懒癌晚期,不过不行!一切都要开始走向正轨,此博会继续见证我的成长, ...

  4. 【JAVA编码专题】UNICODE,GBK,UTF-8区别

    简单来说,unicode,gbk和大五码就是编码的值,而utf-8,uft-16之类就是这个值的表现形式.而前面那三种编码是一兼容的,同一个汉字,那三个码值是完全不一样的.如"汉"的uncode值与g ...

  5. Even Tree

    Link: https://www.hackerrank.com/challenges/even-tree def search(a,b): # 根据核心算法和题目要求要筛选边 seen = {} s ...

  6. linux tar使用

    Linux  tar指令简单使用 -c:创建包,-x:解压或解包(-c和-x可理解为互逆运算),-t:查看包 -f:后加处理文件,必须放在参数组合的最后一位(tar  -cf  a.tar  1.tx ...

  7. 列表:一个打了激素的数组 - 零基础入门学习Python010

    列表:一个打了激素的数组 让编程改变世界 Change the world by program 列表:一个打了激素的数组 有时候我们需要把一堆东西暂时存储起来,因为他们有某种直接或者间接的联系,我们 ...

  8. 了不起的分支和循环01 - 零基础入门学习Python007

    了不起的分支和循环01 让编程改变世界 Change the world by program 我们今天的主题是"了不起的分支和循环",为什么小甲鱼不说C语言,不说Python了不 ...

  9. html 作业1

    <body bgcolor="#000000" topmargin="200px" leftmargin="200px" text=& ...

  10. 转:Reddit排名算法工作原理

    http://www.aqee.net/how-reddit-ranking-algorithms-work/ 这是一篇继<Hacker News 排名算法工作原理>之后的又一篇关于排名算 ...