求从图中的任意一点(起点)到另一点(终点)的最短路径,最短距离;

图中有数字的点表示为图中的不同海拔的高地,不能通过;没有数字的点表示海拔为0,为平地可以通过;

这个是典型的求图中两点的最短路径;本例,用深度优先算法来实现;

在每一个点都有四个方向(有的点的有些方向不能通过),所以在每一个点处要处理四种方向的情况;

深度优先算法函数怎么写?

也就是写递归函数。。。但是递归函数肿么写???

第一:判断初始态,从起点出发,刚开始步数为0;dfs(start_x, start_y, 0);

第二:从起点出发,要做什么事情?尝试四个方向的走法。

在每个方向上要什么?先判断这个点是否到达边界,再判断这个点是否走过并且是否是高地;如果不是高地也没有走过,则再从该点出发,做和之前的点一样的事情;

dfs(tx, ty, step+1);

第二:判断终止条件,到达终点,判断当前步数;返回;

/*************************************************************************
> File Name: search_min_step.cpp
> Author:
> Mail:
> Created Time: 2015年11月13日 星期五 21时49分41秒
************************************************************************/ #include<iostream> using namespace std; const int MAX_X = 50;
const int MAX_Y = 50;
int min_step = 10000;
int my_x, my_y;
int map[MAX_X][MAX_Y];
int book[MAX_X][MAX_Y];
int start_x, start_y;
int dest_x, dest_y; void dfs(int x, int y, int step)
{
/*up, right, down, left*/
int next[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
int tx, ty; if (x == dest_x && y == dest_y){
if (step < min_step)
min_step = step;
return;
} for (int i = 0; i < 4; i++){
tx = x + next[i][0];
ty = y + next[i][1];
if (tx > my_x || ty > my_y || tx < 0 || ty < 0)
continue; if (map[tx][ty] == 0 && book[tx][ty] == 0){
book[tx][ty] = 1;
dfs(tx, ty, step+1);
book[tx][ty] = 0;
}
} }
void input_map_info()
{
cout << "input the max x:";
cin >> my_x;
cout << "input the max y:";
cin >> my_y; cout << "input the map information:\n";
for (int i = 1; i <= my_x; i++){
for (int j = 1; j <= my_y; j++){
cin >> map[i][j];
}
}
} int main()
{
input_map_info(); cout << "input the source location:";
cin >> start_x >> start_y;
cout << "input the destat location:";
cin >> dest_x >> dest_y; book[start_x][start_y] = 1;
dfs(start_x, start_y, 0); cout << "min_step = " << min_step << endl; return 0;
}

在图中寻找最短路径-----深度优先算法C++实现的更多相关文章

  1. 图结构练习——最短路径(dijkstra算法(迪杰斯拉特))

      图结构练习——最短路径 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个带权无向图,求节点1到节点n的最短路径.   ...

  2. Expm 4_2 有向无环图中的最短路径问题

    [问题描述] 建立一个从源点S到终点E的有向无环图,设计一个动态规划算法求出从S到E的最短路径值,并输出相应的最短路径. 解: package org.xiu68.exp.exp4; import j ...

  3. Expm 4_1 多段图中的最短路径问题

      [问题描述] 建立一个从源点S到终点T的多段图,设计一个动态规划算法求出从S到T的最短路径值,并输出相应的最短路径. 解 package org.xiu68.exp.exp4; public cl ...

  4. SDUT OJ 图结构练习——最短路径 ( Floyed 算法 AND Dijkstra算法)

    图结构练习——最短路径 Time Limit: 1000 ms            Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  5. 同步图计算实现最短路径Dijkstra算法

    同上篇讲述pageRank一样,考虑一个顶点V. 根据顶点算法通常步骤1) 接收上个超步发出的入邻居的消息2) 计算当前顶点的值3) 向出邻居发消息 1.接收入邻居的消息 2.求入邻居的最小值,加上顶 ...

  6. 寻找最短路径Dijkstra算法

    1 /** 2 * 1.对于T中的每个顶点u,找到u的具有最小权重的连接边.所有到u的连接边都存储在queues.get(u)中.queues.get(u).peek()返回拥有最小权值 3 * 的连 ...

  7. 网络最短路径Dijkstra算法

    最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...

  8. 最短路径-Dijkstra算法与Floyd算法

    一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1    ADE:2   ADCE:3   ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...

  9. 图中最短路径算法(Dijkstra算法)(转)

    1.Dijkstra 1)      适用条件&范围: a)   单源最短路径(从源点s到其它所有顶点v); b)   有向图&无向图(无向图可以看作(u,v),(v,u)同属于边集E ...

随机推荐

  1. 关于npm audit fix

    https://blog.csdn.net/weixin_40817115/article/details/81007774 npm audit : npm@5.10.0 & npm@6,允许 ...

  2. mouseenter([[data],fn])

    mouseenter([[data],fn]) 概述 当鼠标指针穿过元素时,会发生 mouseenter 事件.该事件大多数时候会与mouseleave 事件一起使用.广州大理石机械构件 与 mous ...

  3. 012_使用死循环实时显示 eth0 网卡发送的数据包流量

    #!/bin/bash while : do echo '本地网卡 eth0 流量信息如下: ' #grep输出所找整行,awk直接输出第5列 ifconfig eth0 | grep "R ...

  4. Linq to AD

    輕鬆找出域中所有用戶,組,而且還可以更改 LINQ to Active Directory https://linqtoad.codeplex.com/ LINQ to LDAP http://lin ...

  5. web超大文件上传

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  6. python 赋值魔法

    序列解包: >>> x,y,z = 1, 2, 3>>> print(x, y, z)1 2 3 >>> a,b, *reset = [1,2,3 ...

  7. LibreOJ #113. 最大异或和

    二次联通门 : LibreOJ #113. 最大异或和 /* LibreOJ #113. 最大异或和 线性基 插入 与 查询最大值 说一下我在学习线性基时遇到的一些问题 1.线性基指的是一个数集 2. ...

  8. HTML学习日记 入门教程 知识点 ing

    初学html,如有错误,欢迎指正谢谢. 这只是一些基础的知识点,是学习后自己想到总结的,不适合资深者. 1.href是Hypertext Reference的缩写.意思是指定超链接目标的URL.是cs ...

  9. 报错 One or more constraints have not been satisfied.

    常出现在导入已有标签时. 需要在<build/><plugins/>里面追加标签 <plugin> <groupId>org.apache.maven. ...

  10. laotech老师唠科mac 深入浅出MAC OS X ceshi ruguokeyi

    laotech老师唠科mac 深入浅出MAC OS X http://study.163.com/plan/planLearn.htm?id=1637004#/learn/resVideo?lesso ...