题目链接:

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(广搜)-手写队列的更多相关文章

  1. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  2. poj 3984 迷宫问题 bfs

    学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用bfs ...

  3. POJ 3984 迷宫问题【BFS/路径记录/手写队列】

    迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...

  4. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  5. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  6. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  7. 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, ...

  8. POJ - 3984 迷宫问题 bfs解法

    #include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...

  9. POJ 3984 迷宫问题 (BFS + Stack)

    链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...

随机推荐

  1. 【概率证明】—— sum and product rules of probability

    1. sum and product rules of probability ⎧⎩⎨p(x)=∫p(x,y)dyp(x,y)=p(x|y)p(y) sum rule of probability 的 ...

  2. mysqls,为node.js而编写的sql语句生成插件 (crud for mysql).

    It is written in JavaScript,crud for mysql.You can also use transactions very easily. mysqls 一款专为nod ...

  3. 爬虫中之Requests 模块的进阶

    requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...

  4. C# 获取操作系统相关信息

    1.获取操作系统版本(PC,PDA均支持) Environment.OSVersion 2.获取应用程序当前目录(PC支持) Environment.CurrentDirectory 3.列举本地硬盘 ...

  5. Selenium的文件上传JAVA脚本

    在写文件上传脚本的时候,遇到了很多问题,包括元素定位,以及上传操作,现在总结下来以下几点: 1. 上传的控件定位要准确,必要时要进行等待 WebElement adFileUpload = drive ...

  6. 前端-Angular思维导图笔记

    看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记

  7. C#监测方法执行效率

    System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); // 开始监视代码运行时间 //需要监测的代码 dothing ...

  8. adb 通过 WiFi 连接 Android 设备

    PC 和 Android 设备连接在同一个局域网. 查看 Android 设备的 IP:设置 > WLAN > 选择连接的WiFi > 查看IP地址. PC 端执行: ping &l ...

  9. Verilog之$sreadmemb

    1 Memories  Memories file format is shown below, the address is specified as @ <address> in he ...

  10. 三维重建:QT+OpenNI+Kinect图像校正

    后记: 当时能不放弃这个方向是因为这里面涉及了一种很有效的三位场景存储方式,可能给出除图元建模之外的一种三维场景描述方式.这和Flash与位图的对比一样,基于图元的flash始终抵不过基于点描述的位图 ...