/********************************
啊哈!算法
深度优先搜索算法
迷宫问题
输入:
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)的更多相关文章

  1. 迷宫问题 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, ...

  2. 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)

    [题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...

  3. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

  4. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  5. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  6. [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 ...

  7. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  8. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  9. DFS/BFS视频讲解

    视频链接:https://www.bilibili.com/video/av12019553?share_medium=android&share_source=qq&bbid=XZ7 ...

  10. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

随机推荐

  1. Codeforces Round #460 (Div. 2)-A. Supermarket

    A. Supermarket time limit per test2 seconds memory limit per test256 megabytes Problem Description W ...

  2. 笔记-网络-抓包-wireshark

    笔记-网络-抓包-wireshark 1.      开始 环境:win8笔记本,无线网 1.1.    无线网卡设置 因为需抓捕无线网卡上的数据包,需要进行一项设置,如捕获有线网卡,无需设置. 打开 ...

  3. 3 View - 错误视图函数

    1.定义视图 本质就是一个函数 视图的参数 一个HttpRequest实例 通过正则表达式组获取的位置参数 通过正则表达式组获得的关键字参数 在应用目录下默认有views.py文件,一般视图都定义在这 ...

  4. LOJ #6008. 「网络流 24 题」餐巾计划

    #6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_ir​i​​ 块餐巾.餐厅可以购买新的餐巾,每块餐 ...

  5. TCP/IP网络编程之I/O复用

    基于I/O复用的服务端 在前面章节的学习中,我们看到了当有新的客户端请求时,服务端进程会创建一个子进程,用于处理和客户端的连接和处理客户端的请求.这是一种并发处理客户端请求的方案,但并不是一个很好的方 ...

  6. RNNs在股票价格预测的应用

    RNNs在股票价格预测的应用 前言 RNN和LSTMs在时态数据上表现特别好,这就是为什么他们在语音识别上是有效的.我们通过前25天的开高收低价格,去预测下一时刻的前收盘价.每个时间序列通过一个高斯分 ...

  7. linux下编译运行TIGL Viewer步骤

    linux下编译运行TIGL Viewer步骤(仅为了正确编译安装的话直接跳到步骤3) 1. linux发行版选择:由于linux发行版众多,不同版本包含的库版本可能存在差别,因此需要选择正确的版本. ...

  8. IntelliJ IDEA 视频教程

    相关视频教程: Intellij IDEA视频教程 最新版Intellij IDEA视频教程

  9. Shell脚本直接执行sql语句和不显示列名

    在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...

  10. rn-fetch-blob+redux 取消请求

    其实取消请求对于普通的ajax请求rn-fetch-blob写法是比较简单的 let task = RNFetchBlob.fetch('GET', 'http://example.com/file/ ...