POJ-3984-迷宫问题-BFS(广搜)-手写队列
题目链接: id=3984">http://poj.org/problem? id=3984
这个本来是个模板题,可是老师要去不能用STL里的queue,得自己手写解决。ORZ....看别人的博客学习。新技能get。。。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define LL long long
using namespace std;
int Map[10][10];
int last=0,total=1; // total为队列总元素,last为先驱标记。
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
struct node
{
int x,y,pre;
}q[30];
bool Isok(int x,int y) // 推断时候在迷宫内部。决定时候继续往下搜;
{
if(x<0||y<0||x>4||y>4||Map[x][y]) return false;
else return 1;
}
void print(int i) // 自己定义输出函数,调用递归,利用递归原理能够非常轻松的从后往前输出。
{
if(q[i].pre!=-1){ // 先驱为-1位起点;
print(q[i].pre);
printf("(%d, %d)\n",q[i].x,q[i].y);
}
}
void bfs(int x,int y)
{
q[last].x=x;
q[last].y=y;
q[last].pre=-1; // 起点,先驱标记为-1;
while(last<total){ // 推断队列是否为空;
for(int i=0;i<4;i++){ // 四个方向搜索。
int a=q[last].x+dir[i][0];
int b=q[last].y+dir[i][1];
if(Isok(a,b)){
//cout<<a<<' '<<b<<endl;
Map[a][b]=1;
q[total].x=a;
q[total].y=b;
q[total].pre=last; // 记录先驱;
total++; // 入队;
}
if(a==4&&b==4){
print(last);
}
}
last++; // 出队。
}
}
int main()
{
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
scanf("%d",&Map[i][j]);
}
}
printf("(0, 0)\n");
bfs(0,0);
printf("(4, 4)\n");
return 0;
}
POJ-3984-迷宫问题-BFS(广搜)-手写队列的更多相关文章
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...
- POJ 3984 迷宫问题【BFS/路径记录/手写队列】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...
- [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- 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, ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- POJ 3984 迷宫问题 (BFS + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
随机推荐
- SUSE glibc升级为2.18过程记录
先验知识:1.运行时,动态库的装载依赖于ld-linux.so.6的实现,它查找共享库的顺序如下:(1)ld-linux.so.6在可执行的目标文件中被指定,可用readelf命令查看(2)ld-li ...
- 饭卡(hdoj--2546--背包)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- 2015 多校赛 第一场 1002 (hdu 5289)
Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...
- web前端处理订单待支付倒计时计算显示问题
在商城类项目的时候,有很多待支付的订单,有时候在订单列表页面会分别显示倒计时,就是页面会有很多倒计时的订单. 处理方法: 1.调用后端接口拿到所有的订单,获取所有的倒计时订单,获取到期时间(尽量时间戳 ...
- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build*解决办法
easy_install -U setuptools or pip install ipython 亲测有效
- [原创]C++带空格字符串的输入问题
字符串一直是一个重点加难点,很多笔试面试都会涉及,带空格的字符串更是十分常见,现在对字符串的输入问题进行一下总结. C++用cin输入的时候会忽略空格以后的字符,比如 char a[100]; cin ...
- DataTable的Select()方法
DataRow[] partno = dtPack.Select("PK_SOHEAD = " + pk_sohead + " AND PART_NO = '" ...
- vue-阻止事件冒泡-开启右键-键盘类事件
一: 阻止事件冒泡 布局: 当点击按钮时,会触发button的click 也会触发父级的方法 <div id="box"> <div @click="p ...
- HDL之Bitwise operation
1 Verilog 1.1 Bitwise operator Bitwise operators perform a bit wise operation on two operands. They ...
- 【深入理解Java虚拟机】自动内存管理机制——垃圾回收机制
Java与C++之间有一堵有内存动态分配和垃圾收集技术所围成的"高墙",墙外面的人想进去,墙里面的人却想出来.C/C++程序员既拥有每一个对象的所有权,同时也担负着每一个对象生 ...