hdu - 2822 Dogs (优先队列+bfs)
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)的更多相关文章
- hdu 2822 Dogs
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2822 Dogs Description Prairie dog comes again! Someda ...
- hdu 2822 Dogs(优先队列)
题目链接:hdu2822 会优先队列话这题很容易AC.... #include<stdio.h> #include<string.h> #include<queue> ...
- 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 ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
随机推荐
- negroni-gzip源码简单分析解读
negroni-gzip源码简单分析解读 这是一个为Negroni设计的gzip压缩处理中间件,需要用到已有的compress中的gzip,阅读了不长的源码之后,总结了一些关键要点和注意点. 检查是否 ...
- Android 给按钮添加监听事件
在安卓开发中,如果要给一个按钮添加监听事件的话,有以下三种实现方式 1.方式一 public class MainActivity extends ActionBarActivity { @Overr ...
- git忽略文件权限的检查
在linux上配置了一个samba服务器,方便在linux上通过ide修改代码,然后发现一个很烦人的问题,就是没有修改权限,在使用命令 chmod 777 filename后可以修改了,然而使用git ...
- spark集群启动步骤及web ui查看
集群启动步骤:先启动HDFS系统,在启动spark集群,最后提交jar到spark集群执行. 1.hadoop启动cd /home/***/hadoop-2.7.4/sbinstart-all.sh ...
- c++通过管道pipe获取cmd输出的字符
#include <stdio.h>#include<iostream>#include<string>using namespace std; // 描述:exe ...
- OpenFlow_tutorial_3_Learn_Development_Tools
一.Several Utilities OpenFlow Tutorial VM 中预装了一些OpenFlow特性的工具和一般通用网络的工具. 1.Openflow Controller:处于Open ...
- 【Lucene】实现全文索引
2. Lucene 实现全文检索的流程2.1.索引和搜索流程图 绿色表示索引过程,对要搜索的原始内容进行索引构建一个索引库,索引过程包括:确定原始内容即要搜索的内容 -> 采集文档 -> ...
- cf536d——优先队列的运用
题目 题目:Lunar New Year and a Wander 题目大意:给定一个n个顶点(编号1~n).m条边的图,求从顶点1出发的字典序最小的路径(途径的边可重复). 思路 使用一个优先队列就 ...
- chfn - 改变你的finger讯息
总览 SYNOPSIS chfn [ -f full-name ] [ -o office ] [ -p office-phone ] [ -h home-phone ] [ -u ] [ -v ] ...
- ascii - 在八进制,十进制,十六进制中的 ASCII 字符集编码
描述 ASCII 是美国对于信息交换的标准代码,它是7位码,许多8位码(比如 ISO 8859-1, Linux 的默认字符集)容纳 ASCII 作为它们的下半部分.对应的国际 ASSII 是 ISO ...