POJ 3984 迷宫问题 bfs 难度:0
http://poj.org/problem?id=3984
典型的迷宫问题,记录最快到达某个点的是哪个点即可
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=10;
const int inf=0x3fffffff;
struct pnt {
int x,y;
pnt(){x=y=0;}
pnt(int tx,int ty){x=tx,y=ty;}
};
int maz[maxn][maxn];
int step[maxn][maxn];
pnt from[maxn][maxn];
int n,m; queue<int> que;
const int dx[8]={0,0,1,-1,1,1,-1,-1};
const int dy[8]={1,-1,0,0,1,-1,1,-1};
bool in(int x,int y){
return x>=0&&x<n&&y>=0&&y<m;
}
void bfs(int sx,int sy){
while(!que.empty())que.pop();
memset(step,0x3f,sizeof(step));
step[sx][sy]=0;
que.push(sx*maxn+sy);
while(!que.empty()){
int x=que.front()/maxn,y=que.front()%maxn;que.pop();
if(x==4&&y==4)break;
for(int i=0;i<4;i++){
int tx=x+dx[i],ty=y+dy[i];
if(in(tx,ty)&&maz[tx][ty]!=1&&step[tx][ty]>step[x][y]+1){
step[tx][ty]=step[x][y]+1;
from[tx][ty]=pnt(x,y);
que.push(tx*maxn+ty);
}
}
}
}
pnt heap[maxn*maxn];int hlen;
int main(){
n=5,m=5; for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",maz[i]+j);
}
}
bfs(0,0);
pnt ask=pnt(4,4);
heap[hlen++]=ask;
do{
ask=from[ask.x][ask.y];
heap[hlen++]=ask;
}while(ask.x!=0||ask.y!=0); for(int i=hlen-1;i>=0;i--){
printf("(%d, %d)\n",heap[i].x,heap[i].y);
} return 0;
}
POJ 3984 迷宫问题 bfs 难度:0的更多相关文章
- poj 3984 迷宫问题 bfs
学会这道水题之后我懂得了不少哈,首先水题也能学到不少知识,尤其像我这样刚入门的小菜鸟,能学到一些小技巧. 然后就是可以从别人的代码里学到不一样的思路和想法. 这题就是求最短的路径,首先想到就是用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最短路径的记录和打印问题)
题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...
- POJ 2251 Dungeon Master bfs 难度:0
http://poj.org/problem?id=2251 bfs,把两维换成三维,但是30*30*30=9e3的空间时间复杂度仍然足以承受 #include <cstdio> #inc ...
- 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 + Stack)
链接 : Here! 思路 : BFS一下, 然后记录下每个孩子的父亲用于找到一条路径, 因为寻找这条路径只能从后向前找, 这符合栈的特点, 因此在输出路径的时候先把目标节点压入栈中, 然后不断的向前 ...
- POJ - 3984 迷宫问题 bfs解法
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> usi ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题(简单bfs+路径打印)
传送门: http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
随机推荐
- EOS Dapp开发(1)-基于Docker的开发环境搭建
随着EOS主网的上线,相信基于EOS的Dapp开发会越来越多,查阅了很多资料相关的开发资料都不是很多,只能自己摸索,按照网上仅有的几篇教程,先git clonehttps://github.com/E ...
- Spring、springmvc配置
首先把三个文件copy到resources目录下: 然后把这两个文件copy到WEB-INF下: 在datasource.properties中增加: db.driverLocation=C:\\Us ...
- js-template-art【二】语法
参看地址 一.模板语法 1.变量使用与输出 <% if (user) { %> <h2><%= user.name %></h2> <% } %& ...
- 什么是API测试
什么是API API是Application Programming Interface的简写. 实现了两个或多个独立系统或模块间的通信和数据交换能力. 什么是API测试 图片.png API测试是不 ...
- [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- linux_一些shell命令分析记录
一.用于shell脚本的界面命令交互 echo "请输入css-dist下载地址:" read addcss echo "开始下载css的zip包"( wget ...
- [OpenCV入门教程之十二】OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑
http://blog.csdn.net/poem_qianmo/article/details/25560901 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog ...
- [one day one question] webpack 打包报错 Cannot assign to read only property 'exports' of object '#<Object>'
问题描述: webpack 打包报错 Cannot assign to read only property 'exports' of object '#<Object>',这怎么破? 解 ...
- Github 上传代码的两种方式
上传本地代码/文件->Github 折腾了半天时间... Github前期准备部分 1)登录github,新建一个 repository 2)repository 命名 3)Github是一个托 ...
- 20145104张家明 《Java程序设计》第一周学习总结
20145104张家明 <Java程序设计>第1周学习总结 教材学习内容总结 在开学的第一周,通过了看书进行了系统的学习java,首先简单的了解java的发展历程,然后了解了JVM.JRE ...