迷宫问题(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 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
随机推荐
- HUD:3746-Cyclic Nacklace(补齐循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- [BZOJ1503]郁闷的出纳员(Splay)
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...
- 实验一 查看CPU和内存,用机器指令和汇编指令编程
(1):使用debug,将下面的程序段写入内存,逐条执行,观察每条指令执行后,CPU中相关寄存器中内存的变化. 机器码 汇编指令 b8 20 4e mov ax,4E20H 05 ...
- python矩阵和向量的转置问题
numpy有很多方法进行转置,这里由于时间和精力限制(主要是我实在比较懒,有一个基本上一直能使的,就懒得看其他的了),其他方法我没研究,这里我总结的东西,如果有问题,欢迎各路大佬拍砖 一.创建矩阵: ...
- [转]Docker容器内不能联网的6种解决方案
注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...
- Unity 查找
GameObject.Find().Transform.Find查找游戏对象 1.前置条件 Unity中常用到查找对象,非隐藏的.隐藏的,各种方法性能有高有低,使用又有各种条件限制. 在此对查找的性能 ...
- TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting Time Limit: 2.0 Seconds Memory Limit: 150000K Total Runs: 152 Accepted Ru ...
- Linux下调试段错误 (gdb,core,ulimit)
Linux环境下经常遇到某个进程挂掉而找不到原因,我们可以通过生成core file文件加上gdb来定位. (1)首先 在makefile中要增加编译调试选项 -g,才可以利用下面的gdb来调试 gc ...
- Xposed初体验
Xposed初体验 1 测试环境 硬件:小米2s 16GB 电信版 系统:MIUI 4.4.18(开发版) Xposed版本: 2.5 注:Xposed版本号必须大于2.3,MIUI系统版本号也必须大 ...
- Linux调用fork()编程
本文出自:svitter's blog #include <iostream> #include <cstdio> #include <unistd.h> usin ...