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 ...
随机推荐
- Qt::QWidget 无默认标题栏边框的拖拽修改大小方式
开发环境:win10+vs2015+qt5.9.1 背景:开发过程中,一般很少会使用系统提供的标题栏和边框:往往都是自定义一个自己设计的方案.这时候在QWidget中需要加上flag:Qt::Fram ...
- 【查看】mysql 常规书写注意事项(那些坑)
mysql 常规书写注意事项,mysql注意事项 1. 注释: -- 后面一定要加一个空格,否则会报错 2.注释:/*! content */ 在mysql中是会执行的,但是其他数据库不会. 例 ...
- 使用Atom预览markdown
1.打开任意.md文件(markdown源文件)2.windows : ctrl + shift + pmac : command + shift + p这条命令跟Sublime Text是一样的,打 ...
- ajax请求,html调用js
1:html中调用js中的函数,js使用ajax请求向后台请求,返回数据. <!DOCTYPE html> <html lang="en"> <hea ...
- 前端页面汉子显示为问号,需修改 linux下面修改mysql 数据库的字符编码为utf8
设置MySQL数据库编码为UTF-8 登陆后查看数据库当前编码:SHOW VARIABLES LIKE 'char%'; 修改/etc/mysql/my.cnf (默认安装路径下) (标签下没有的添加 ...
- PAT 1055 The World's Richest[排序][如何不超时]
1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...
- 【4】Python对象
本章主题 Python对象 内建类型 标准类型操作符 值的比较 对象身份比较 布尔类型 标准类型内建函数 标准类型总览 各种类型 不支持的类型 Python对象 Python使用 ...
- ng-深度学习-课程笔记-1: 介绍深度学习(Week1)
1 什么是神经网络( What is a neural network ) 深度学习一般是指非常非常大的神经网络,那什么是神经网络呢? 以房子价格预测为例,现在你有6个房子(样本数量),你知道房子的大 ...
- Python ConfigParser的使用
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- 随机生成气泡碰撞(原生js)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>随 ...