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 ...
随机推荐
- Python小白学习之基础知识(个人笔记)
介绍while else的使用,这个不常用 格式化输出 while esle ,当循环内有break语句时,不执行else语句,当没有break语句时,执行完while循环,然后执行else下面的语句 ...
- Jmeter使用JDBC链接数据库进行压力测试
一.关于性能测试 对数据库进行压测时,我们需要关注的几个方面: 1.系统相关指标,诸如:系统CPU/内存/IO等 2.进程相关指标,诸如:mysql该数据库的对应的进程占用CPU/内存/IO等 3.数 ...
- Jmeter资源监控工具ServerAgent运行原理的一些研究
用过Jmeter的应该都了解,有个ServerAgent工具,放在linux或者windows服务器上开启服务后,在Jmeter中配置下监视器,就可以抓取到服务器的一些资源信息,抓取的主要是cpu.内 ...
- NO--09今天遇到的一点小问题之axios全局注册
今天用 Vue 写项目的时候,用到 axios ,因为 axios 不能用 Vue.use() ,所以在每个 .vue 文件中使用 axios 时就需要 import , .vue 文件少的话还好说, ...
- iOS 播放音频文件
// 播放音乐 NSString *path = [[NSBundle mainBundle] pathForResource:@"1670" ofType:@&qu ...
- HashMap 和 HashTable 到底哪不同 ?
HashMap 和 HashTable 到底哪不同 ? 2017/05/29 | 分类: 基础技术 | 1 条评论 | 标签: HASHMAP, HASHTABLE 分享到: 原文出处: 程序员赵鑫 ...
- 《图解 HTTP 》阅读 —— 第五章
第5章 与HTTP协作的web服务器 一台服务器可以托管多个域名. 在相同的IP地址下,虚拟主机可以寄存多个不同主机名和域名的网站,所以在发送HTTP请求时,必须在Host首部内指定完整的主机名和域名 ...
- 17 Tips For Writing An Excellent Email Subject Line
Out of the billions of emails that are sent every day, how can you make sure that yours stands out? ...
- 第10次Scrum会议(10/22)【欢迎来怼】
一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/22 17:20~17:33,总计13min.地点:东北师范 ...
- Visual C++ 8.0对象布局
哈哈,从M$ Visual C++ Team的Andy Rich那里又偷学到一招:VC8的隐含编译项/d1reportSingleClassLayout和/d1reportAllClassLayout ...