poj--3984--迷宫问题(bfs+路径记录)
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Submit Status
Description
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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
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
Sample Output
(0, 0) (1, 0) (2, 0) (2, 1) (2, 2) (2, 3) (2, 4) (3, 4) (4, 4)
这道题不难,只是以前路径记录没有掌握好,水一水
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define MAX 10
int map[MAX][MAX];
bool vis[MAX][MAX];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
struct node
{int x,y,prex,prey;
};
node path[MAX][MAX];
node st,ed;
void putpath(int x,int y)
{stack<node>p;node now=path[x][y];while(1){p.push(now);if(now.x==0&&now.y==0)break;now=path[now.prex][now.prey];}while(!p.empty()){now=p.top();p.pop();printf("(%d, %d)\n",now.x,now.y);}
}
bool check(node a)
{if(map[a.x][a.y]==1||a.x<0||a.y>4||a.y<0||a.x>4)return 0;return 1;
}
void bfs()
{memset(vis,0,sizeof(vis));queue<node>q;st.x=0,st.y=0;q.push(st);vis[0][0]=1;while(!q.empty()){st=q.front();q.pop();if(st.x==4&&st.y==4){path[st.x][st.y]=st;break;}for(int i=0;i<4;i++){ed.x=st.x+dx[i];ed.y=st.y+dy[i];if(check(ed)&&!vis[ed.x][ed.y]){vis[ed.x][ed.y]=1;ed.prex=st.x;ed.prey=st.y;path[ed.x][ed.y]=ed;q.push(ed); }}}
}
int main()
{for(int i=0;i<5;i++){for(int j=0;j<5;j++)scanf("%d",&map[i][j]);}bfs();putpath(4,4);return 0;
}
转载于:https://www.cnblogs.com/playboy307/p/5273706.html
poj--3984--迷宫问题(bfs+路径记录)相关推荐
- POJ 3984 迷宫问题 BFS求最短路线+路径记录
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31050 Accepted: 17826 Descriptio ...
- POJ 3984 迷宫问题 BFS DFS两种解法
题目地址:点这里 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18495 Accepted: 10947 Descr ...
- poj 3984 迷宫问题 BFS
/* 题目: 求最少时间从(0,0)走到(4,4)的路径 分析: 纯粹BFS题目,不过需要打印路径,可以用数组记录当前的坐标的上一个坐标, 因为BFS构造出一棵BFS最优生成树,每一个节点的父母节点都 ...
- 【POJ3126 Prime Path】【POJ 3087 Shuffle'm Up】【UVA 11624 Fire!】【POJ 3984 迷宫问题】
POJ3126Prime Path 给定两个四位素数a b,要求把a变换到b 变换的过程要 每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与 前一步得到的素数 只能有一个位 ...
- (简单) POJ 3984 迷宫问题,BFS。
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- POJ 3984 迷宫问题 (Dijkstra)
迷宫问题 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
- POJ - 3984+POJ - 3414(BFS+路径记录)
题目一 迷宫问题 题目链接: http://poj.org/problem?id=3984 题目: Description 定义一个二维数组: int maze[5][5] = {0, 1, 0, 0 ...
- POJ 3984 迷宫问题
定义一个二维数组: 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, ...
- K - 迷宫问题 POJ - 3984
K - 迷宫问题 POJ - 3984 5*5迷宫,输出从(0,0)走到(4,4)的最短路径 类似康托,自己弄个一一对应的公式即可 #include<iostream> #include& ...
最新文章
- 资源盗链困扰站长 安全狗内置盗链保护功能
- 关闭iOS软键盘的小方法
- 5G NR RLC:RLC架构和RLC entity
- FFmpeg基本组成
- android——根据MVC框架设计的结构
- 重要的Python数据分析库
- 虚拟机XP系统突然变卡顿解决办法
- python多因素方差分析_统计科学之多因素方差分析
- Unity2d 坦克大战 (二)道具效果实现
- repeate数据展示
- 真西游记java,原来《西游记》原著真的很吓人,我们都被86版西游记骗了?
- 关于延拓定理的一点注解
- WebApi编程(一)-DOM
- 华宇软件华为鸿蒙,舒华体育携手华为打造:全球首款搭载鸿蒙操作系统跑步机面世...
- RANSAC算法(仅供学习使用)
- JavaScript:实现Sudoku数独游戏算法(附完整源码)
- 截取图片DEMO. JAVA Windows FFmpeg
- 华为无线设备配置同一业务VLAN的AP间快速漫游
- MATLAB机器人工具箱(四)动力学
- 小式智能客服机器人严肃升级!!