BFS(最短路) HDOJ 4308 Saving Princess claire_
题意:一个(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_的更多相关文章
- hdu 4308 Saving Princess claire_
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Description Princess cla ...
- hdu 4308 Saving Princess claire_ BFS
为了准备算法考试刷的,想明确一点即可,全部的传送门相当于一个点,当遇到一个传送门的时候,把全部的传送门都压入队列进行搜索 贴代码: #include <iostream> #include ...
- HDU 4308 Saving Princess claire_(简单BFS)
求出不使用P点时起点到终点的最短距离,求出起点到所有P点的最短距离,求出终点到所有P点的最短距离. 答案=min( 不使用P点时起点到终点的最短距离, 起点到P的最短距离+终点到P的最短距离 ) #i ...
- HDU 4308 BFS Saving Princess claire_
原题直通车:HDU 4308 Saving Princess claire_ 分析: 两次BFS分别找出‘Y’.‘C’到达最近的‘P’的最小消耗.再算出‘Y’到‘C’的最小消耗,比较出最小值 代码: ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- 2012 #1 Saving Princess claire_
Saving Princess claire_ Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- ZOJ 3369 Saving Princess
Saving Princess Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- POJ 2251 Dungeon Master (BFS最短路)
三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...
随机推荐
- pycharm查看代码注释的方法,代码编写日志及作者信息等
竟然在边栏有个右键的快捷键.annotate可以查看代码书写日期及作者 鼠标悬停可以看到更加详细的时间等信息 原理应该是利用git blame
- 移动端CSS小结
Meta 标签 <meta name="viewport" content="width=device-width, user-scalable=no, init ...
- Eureka 简介
Eureka 简介
- HDU OJ Max sum 题目1003
#include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...
- Hive中行列转换
1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...
- Hybrid App适配Android注意点
近期把做好的ipad HTML5混合应用适配到android上,发现android的webview比 iPad差太多了,android4.4因为升级到chromium.和chrome内核一致,全部问题 ...
- Python遇到的零碎小问题
切记else语句的后面直接加冒号: 字符和数字绝对不能直接相加 对于字符与整数之间的转化 ord('E')可以将其转化为45,chr(65)可以将其转化为A 编写程序的时候尽量要考虑时间复杂度 app ...
- 排队理论之性能分析 - Little Law & Utilization Law
了解一个系统的性能一般是參考一些度量值(Metric),而怎样计算出这些Metric就是我们要讨论的.Little Law(排队理论:利特儿法则)和Utilization Law是Performanc ...
- web前端开发 代码规范 及注意事项
web前端开发 代码规范 及注意事项 外部命名规范 html .js .css文件名称命名规范 my_script.js my_camel_case_name.css my_index.html 路径 ...
- 5.eclipse 自带的jdk没有源码,改了它
其实JDK源码在安装的时候已经放在了jdk所在的目录下,只是eclipse使用 了不带有源码的jre,导致没找到对应的源码,点击 Window->Perference->Java-> ...