题目传送门

题意:一个(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. Web容器自己主动对HTTP请求中參数进行URLDecode处理

    这篇文章转载自 : Web容器自己主动对HTTP请求中參数进行URLDecode处理 如题.在Java中或许非常多人都没有注意到当我们发送一个http请求时,假设附带的參数被URLEncode之后,到 ...

  2. linux字符驱动之poll机制按键驱动

    在上一节中,我们讲解了如何自动创建设备节点,实现一个中断方式的按键驱动.虽然中断式的驱动,效率是蛮高的,但是大家有没有发现,应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情 ...

  3. volatile关键字解析&内存模型&并发编程中三概念

    原文链接: http://www.cnblogs.com/dolphin0520/p/3920373.html volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java5之前,它是一个 ...

  4. swiper插件制作轮播图swiper2.x和3.x区别

    swiper3.x仅仅兼容到ie10+.比較适合移动端. swiper3.x官网  http://www.swiper.com.cn/ swiper2.x能够兼容到ie7+.官网是http://swi ...

  5. MYSQL将时间格式化

    SELECT *,DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y/%m/%d %H:%i:%s") AS dateFormat FROM `I ...

  6. 深入浅出 - Android系统移植与平台开发(十二)- Android JNI机制

    第五章.JNI机制 4.1 JNI概述 由前面基础知识可知,Android的应用层由Java语言编写,Framework框架层则是由Java代码与C/C++语言实现,之所以由两种不同的语言组合开发框架 ...

  7. encodeURIComponent

    <script type="text/javascript"> function show(){ var f="#wer中文测试"; f = enc ...

  8. Win32 Windows编程 七

    定时器消息 1. WM_TIMER 依照定时器设置的时间段,自己主动向窗体发送一个定时器消息WM_TIMER.优先级比較低 定时器精度比較低.毫秒级别.消息产生时间也精度比較低 2 .消息和函数 WM ...

  9. spring的PROPAGATION_REQUIRES_NEW事务,下列说法正确的是(D)

    A:内部事务回滚会导致外部事务回滚 B:内部事务回滚了,外部事务仍可以提交 C:外部事务回滚了,内部事务也跟着回滚 D:外部事务回滚了,内部事务仍可以提交 PROPAGATION_REQUIRES_N ...

  10. Using Python with TurboGears A complete web framework integrating several Python projects

    Using Python with TurboGears TurboGears is a Python web framework based on the ObjectDispatch paradi ...