地下迷宫(bfs输出路径)



题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了;
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
#pragma(1)
typedef struct Node{
int x, y;
int t;
int sz;
friend bool operator < (Node a, Node b){
return a.t > b.t;
}
}Node; Node node[];
priority_queue<Node>Q;
int vis[][];
int mp[][];
int pre[];
int disx[] = {,,,-};
int disy[] = {,-,,};
int mv(int x, int y){
if(x == ){
if(y == ){
return ;
}
else if(y == -){
return ;
}
}else if(x == ){
if(y == )
return ;
}else if(x == -){
if(y == )
return ;
}
}
void print(int sz){
if(sz == )return;
print(pre[sz]);
printf(",[%d,%d]", node[sz].x, node[sz].y);
}
void bfs(int n, int m, int p){
Node a,b;
a.x = , a.y = , a.t = p, a.sz = ;
while(!Q.empty()){
Q.pop();
}
Q.push(a);
memset(vis, , sizeof(vis));
memset(pre, , sizeof(pre));
memset(node, , sizeof(node));
vis[][] = ;
node[] = {, , , };
int sz = ;
while(!Q.empty()){
a = Q.top();
Q.pop();
for(int i = ; i < ; i ++){
b.x = a.x + disx[i];
b.y = a.y + disy[i];
b.t = a.t - mv(disx[i], disy[i]);
b.sz = ++sz;
node[sz] = {b.x, b.y, b.t, b.sz};
pre[sz] = a.sz;
if(b.x < || b.y < || b.x >= n || b.y >= m)continue;
if(vis[b.x][b.y])continue;
if(b.t < )continue;
if(mp[b.x][b.y] == )continue;
if(b.x == && b.y == m - ){
printf("[%d,%d]",,);
print(sz);
puts("");
return;
}
vis[b.x][b.y] = ;
Q.push(b);
}
}
puts("Can not escape!");
return;
}
int main(){
int n, m, q;
while(~scanf("%d%d%d", &n, &m, &q)){
memset(mp, , sizeof(mp));
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
scanf("%d", &mp[i][j]);
}
}
bfs(n, m, q);
}
return ;
}
地下迷宫(bfs输出路径)的更多相关文章
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
		http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ... 
- 蓝桥T291(BFS + 输出路径)
		http://lx.lanqiao.org/problem.page?gpid=T291 学霸的迷宫 时间限制:1.0s 内存限制:256.0MB 问题描述 学霸抢走了大家的作业,班 ... 
- poj 3414 Pots(bfs+输出路径)
		Description You are given two pots, having the volume of A and B liters respectively. The following ... 
- bfs输出路径 && 最短路(迪杰斯特拉)输出路径
		问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ... 
- PTA 7-33 地下迷宫探索(深搜输出路径)
		地道战是在抗日战争时期,在华北平原上抗日军民利用地道打击日本侵略者的作战方式.地道网是房连房.街连街.村连村的地下工事,如下图所示. 我们在回顾前辈们艰苦卓绝的战争生活的同时,真心钦佩他们的聪明才智. ... 
- 迷宫问题---poj3984(bfs,输出路径问题)
		题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x, pre[x][y].y)而来: #include<stdio.h> #includ ... 
- POJ 3414 Pot (输出路径)【BFS】
		<题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯 ... 
- Pots--poj(bfs,输出路径)
		http://poj.org/problem?id=3414 题意: 给你两个容量为a,b的杯子:有3个操作: 1:FILL(i):把第i个杯子从水库中装满: 2:DROP(i):把第i个杯子清空: ... 
- POJ.3894  迷宫问题 (BFS+记录路径)
		POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ... 
随机推荐
- JSP实现分页功能
			分页须知知识点: (1)JDBC2.0的可滚动结果集. (2)HTTP GET请求. 一.可滚动结果集 Connection con = DriverManager.getConnection( ... 
- samba后台进程及安全模式简介
			识别 Samba 后台程序Linux 服务器通常作为守护程序(daemon) 来实现,这一词源于希腊神话,其中守护神(daemon)是超自然生物.Linux 守护程序在后台运行以便执行一些有用的任务. ... 
- nignx日志格式
			web-master的nginx格式: log_format web_format '$remote_addr $remote_port $remote_user [$time_local] ' '& ... 
- SQL Server -ISNULL()函数
			SQL中有多种多样的函数,下面将为您介绍SQL中的ISNULL函数,包括其语法.注释.返回类型等,供您参考,希望对您学习SQL能够有所帮助. ISNULL 使用指定的替换值替换 NULL. 语法ISN ... 
- 常见的DoDataExchange什么意思
			该函数中的代码是由ClassWizard自动加入的.DoDataExchange只有一个参数,即一个CDataExchange对象的指针pDX.在该函数中调用了DDX函数来完成数据交换,调用DDV函数 ... 
- CString与char *互转总结
			1 前言 今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结. 首先大家得清楚一件事,一般在网上提出问题的人大 ... 
- windows server 2008 设置多用户同时远程登录
			>Windows server 2008默认只支持一个administrator用户登陆,一个登录后另一个就被踢掉了,有没有办法像Windows Server 2003那样允许多用户用同时同一个 ... 
- hdfs的实现机制和文件系统概念
			1.HDFS的诞生背景: 数据量太大,在一个结点(机器)存不下.所以需要分布式存储,HDFS就是hadoop的分布式文件系统,来存储分布式数据. 2.共享文件系统也是一种分布式存储但有缺点:1.并发差 ... 
- AngularJS中的控制器示例
			<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ... 
- python 加密模块安装
			我们使用Python做加密算法如AES.MD5.SHA等时,需要用到PyCrypto模块 PyCrypto模块的安装方法 1.一般在官方网站下载: https://www.dlitz.net/soft ... 
