题目链接: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. Python_生成測试数据

    本文出自:http://blog.csdn.net/svitter 生成1~10的随机数1000个: import random fp = open("test", 'w'); f ...

  2. 安卓MonkeyRunner源码分析之与Android设备通讯方式

    如前文<谁动了我的截图?--Monkeyrunner takeSnapshot方法源码跟踪分析>所述,本文主要会尝试描述android的自动化测试框架MonkeyRunner究竟是如何和目 ...

  3. 一个简单的dom查询函数

    var regid = /^#([\w-]*)$/, regClass = /^\.([\w-]*)$/, regName = /^(div|a|p|ul|li|input|select|docume ...

  4. Linux开源模块迁移概述暨交叉编译跨平台移植总结--从《嵌入式Linux驱动模板简洁和工程实践》

    本文摘录<嵌入式Linux驱动模板简洁和工程实践>一本书"开发和调试技术". Linux强大的是,有那么多的开源项目可以使用.通常非常需要可以通过寻找相关的源模块被定义 ...

  5. 快速构建Windows 8风格应用35-触控输入

    原文:快速构建Windows 8风格应用35-触控输入 引用 Windows 8设备通常具有多点触摸屏,用户可以同时使用多个手指来进行不同的输入交互,如点击.拖动或收缩等手势操作.另外Windows ...

  6. Spring IOC之Bean 概述

    1.Bean概述 一个Spring IOC容器管理一个或者多个bean.这些bean是根据你提供给容器的配置数据信息创建的,例如XML形式的的定义. 在容器内部,这些bean的定义表示为BeanDef ...

  7. restrict

    restrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式.即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其 ...

  8. leetcode 34 Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. Web前端框架与类库

    Web前端框架与类库的思考 说起前端框架,我也是醉了.现在去面试或者和同行聊天,动不动就这个框架碉堡了,那个框架好犀利. 当然不是贬低框架,只是有一种杀鸡焉用牛刀的感觉.网站技术是为业务而存在的,除此 ...

  10. 使用Oracle Wrap工具加密你的代码

    Oracle提供Wrap工具,可以用于加密你的Package等.不过需要注意的是,加密后的代码无法解密,你需要保管好你的源代码. 以下是个例子: 1.源代码 create or replace fun ...