• 岛屿问题求最短路径(DFS)


    用1代表陆地,用0代表陆地,你可以上下左右移动,给出下面这张地图求出最短路径

    01011
    00001
    10100
    10101
    10000

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int arr[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
    int start_x, start_y, end_x, end_y;
    int MIN = 99999, step = 0;
    int map[5][5] = {
    0, 1, 0, 1, 1,
    0, 0, 0, 0, 1,
    1, 0, 1, 0, 0,
    1, 0, 1, 0, 1,
    1, 0, 0, 0, 0
    };
    
    void DFS(int x, int y, int step) {
        if (x == end_x && y == end_y) {//函数结束条件:到达目标点
            MIN = min(MIN, step);
            return;
        }
        for (int i = 0; i < 4; ++i) {
            int nx = arr[i][0] + x;
            int ny = arr[i][1] + y;//移动位置
            if (nx >= 5 || nx < 0 || ny >= 5 || ny < 0 || map[nx][ny] == 1) {//如果移动到边界外或者碰到墙,则重新选择方向走
                continue;
            }
            map[x][y] = 1;//标记刚刚呆的地方为1,不需要回去了
            DFS(nx, ny, step + 1);//以新的地方再次搜索
            map[nx][ny] = 0;//当回退时,说明找到了"终点",将改点设置可行,返回上个节点,找寻从上一个点有没有其他的路到终点
        }
    }
    
    int main()
    {
        cin >> start_x >> start_y >> end_x >> end_y;//输入起点和终点
        DFS(start_x, start_y, 0);
        cout << MIN;
        system("PAUSE");
        return 0;
    }

    类似的下面这种也仅仅是上面的符号变换。

    #S######.#
    ......#..#
    .#.##.##.#
    .#........
    ##.##.####
    ....#....#
    .#######.#
    ....#.....
    .####.###.
    ....#...G#

    “#”代表海洋,“.”代表陆地,S代表起点,G代表终点。
    这道的起点和终点坐标需要我们去查找,只要在输入时检测是否为起点或者终点,记录下坐标即可。

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    #define MAX_SIZE 50
    
    using namespace std;
    
    vector<vector<char> > map(MAX_SIZE, vector<char>(MAX_SIZE, '.'));
    int arr[4][2]{ 1, 0, -1, 0, 0, 1, 0, -1 };
    int MIN = 99999, begin_x, end_x, begin_y, end_y, a, b;
    
    void DFS(int x, int y, int step) {
        if (x == end_x && y == end_y) {
            MIN = min(MIN, step);
            return;
        }
    
        for (int i = 0; i < 4; i++) {
            int nx = x + arr[i][0];
            int ny = y + arr[i][1];
    
            if (nx < 0 || nx >= a || ny < 0 || ny >= b || map[nx][ny] == '#') {
                continue;
            }
            map[nx][ny] = '#';
            DFS(nx, ny, step + 1);
            map[nx][ny] = '.';
        }
    }
    
    int main()
    {
        int m, n;
        cin >> m >> n;
        a = m, b = n;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                cin >> map[i][j];
                if (map[i][j] == 'S') {
                    begin_x = i, begin_y = j;
                }
                if (map[i][j] == 'G') {
                    end_x = i, end_y = j;
                }
            }
        }
    
        DFS(begin_x, begin_y, 0);
    
        cout << MIN;
    
        return 0;
    }

    上面的这两种方式,利用递归将所有到达目的地的路线都找了出来,取最小值。

  • 相关阅读:
    [BZOJ 1698] 荷叶池塘
    [BZOJ 3132] 上帝造题的七分钟
    [JLOI2011] 飞行路线
    [Codeforces Round49F] Session in BSU
    [BZOJ 3036] 绿豆蛙的归宿
    CRC-16校验原理
    ubuntu下mysql的安装与配置
    【OpenCV】边缘检测:Sobel、拉普拉斯算子
    我对sobel算子的理解
    梯度算子(普通的+Robert + sobel + Laplace)
  • 原文地址:https://www.cnblogs.com/Mayfly-nymph/p/10629390.html
Copyright © 2020-2023  润新知