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 ...
随机推荐
- Android开发之一个未解决的bug
使用Activity之间传递数据的时候,出现了一个bug,但是没有找到哪里出错了. 把代码和log都记录下来,以后研究 代码: MainActivity.class package com.examp ...
- HTML5学习(八)----Web存储
参考地址:http://www.w3school.com.cn/html5/html_5_webstorage.asp 在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localS ...
- 如何从Linux源码获知版本信息
/*************************************************************************** * 如何从Linux源码获知版本信息 * 声明 ...
- JQuery focus()
跳转到登陆页面时可以使用focus方法 <input name="login_account"id="login_account"class=" ...
- 使用Spring Profile和Mybatis进行多个数据源(H2和Mysql)的切换
最近在做WebMagic的后台,遇到一个问题:后台用到了数据库,本来理想情况下是用Mysql,但是为了做到开箱即用,也整合了一个嵌入式 数据库H2.这里面就有个问题了,如何用一套代码,提供对Mysql ...
- [转]ubuntu zip 文件乱码解决 压缩乱码
ubuntu zip 文件乱码解决 压缩乱码 1.1 通过unzip行命令解压,指定字符集 unzip -O CP936 xxx.zip (用GBK, GB18030也可以) 有趣的是unzip的ma ...
- CentOS 6.4利用xampp安装bugfree3
1.下载xampp 安装 http://www.apachefriends.org/zh_cn/xampp.html 直接执行.run文件安装 默认会安装到/opt/lampp 2 .启动xampp ...
- PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端
demo效果图: PC端 android端 大体流程 android端是服务器端,绑定IP和端口,监听来自PC端的连接, pc端通过socket与服务器andorid端传输图片. 主要代码 andro ...
- 在App里面添加App Store中App链接的解决方法
详见stackoverflow.com/questions/433907/how-to-link-to-apps-on-the-app-store http://developer.apple.com ...
- HDU 4283 You Are the One (12年天津 区间DP)
题意:有一个队列,每个人有一个愤怒值a[i],如果他是第k个上场,不开心指数就为(k-1)*a[i].但是边上有一个小黑屋(其实就是个堆栈),可以一定程度上调整上场程序 思路:枚举区间和每个人第几个上 ...