poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧。
然后就是可以从别人的代码里学到不一样的思路和想法。
这题就是求最短的路径,首先想到就是用bfs,但是求到最短之后不知道怎么输出了咋办?很着急有木有???
基本的广搜题已经做的挺熟练的,但是这个记录路径还是没搞懂,大致的方向还是明白的,记录,递归输出。。。。
然后我就找到了写得不错的代码。。然后加上了我“本地化”处理,啊哈~~~~~~~·
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int map[5][5],vis[5][5],pre[100];
struct loca{
int x;
int y;
}list[50]; int dir[8] = {0,1,0,-1,1,0,-1,0}; int judge(int x,int y)
{
if(x>=1&&x<5&&y>=0&&y<5&&!map[x][y])
return 1;
return 0;
} void print(int x) //递归输出哈,菜鸟学习了。。。
{
int t;
t = pre[x];
if(t == 0)
{
printf("(0, 0)\n");
printf("(%d, %d)\n",list[x].x,list[x].y);
return ;
}
print(t);
printf("(%d, %d)\n",list[x].x,list[x].y);
} void bfs()
{
int i,head,tail;
int x,y,xx,yy;
memset(vis,0,sizeof(vis));
head = 0;
tail = 1;
list[0].x = 0;
list[0].y = 0;
pre[0] = -1;
while(head < tail)
{
x = list[head].x;
y = list[head].y;
if(x ==4 && y==4)
{
print(head);
return ;
}
for(i=0;i<8;i+=2)
{
xx = x + dir[i];
yy = y + dir[i+1];
if(!vis[xx][yy] && judge(xx,yy))
{
vis[xx][yy] = 1;
list[tail].x = xx;
list[tail].y = yy;
pre[tail] = head;
tail++;
}
}
head ++;
}
return ;
} int main(void)
{
int i,j;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&map[i][j]);
bfs();
return 0;
}
poj 3984 迷宫问题 bfs的更多相关文章
- 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一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
随机推荐
- poj 水题系列
题目:http://poj.org/problem?id=3006 筛选法求素数 #include <iostream> #include<cstdio> #include&l ...
- ShareSDK 实现新浪微博分享(微信,QQ,新浪微博类似)
1 . 分享(前提是集成了sdk,配置好了Key),只要实现点击事件,调用shareSina(); ShareSDK.initSDK(this); private void shareSina() { ...
- Azure HDInsight与Hadoop周边系统集成
Sunwei 9 Dec 2014 1:54 AM 传统的Hadoop系统提供给用户2个非常优秀的框架,MR计算框架和HDFS存储框架,尽管MR已经显得有些老迈而缓慢,但是HDFS还是很多应用系统的 ...
- C语言数组和指针的理解_在取地址运算上的操作_指针加减操作_a 和&a 的区别
1.一个实例+理论分析 在了解数组和指针的访问方式前提下,下面再看这个例子: main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); pr ...
- 临时禁用Resharper
Visual Studio 菜单 –> 工具 –> 选项 –> ReSharper –> Suspend按钮
- [转载]C#基础-Func,Action
Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...
- 使用DialogFragment创建对话框总结
回调Activity中的函数 http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents 在DialogFragme ...
- Mongo DB 安装-及分布式集群部署(初稿)
一.安装步骤, 1, 下载最新的Mongo DB数据库:http://www.mongodb.org/downloads?_ga=1.44426535.2020731121.1421844747\ 下 ...
- Java 时间转换问题总结
这几天开发中遇到时间转换出错的问题,特总结如下: ========================================================================= ...
- algorithm: heap sort in python 算法导论 堆排序
An Python implementation of heap-sort based on the detailed algorithm description in Introduction to ...