http://acm.hdu.edu.cn/showproblem.php?pid=2822

给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到  . 才需要挖一次。

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = ;
int n,m;
int sx,sy,ex,ey;
char maze[maxn][maxn];
int vis[maxn][maxn];
int dir[][]={-,,,,,,,-};
struct point
{
int x,y,step;
char z;
bool operator < (const point a) const
{
return step>a.step;
}
}; int bfs()
{
// printf("%d %d\n",a,b);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
vis[i][j]=<<;
priority_queue<point>que;
point s;
s.x=sx;s.y=sy;s.step=;s.z='X';
que.push(s);
vis[s.x][s.y]=;
while(!que.empty())
{
point e=que.top();que.pop();
//printf("%d %d %d\n",e.x,e.y,e.step);
if(e.x==ex&&e.y==ey) return e.step;
for(int i=;i<;i++)
{
s=e;
s.x=e.x+dir[i][];
s.y=e.y+dir[i][];
if(s.x>=&&s.x<n&&s.y>=&&s.y<m)
{
if(maze[s.x][s.y]=='X') s.step=e.step;
else if(maze[s.x][s.y]=='.') s.step=e.step+;
if(s.step<vis[s.x][s.y])
{
vis[s.x][s.y]=s.step;
que.push(s);
}
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break;
for(int i=;i<n;i++)
{
scanf("%s",maze[i]);
// printf("%s\n",maze[i]);
}
scanf("%d %d",&sx,&sy);
scanf("%d %d",&ex,&ey);
//printf("%d%d%d%d\n",sx,sy,ex,ey);
sx--,sy--,ex--,ey--;
printf("%d\n",bfs());
}
return ;
}

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

  1. hdu 2822 Dogs

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

  2. hdu 2822 Dogs(优先队列)

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

  3. hdu 1026 Ignatius and the Princess I【优先队列+BFS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  5. ZOJ 649 Rescue(优先队列+bfs)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  6. 【POJ3635】Full Tank 优先队列BFS

    普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...

  7. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  8. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

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

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

随机推荐

  1. 为OS X开发者准备的15个超棒应用

    几乎所有的开发人员在他们日常的开发工作中都有他们自己不可缺少的工具或实用程序集. 这些工具中的每一个都提供了特定的功能,大多数开发者都已经将他们集成到了其工作流程中. 使用这些工具或实用程序不单单只是 ...

  2. hihocoder offer收割编程练习赛10 C 区间价值

    思路: 令v[l, r](0<= l <= r < n)表示区间[l,r]的价值,则长度为n的区间的价值最少为0,最多为n*(n-1)/2.整体对价值二分,求能满足sum{v[l, ...

  3. Shiro 自定义登陆、授权、拦截器

    Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...

  4. 利用nginx与nginx-rtmp-module搭建流媒体服务器实现直播

    使用环境是centos 7.0+nginx:可以实现简单的流媒体服务. 先下载nginx-rtmp-module拓展: nginx-rtmp-module的官方github地址:https://git ...

  5. vultr服务器L2TP搭建

    前期准备,购买外服,选择vultr服务商,可选择洛杉矶的,系统为Ubuntu 14.04 x64 一.安装L2TP/IPSec wget --no-check-certificate https:// ...

  6. (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务

    http://blog.csdn.net/yerenyuan_pku/article/details/72514034 Restful就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格 ...

  7. WebService 生成客户端

    webservice 生成客户端 wsdl2java -encoding UTF-8 -p com.trm -d D:\happywork -client http://localhost/trm-t ...

  8. jquery 点击切换div

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. call, apply, bind 区别

    #call, apply, bind 区别及模拟实现call apply bind 三者都可以用来改变this的指向,但是在用法上略有不同  首先说一下call和apply的区别 call和apply ...

  10. jQuery动态移除和绑定事件

    function bindEvent() { //移除绑定事件 $('.btnsp').unbind('click'); //绑定事件 $('.btnsp').bind('click', functi ...