迷宫问题(DFS,BFS)
/********************************
啊哈!算法
深度优先搜索算法
迷宫问题
输入:
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
1 1 4 3 输出:7
*********************************/
#include<iostream>
#include<ctime> using namespace std;
int count=;
int endx,endy,m,n;
void dfs(int **pos,bool **book,int sx,int sy,int step,int &min)
{
if(sx==endx && sy==endy)
{
if(step<=min)
min=step;
cout<<"第"<<count++<<"方案:"<<step<<endl;
return;//出口
}
int move[][]={
{,},
{,-},
{,},
{-,}
}; int tx,ty;
for(int i=;i<;i++)
{
tx=sx+move[i][];
ty=sy+move[i][];
if(tx>m || tx< || ty>n || ty<)
continue;
if(pos[tx][ty]== && book[tx][ty]==)
{
book[tx][ty]=true;
dfs(pos,book,tx,ty,step+,min);
book[tx][ty]=;//**尝试结束,取消这个点的标记
}
}
} int main()
{
int i,j;
cout<<"输入迷宫的行列:";
cin>>m>>n;
int **pos=new int *[m+];//迷宫。指针数组
bool **book=new bool*[m+];//记录是否走过
for(i=;i<m+;i++)
{
pos[i]=new int[n+];
book[i]=new bool[n+];
} cout<<"输入迷宫(1表示障碍物,0表示通道,空格隔开):"<<endl;
for(i=;i<m+;i++)
{
for(j=;j<n+;j++)
{ cin>>pos[i][j];
while(!cin.good())
{
cin.clear();
while(cin.get()!='\n')
continue;
cout<<"在第"<<i+<<"行"<<"第"<<j+<<"列"<<"输入错误"<<endl;
cout<<"重新输入:"<<endl;
cin>>pos[i][j];
}
}
} cout<<endl<<"迷宫:"<<endl;
for(i=;i<m+;i++)
{
for(j=;j<n+;j++)
cout<<pos[i][j]<<" ";
cout<<endl;
} for(i=;i<m+;i++)
for(j=;j<n+;j++)
book[i][j]=; int startx,starty;
cout<<"输入起始点: ";
cin>>startx>>starty;
book[startx][starty]=true; cout<<"输入终点: ";
cin>>endx>>endy; int step=,min=; dfs(pos,book,startx,starty,step,min);
if (min<)
cout<<"最短步数是: "<<min<<endl;
else cout<<"不存在路径"<<endl; for(i=;i<m+;i++)
{
delete [] pos[i];
delete [] book[i];
}
delete [] pos;
delete [] book;
return ;
}
/**********************
BFS
*************/
#include<iostream>
using namespace std; struct node
{
int x;
int y;
int f;//记录路径
int s;//记录步长
}; struct stack
{
int st[];
int top;
};
int main()
{
node queue[]; bool book[][]={false};
int m,n,sx,sy,ex,ey;
cout<<"row and column:";
cin>>m>>n;
int i,j;
int **pos=new int*[m+];
for(i=;i<m+;i++)
pos[i]=new int[n+]; cout<<"screat map:"<<endl;
for(i=;i<m+;i++)
for(j=;j<n+;j++)
cin>>pos[i][j];
cout<<"start and end:";
cin>>sx>>sy>>ex>>ey;
book[sx][sy]=;
int head=, tail=;
queue[head].x=sx; //定义后初始化只能以这种方式,出发点
queue[head].y=sy;
queue[head].f=head;
queue[head].s=;
tail++;//tail超前队列最后一个元素一位 int move[][]={
{,},{-,},{,-},{,}
}; int goal=;
while(head!=tail)
{
if( queue[head].x==ex && queue[head].y==ey)
{
goal=head;
cout<<"最短路径:"<<queue[head].s<<endl;
head++;
break; //广度优先搜索最先找到的就是最短的
}
for(i=;i<;i++)
{
node t={queue[head].x+move[i][],queue[head].y+move[i][],head,queue[head].s+};
//遍历四个方向如果合法且没被标记则入队
if(t.x>m || t.x< || t.y>n || t.y<)
continue;
if(pos[t.x][t.y]== && book[t.x][t.y]==false)
{
queue[tail]=t;//结构体可整体复制
tail++;
book[t.x][t.y]=true;//注意走过的路要标记
}
}
head++; }
//打印路径
cout<<"队列中的位置是:"<<endl;
i=goal;cout<<goal<<endl;
stack re={{},};
while(queue[i].s>=)//直到回溯到起始点
{
re.st[re.top++]=i;//反着从终点到起点入栈
i=queue[i].f;//这里错了,不要i--,i记录上一个位置的前任(在队列中的位置)
if(i==)//起始点的前任是它自己,要标记退出,不然死循环
break; }
while(re.top>=)
{
cout<<queue[re.st[re.top]].x<<" "<<queue[re.st[re.top]].y;//先进后出,出栈,正着打印
if(re.top!=)
cout<<"->";
re.top--;
}
cout<<endl; for(i=;i<m+;i++)
delete [] pos[i];
delete [] pos;
return ;
}
迷宫问题(DFS,BFS)的更多相关文章
- 迷宫问题 dfs 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, 0, 1, ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- DFS/BFS视频讲解
视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
随机推荐
- 【转】Python操作MongoDB
Python 操作 MongoDB 请给作者点赞--> 原文链接 这篇文章主要介绍了使用Python脚本操作MongoDB的教程,MongoDB作为非关系型数据库得到了很大的宣传力度,而市面 ...
- 初识Java之入门学习(扫盲)
一,开发环境的配置 1. jdk1.8的安装 2. 环境变量的配置 3.MyEclipse8.5的安装 jdk是什么: JDK 是Java开发工具包 (Java Development Kit ) 的 ...
- MySQL之架构与历史(二)
多版本并发控制 MySQL的大多数事务型存储引擎实现的都不是简单的行级锁.基于提升并发性能的考虑,它们一般都同时实现了多版本并发控制(MVCC).不仅是MySQL,包括Oracle.PostgreSQ ...
- 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体
一.CSS如何设置字体的类型.大小.颜色 设计网页时,一般设置body的字体,让其他标签继承body的字体,这样设置特别方便,但是标题标签h1到h6和表单标签(input类型)是没有继承body的字体 ...
- c语言入门-02-第一个c程序开始
我们来开我们第一个c代码 #include<stdio.h> int main(){ // print num int num; num = 1; printf("%d\n&qu ...
- 在windows64位上安装Python3.0
1.下载安装包 下载地址:https://www.python.org/downloads/ 如果要下载帮助文件:Download Windows help file 如果要下载基于网页的安装程序: ...
- 基于单层决策树的AdaBoost算法源码
基于单层决策树的AdaBoost算法源码 Mian.py # -*- coding: utf-8 -*- # coding: UTF-8 import numpy as np from AdaBoos ...
- Leetcode 525.连续数组
连续数组 给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组. 示例 1: 输入: [0,1] 输出: 2 说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2: ...
- Spring框架annotation实现IOC介绍
Spring学习笔记(三) 续Spring 学习笔记(二)之后,对Spring框架的annotation实现方法进行整理 本文目录 @Autowire 1 @Autowire+@Qualifier t ...
- MAC抓包工具charles(青花瓷)
下载链接:http://pan.baidu.com/s/1pL6ClBX 配置教程:http://blog.csdn.net/jiangwei0910410003/article/details/41 ...