题目链接: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. ural1519插头DP

    1519. Formula 1 Time limit: 1.0 second Memory limit: 64 MB Background Regardless of the fact, that V ...

  2. three.js 源代码凝视(七)Math/Euler.js

    商域无疆 (http://blog.csdn.net/omni360/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:商域无疆 -  本博客专注于 敏捷开发 ...

  3. jmeter参数化之CSV Data Set Config

    在jmeter中,可以用CSV Data Set Config实现参数化. 1.准备参数化数据

  4. 在windows server里,对于同一个账号,禁止或允许多个用户使用该账户,同时登录

    开始 -> 运行 -> gpedit.msc -> 本地计算机 策略 -> 计算机配置 -> 管理模板 -> Windows 组件 -> 远程桌面服务 -&g ...

  5. SQL点滴27—性能分析之执行计划

    原文:SQL点滴27-性能分析之执行计划 一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<I ...

  6. Cacti+Nagios监控平台完美整合

    Cacti+Nagios监控平台完美整合 本博文出自51CTO博客吴光科博主,有任何问题请进入博主页面互动讨论!博文地址:http://wgkgood.blog.51cto.com/1192594/1 ...

  7. js 实现自动换行

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

  8. leetcode第33题--Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  9. foreach,foreachelse

    foreach,foreachelse Table of Contents目录 iteration 用于显示当前循环的执行次数[待考] first : 当前 foreach 循环第一次执行时 firs ...

  10. [转]当图片源大小大于ImageView大小时的处理方式(缩放)

    转一篇当图片源大小大于ImageView大小定死时的处理方法,但不适用于图片大小小于ImageView时的情况,因为inSampleSize不能<1, 谁有特别好的放大的解决方案,除了设置Ima ...