hdu 2822 Dogs(优先队列)
题目链接: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(优先队列)的更多相关文章
- hdu - 2822 Dogs (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2822 给定起点和终点,问从起点到终点需要挖几次只有从# 到 .或者从. 到 . 才需要挖一次. #includ ...
- hdu 2822 Dogs
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2822 Dogs Description Prairie dog comes again! Someda ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- hdu 1509 & hdu 1873 & hdu 1896 (基础优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的 ...
- HDU 5875 Function 优先队列+离线
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5875 Function Time Limit: 7000/3500 MS (Java/Others) ...
- HDU 2822
Dogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- hdu 5360 Hiking(优先队列+贪心)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L, ...
- HDU 5289 Assignment [优先队列 贪心]
HDU 5289 - Assignment http://acm.hdu.edu.cn/showproblem.php?pid=5289 Tom owns a company and he is th ...
随机推荐
- DOM事件处理程序-事件对象-键盘事件
事件流: 事件流--描述的是从页面中接受事件的顺序 IE ---事件冒泡流:即事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播至最不具体的那个节点(文档). Netsc ...
- puppet 4.4 System Requirements
puppet是linux下自动部署管理工具,有apply,agent/server两种模式,安装后默认为agent/server模式. apply模式下,每台机器均有自己的catalog文件,如果需要 ...
- Java数据输入
以下是数据输入实例: //以下是数据输入实例 import java.util.Scanner;//导入java.util.Scanner,Scanner首字母大写 public class Test ...
- iOS_SN_深浅拷贝( 百度的)_转载
文章原地址:http://www.cnblogs.com/5ishare/p/4362459.html 深浅拷贝前提是:是实现NSCopying或者NSMutableCopying协议. 浅拷贝只是复 ...
- VS2015试验随手记
1.第一次安装时,未完整安装,没有安装MFC,导致可以创建MFC工程,但是不能编译 解决办法,修改安装,加入MFC 2.学习创建windows runtime component,第一次使用,可以得到 ...
- android中细节效果总结
android中细节效果总结 andorid取消最上方的标题同时全屏显示 Source code protected void onCreate(Bundle savedInstanceS ...
- 三个重要的游标sp_cursoropen
請問這三個存諸過程的作用是什么﹖ sp_cursoropen, sp_cursorfetch, sp_cursorclose API 服务器游标实现 SQL Server OLE DB 提供程序. ...
- 03--理解HelloWorld结构
作为程序猿还是要代码来说事的,现在开始进入到具体的代码中来.国际惯例HelloWorld打头阵,我也不能免这个俗. Win32入口函数中主要代码如下: main.cpp // 创建应用实例 AppDe ...
- js 16进制字符串互转
/** * 16进制转换为字符串 * @param hex * @returns {*} */ function hexToString(hex) { var tmp = ''; if (hex.le ...
- linux常用命令(6)mv命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录.1 命令格式:mv [选项] 原文件或目录 ...