首页 技术 正文
技术 2022年11月15日
0 收藏 850 点赞 3,646 浏览 928 个字

DFS

关于dfs,我的理解就是深度搜索,找到所有与入口相连的路径,可以用于迷宫求出口,利用递归的思想,进行搜索返回所有值。
比如,给你两个值分别表示迷宫的长和宽,迷宫有一个入口,一个出口,判断能否从迷宫出来,入口用”s“表示,出口用“e“表示,墙壁用”#“表示,路用”.“表示
            输入:3  4
s...
.##.
#..e
4 4
s...
..##
#.##
###e
输出:You can gou out!
Trapped!
#include "pch.h"
#include <cstdio>
#include <cstring>
#include <queue>
#include<cstdio>
#include <algorithm>
#include <iostream>
#include <istream>using namespace std;
int n, m, si, sj, ei, ej;
char ditu[30][30];
int flag[30][30];
int s = 0;
void sou(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m || flag[x][y] == 1)
return;
if (x == ei && y == ej)
s = 1;
//把使用的地方标记为1,防止下次再用
flag[x][y] = 1;
//四个方向
sou(x + 1, y);
sou(x - 1, y);
sou(x, y + 1);
sou(x, y - 1);
}
int main()
{
int i, j;
while (cin >> n >> m)
{
getchar();
s = 0;
memset(flag, 0, sizeof(flag));//还原flag,flag用来标记墙壁并且标记使用过的地方
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
cin >> ditu[i][j];//输入地图
if (ditu[i][j] == 's')
{
si = i;
sj = j;
}
if (ditu[i][j] == 'e')
{
ei = i;
ej = j;
}
if (ditu[i][j] == '#')
flag[i][j] = 1;
}
getchar();
}
sou(si, sj);//查找
if (s == 0)
cout << "Trapped!" << endl;
else
cout << "You can go out!" << endl;
}
return 0;
}
上一篇: JVM内存简析
下一篇: Android 蓝牙学习
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726