题目传送门

题意:一个(r*c<=5000)的迷宫,起点'Y‘,终点'C',陷阱‘#’,可行路‘*’(每走一个,*cost),传送门P,问Y到C的最短路

分析:一道最短路问题,加了传送门的功能,那么第一次走到P时是最短的路径,之后再到P都不会比第一次短,所以将所有P看成一个点,走过就vis掉,剩下就是简单的BFS了

收获:1、复习BFS求最短路  2. memset不能乱用,尤其是数组很大时,容易爆内存

代码:

/************************************************
* Author :Running_Time
* Created Time :2015-8-22 11:56:55
* File Name :I.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
typedef pair<int, int> P;
const int N = 5e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct Node {
int x, y, step;
};
Node t[N/10];
char maze[N][N];
bool vis[N][N];
int n, m, cost, tot;
int sx, sy, ex, ey; bool judge(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] == '#') return false;
return true;
} int BFS(void) {
// memset (vis, false, sizeof (vis));
// memset (d, INF, sizeof (d));
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) vis[i][j] = false;
}
vis[sx][sy] = true;
queue<Node> Q; Q.push (Node {sx, sy, 0}); while (!Q.empty ()) {
Node u = Q.front (); Q.pop ();
for (int i=0; i<4; ++i) {
int tx = u.x + dx[i], ty = u.y + dy[i];
if (!judge (tx, ty)) continue;
vis[tx][ty] = true;
if (maze[tx][ty] == 'C') {
return u.step;
}
if (maze[tx][ty] == 'P') {
for (int i=1; i<=tot; ++i) {
t[i].step = u.step;
Q.push (t[i]);
vis[t[i].x][t[i].y] = true;
}
}
else {
Q.push (Node {tx, ty, u.step + 1});
}
}
} return -1;
} int main(void) {
while (scanf ("%d%d%d", &n, &m, &cost) == 3) {
sx = sy = 0; ex = ey = 0; tot = 0;
for (int i=1; i<=n; ++i) {
scanf ("%s", maze[i] + 1);
for (int j=1; j<=m; ++j) {
if (maze[i][j] == 'Y') {
sx = i, sy = j;
} else if (maze[i][j] == 'C') {
ex = i, ey = j;
} else if (maze[i][j] == 'P') {
t[++tot].x = i; t[tot].y = j;
}
}
} int res = BFS ();
if (res != -1) {
printf ("%d\n", res * cost);
} else puts ("Damn teoy!");
} return 0;
}

  

BFS(最短路) HDOJ 4308 Saving Princess claire_的更多相关文章

  1. hdu 4308 Saving Princess claire_

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Description Princess cla ...

  2. hdu 4308 Saving Princess claire_ BFS

    为了准备算法考试刷的,想明确一点即可,全部的传送门相当于一个点,当遇到一个传送门的时候,把全部的传送门都压入队列进行搜索 贴代码: #include <iostream> #include ...

  3. HDU 4308 Saving Princess claire_(简单BFS)

    求出不使用P点时起点到终点的最短距离,求出起点到所有P点的最短距离,求出终点到所有P点的最短距离. 答案=min( 不使用P点时起点到终点的最短距离, 起点到P的最短距离+终点到P的最短距离 ) #i ...

  4. HDU 4308 BFS Saving Princess claire_

    原题直通车:HDU 4308 Saving Princess claire_ 分析: 两次BFS分别找出‘Y’.‘C’到达最近的‘P’的最小消耗.再算出‘Y’到‘C’的最小消耗,比较出最小值 代码: ...

  5. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  6. 2012 #1 Saving Princess claire_

    Saving Princess claire_ Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  7. hdu----(4308)Saving Princess claire_(搜索)

    Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  8. ZOJ 3369 Saving Princess

    Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  9. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. jenkins修改日志级别方法

    1.jenkins日志有时候也会消耗掉很大内存,在传输时也会消耗掉大量带宽,如图,300+M的日志大小,太夸张了吧 2.修改日志级别的方法: 在配置文件里修改,重启后永久生效,配置路径:/etc/sy ...

  2. [转]linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结

    原文:http://www.jb51.net/LINUXjishu/43356.html ------------------------------------------------------- ...

  3. js中splice()的强大(删除,插入或替换数组的元素)

    1.删除-用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数) 2.插入-向数组指定位置插入任意项元素.三个参数,第一个参数(其实位置),第二个参数(0),第三个参数( ...

  4. 程序运行中(BSS段、数据段、代码段、堆栈)

    程序运行中(BSS段.数据段.代码段.堆栈) BSS段:(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简 ...

  5. 【拆分版】Docker-compose构建Zookeeper集群管理Kafka集群

    写在前边 在搭建Logstash多节点之前,想到就算先搭好Logstash启动会因为日志无法连接到Kafka Brokers而无限重试,所以这里先构建下Zookeeper集群管理的Kafka集群. 众 ...

  6. HDU 5274 Dylans loves tree 树链剖分+线段树

    Dylans loves tree Problem Description Dylans is given a tree with N nodes. All nodes have a value A[ ...

  7. Hibernate 之 二级缓存

    在上篇文章中我们对缓存以及Hibernate的一级缓存进行了介绍,接下来的主要内容将是Hibernate的二级缓存. 二级缓存也称为进程级的缓存或SessionFactory级的缓存,二级缓存可以被所 ...

  8. gdb条件断点

    1 通用格式 break file:line if condition condition的格式:变量名 条件运算符 常量,比如 if  i == 3 2 例 #include <stdio.h ...

  9. 8核 16g 及时释放内存空间

    del  释放 大变量 所在内存空间 GB数据

  10. C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法

    以前这个问题困扰我多天最后查资料一大堆,最后最终攻克了,看到非常多人做C#串口都遇到相同的问题,所以写一篇博文,以便学习交流. 一定要在com实例化的时候设置ReceivedBytesThreshol ...