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 < ...
随机推荐
- InfluxDB useful commands
InfluxDB 配置文件地址:/etc/influxdb/influxdb.conf 通过curl写数据 curl -i -XPOST 'http://localhost:8086/write?db ...
- oracle技术总结
http://www.cnblogs.com/jimeper/ http://blog.csdn.net/dragonxiangfu http://www.boobooke.com/bbs/threa ...
- 发挥bat的作用
from 转自:http://blog.csdn.net/hitlion2008/article/details/7467252 1.什么是Windows BATCH BATCH也就是批处理文件,有时 ...
- 图像处理之基础---boxfiter
http://blog.sina.com.cn/s/blog_7221228801019yg2.html DSP6000图像位移与变形典型算法 http://blog.csdn.net/anson20 ...
- 一张图理清js原型链(通过内置对象的引用关系)
很多同学估计写了几年js也没有搞清内置对象之间的原型链关系,鄙人抽空手绘了一张简图,以作参考: 简单说明一下,上图中annonymous()函数相当于是所有函数的根(它本身也是函数),他上面提供了一些 ...
- Timus 1545. Hieroglyphs Trie的即学即用 实现字典提示功能
前面学了Trie,那么就即学即用.运用Trie数据结构来解决这道题目. 本题目比較简单,当然能够不使用Trie.只是多用高级数据结构还是非常有优点的. 题目: Vova is fond of anim ...
- jquery源码学习笔记一:总体结构
练武不练功,到老一场空.计算机也一样. 计算机的功,就是原理.如果程序员只会使用各种函数,各种框架,而不知其原理,顶多熟练工人而已.知其然,更要知其所以然. jquery我们用得很爽,但它究竟咋实现的 ...
- SSH常见错误
错误一: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml] ...
- C项目实践--图书管理系统(1)
1.功能需求分析 图书管理系统主要用于对大量的图书信息,包括书名.作者.出版社.出版日期.ISBN(书号)等进行增.删.改.查以及保存等操作.同时也包括对用户的管理,用户包括管理员和普通用户两种权限, ...
- 常用的Sublime Text插件及安装方法
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...