迷宫问题 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, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4) 这种找出路径并且输出,一般我们都只会用bfs,实际上dfs也是可以解决的
但是这样解题是有缺陷的,因为这个题目只有一条路,已经确定了,所以可以用dfs,若是求不止一条的最短路,就不行
dfs 方法可以学习一下
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std; int path[100][2],a[10][10],ans;
bool vis[10][10],flag=0;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0}; bool judge(int x,int y)
{
if(x>=0&&y>=0&&x<=4&&y<=4&&!flag&&!vis[x][y]&&!a[x][y]) return 1;
return 0;
} void dfs(int x,int y,int step)
{
path[step][0]=x;
path[step][1]=y;
if(x==4&&y==4)
{
flag=1;
ans=step;
return ;
}
for(int i=0;i<4;i++)
{
int tx=x+dx[i];
int ty=y+dy[i]; if(judge(tx,ty))
{
vis[tx][ty]=1;
dfs(tx,ty,step+1);
vis[tx][ty]=0;
}
}
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
dfs(0,0,1); for(int i=1;i<=ans;i++)
{
printf("(%d, %d)\n",path[i][0],path[i][1]);
}
return 0;
}
bfs 也挺好的,可以学习学习
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
int t[4][2]={0,1,1,0,-1,0,0,-1};
typedef pair<int,int> ee;
int vis[10][10];
int a[10][10];
int d[10][10];
ee b[10][10];
ee exa;
void bfs()
{
int sx=0,sy=0;
int tx,ty;
memset(vis,0,sizeof(vis));
queue<ee>q;
q.push(ee(sx,sy));
vis[sx][sy]=true;
d[sx][sy]=0; while(!q.empty())
{
exa=q.front();q.pop();
if(exa.first==4&&exa.second==4) break; for(int i=0;i<4;i++)
{
tx=exa.first+t[i][0];
ty=exa.second+t[i][1]; if(ty<0||tx<0||ty>5||tx>5) continue;
if(vis[tx][ty]) continue;
if(a[tx][ty]==1) continue; vis[tx][ty]=true;
d[tx][ty]=d[exa.first][exa.second]+1;
b[tx][ty]=exa;
q.push(ee(tx,ty));
}
}
return;
}
void print()
{
vector<ee> node;
while(1)
{
node.push_back(exa);
if(d[exa.first][exa.second]==0) break;
exa=b[exa.first][exa.second];
}
// node.push_back(ee(sx,sy)); for(int i=node.size()-1;i>=0;i--)
{
printf("(%d, %d)\n",node[i].first,node[i].second);
}
} int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
bfs();
print();
return 0;
}
迷宫问题 dfs bfs 搜索的更多相关文章
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- DFS+BFS(广度优先搜索弥补深度优先搜索遍历漏洞求合格条件总数)--09--DFS+BFS--蓝桥杯剪邮票
题目描述 如下图, 有12张连在一起的12生肖的邮票.现在你要从中剪下5张来,要求必须是连着的.(仅仅连接一个角不算相连) 比如,下面两张图中,粉红色所示部分就是合格的剪取. 请你计算,一共有多少 ...
- 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),每次可以选择一种颜色,使得和左上角相 ...
- 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)
从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...
随机推荐
- set get del
//设置 $ob = new Redis(); $ob->connect('127.0.0.1', 6379); $re = $ob->set('str1', serialize(['a' ...
- [转]Angular CLI 安装和使用
本文转自:https://www.jianshu.com/p/327d88284abb 一. 背景介绍: 两个概念: 关于Angular版本,Angular官方已经统一命名Angular 1.x统称为 ...
- [转]How to Improve Entity Framework Add Performance?
本文转自:http://entityframework.net/improve-ef-add-performance When you overuse the Add() method for mul ...
- PowerDesigner连接SqlServer数据库导出表结构
环境:PowerDesigner15 数据库sql server 2005 第一步.打开PowerDesigner ,建立一个物理数据模型,具体如下图: 第二步.新建成功之后,点击"Data ...
- MVC和WebForm区别
WebForm的理解 1. WebForm概念 ASP.NETWebform提供了一个类似于Winform的事件响应GUI模型(event-drivenGUI),隐藏了HTTP.HTML.JavaSc ...
- jquery只能输入数字
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【WebSocket No.1】实现服务端webSocket连接通讯
前言 现阶段socket通信使用TCP.UDP协议,其中TCP协议相对来说比较安全稳定!本文也是来讲解TCP为主(恕在下学艺不精). 下面是个人理解的tcp/ip进行通讯之间的三次握手! 1.客户端先 ...
- 【Mybatis】1、Mybatis拦截器学习资料汇总
MyBatis拦截器原理探究 http://www.cnblogs.com/fangjian0423/p/mybatis-interceptor.html [myBatis]Mybatis中的拦截器 ...
- 如何靠谱地查到Tomcat的版本
Tomcat版本获取 一般找jdk的版本的时候,我们直接执行如下命令就可以得知了 java -version 但是Tomcat的版本呢? 除了Tomcat安装目录路径里包含的版本号,还有其他靠谱的获取 ...
- 排错-LR安装No Background bmp defined in ...的解决办法
LR安装No Background bmp defined in section General entry BGBmp的解决办法 by:授客 QQ:1033553122 问题描述: 我在win7装L ...