题目链接:hdu4198

题目大意:求起点S到出口的最短花费,其中#为障碍物,无法通过,‘.’的花费为1 ,@的花费为d+1。

需注意起点S可能就是出口,因为没考虑到这个,导致WA很多次.......

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
char map[505][505];
int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n,m,t;
int begin_x,begin_y,end_x,end_y;
struct node
{
int x,y,time;
friend bool operator < (node a,node b)
{
return a.time > b.time;
}
};
void bfs()
{
priority_queue <node> q;
node s,temp;
s.x = begin_x;
s.y = begin_y;
s.time = 0;
map[begin_x][begin_y] = '#';
q.push(s);
while(!q.empty())
{
temp = q.top();
q.pop();
if(temp.x == end_x && temp.y == end_y)
{
printf("%d\n",temp.time + 1);
return;
}
for(int i = 0 ; i < 4 ; i ++)
{
s.x = temp.x + d[i][0];
s.y = temp.y + d[i][1];
if(s.x < 0 || s.x >= n || s.y < 0 || s.y >= m || map[s.x][s.y] == '#')
continue;
if(map[s.x][s.y] == '.') s.time = temp.time + 1;
else s.time = temp.time + t + 1;
map[s.x][s.y] = '#';
q.push(s);
}
}
}
int main()
{
int T,i,j;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&t);
for(i = 0 ; i < n ; i ++)
{
scanf("%s",map[i]);
for(j = 0 ; j < m ; j ++)
{
if(map[i][j] == 'S')
{
begin_x = i;
begin_y = j;
}
if( (i == 0 || i == n - 1 || j == 0 || j == m - 1) && map[i][j] != '#')//刚开始用的else if,没有考虑起点也是终点的情况,WA了很多次
{
end_x = i;
end_y = j;
}
}
}
bfs();
}
return 0;
}

hdu 4198 Quick out of the Harbour(BFS+优先队列)的更多相关文章

  1. HDU - 4198 Quick out of the Harbour (BFS+优先队列)

    Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...

  2. hdu 4198 Quick out of the Harbour

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4198 Quick out of the Harbour Description Captain Cle ...

  3. hdu 4198:Quick out of the Harbour解题报告

    Quick out of the Harbour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  4. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  5. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  6. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  7. hdu 1026 Ignatius and the Princess I(BFS+优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...

  8. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  9. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

随机推荐

  1. iOS开发之在地图上绘制出你运行的轨迹

    首先我们看下如何在地图上绘制曲线.在Map Kit中提供了一个叫MKPolyline的类,我们可以利用它来绘制曲线,先看个简单的例子. 使用下面代码从一个文件中读取出经纬度,然后创建一个路径:MKPo ...

  2. ubuntu12.04管理员账户登录不了桌面,仅仅能客人会话登录

    ubuntu12.04管理员账户登录不了桌面,仅仅能客人会话登录 求助!!ubuntu12.04管理员账户登录不了桌面,仅仅能客人会话登录. 登录管理员账户时,输入password后,一直在登录界面循 ...

  3. Notepad++ 配置 支持jquery、html、css、javascript、php代码提示

    原文:Notepad++ 配置 支持jquery.html.css.javascript.php代码提示 官网下载:http://notepad-plus-plus.org/ 获取插件的方法:打开软件 ...

  4. mvc之验证IEnumerable<T> 类型,多选框验证

    原文:mvc之验证IEnumerable<T> 类型,多选框验证 假设我们有这么一种需求,我们要同时添加年级和年级下面的多个班级,我们一般会像下面这种做法. Action中我们这样接收: ...

  5. Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法

    原文:Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法 很多朋友做安装包的时候,所打包的软件需要.NET Framework之类的环境,他 ...

  6. LINUX下getsockopt和setsockopt函数

    这两个函数仅用于套接字. 函数原型: #include <sys/socket.h> #include <sys/types.h> int getsockopt(int soc ...

  7. 【SSRS】入门篇(四) -- 向报表添加数据

    原文:[SSRS]入门篇(四) -- 向报表添加数据 定义好数据集后 [SSRS]入门篇(三) -- 为报表定义数据集 ,就可以开始设计报表了,将要显示在报表的字段.文本框.图像和其他项从工具箱拖放到 ...

  8. js根据IP地址判断城市

    var province = '' ;var city = '' ;jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iploo ...

  9. NHProfiler使用方法

    NHProfiler使用方法 NHProfiler是一个针对Nhibernate运行分析的工具. 使用如下: (1)在创建ISessionFactory的项目中引用NHProfiler安装目录下的Hi ...

  10. selenium + python 部署自动化测试环境

    选择selenium和python其实是怀有私心的:码两行python,熟悉熟悉.    selenium优点很多,我最看重的是支持多语言,足够简单,同时支持浏览器. 实际工作中,简单实用真的太重要了 ...