POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径)
题意分析
定义一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
简单的BFS,路径记录开一个二维数组就好。
代码总览
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define nmax 6
using namespace std;
int mp[nmax][nmax];
typedef struct{
int x;
int y;
bool isvis;
}mes;
typedef struct{
int x;
int y;
}point;
mes visit[nmax][nmax];
int spx[5] = {0,1,0,-1};
int spy[5] = {1,0,-1,0};
bool check(point temp)
{
if(temp.x <0 || temp.x >=5 ||temp.y < 0 || temp.y >=5 || visit[temp.x][temp.y].isvis|| mp[temp.x][temp.y] == 1)
return false;
else return true;
}
void bfs()
{
memset(visit,0,sizeof(visit));
queue<point> q;
while(!q.empty()) q.pop();
point temp = {0,0},head;
visit[temp.x][temp.y].isvis = true;
q.push(temp);
while(!q.empty()){
head = q.front(); q.pop();
if(head.x == 4 && head.y == 4){
return;
}
for(int i = 0;i<4;++i){
temp.x = head.x + spx[i];
temp.y = head.y + spy[i];
if(check(temp)){
visit[temp.x][temp.y].isvis = true;
visit[temp.x][temp.y].x = head.x;
visit[temp.x][temp.y].y = head.y;
q.push(temp);
}
}
}
}
void output(point temp)
{
point h;
vector<point> v; v.clear();
while(1){
v.push_back(temp);
if(temp.x == 0 && temp.y == 0) break;
h.x = visit[temp.x][temp.y].x;
h.y = visit[temp.x][temp.y].y;
temp = h;
}
for(int i = v.size()-1;i>=0;--i){
printf("(%d, %d)\n",v[i].x,v[i].y);
}
}
int main()
{
for(int i = 0;i<5;++i){
for(int j = 0;j<5;++j){
scanf("%d",&mp[i][j]);
}
}
bfs();
output({4,4});
return 0;
}
POJ.3894 迷宫问题 (BFS+记录路径)的更多相关文章
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
随机推荐
- 使用Git提交与管理代码
要成为一个合格的码农,必须要有效对自己的代码进行管理,最近打算有空就整理一下自己以前写得代码,把不涉及实验室的机密的内容等放到github上,之前只知道git clone 别人的代码,希望有一天自己的 ...
- php+MySQL的对用户表分表,使用户均匀分布
假如说我们目前已有一亿个注册用户,要把这些用户平均分配到100张表中,并且后续注册的用户也要均匀分配到这100张表 首先当用户注册时,如用户名为“username”,用php的crc32()函数处理用 ...
- Git生成多个ssh key
在实际的工作中, 有可能需要连接多个远程仓库, 例如我想连接私有仓库.GitLab官网.GitHub官网, 那么同一台电脑就要生成多个ssh key: ssh-keygen -t rsa -C &qu ...
- windows环境下apache-apollo服务器搭建及发布订阅测试
查证了一些资料之后,发现 apache-apollo服务器使用的人还是挺多的,资料也比较齐全,所以直接选择 apache-apollo了,具体性能如何,先用起来再说吧: 1.下载 apache-apo ...
- NOIP2018出征策
蒟蒻的风之旅人即将退役,现在分享一下退休前的故事 首先,经过这么多时间的划水训练,我成功从一个萌新变成了一个蒟蒻.我学会了各种奇怪玄学的算法,比如说昨天老师讲的NOIP第三题通用的算法,叫做XG算法, ...
- nginx 源码阅读 core
ngx_config.h 数据对齐 #define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) ngx_core.h #define ng ...
- 关于XSS的一些知识点
安全套接层(SSL)无助于减少XSS攻击.当Web浏览器使用SSL的时候,在网络中传送的数据是经过加密的,但是因为XSS攻击是在客户机器上发生的,所以数据已经被解密了,这时,攻击者仍然能够利用XSS安 ...
- vue+vue-video-player实现弹窗播放视频
将视频播放器标签放在对话框标签中,实现弹窗 template 中 <el-dialog :visible.sync="dialogVisible" width='680px' ...
- 2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest Problem H. Pair: normal and paranormal
题目链接:http://codeforces.com/group/aUVPeyEnI2/contest/229669 时间限制:1s 空间限制:64MB 题目大意:给定一个长度为2n,由n个大写字母和 ...
- goroutine与channels
goroutine(协程) 大家都知道java中的线程Thread,golang没有提供Thread的功能,但是提供了更轻量级的goroutine(协程),协程比线程更轻,创办一个协程很简单,只需要g ...