广度优先搜索之2个岛屿之间的最短距离

感觉还是深度优先搜索啊,这里使用队列实现
注意事项,循环后的变量不要重复遍历。
思路:先找到第一个岛屿,然后用双循环遍历向第二个岛屿推进。这里用队列是方便消耗掉原先的数据,用栈就要用2个,一个是原先的栈,一个是新的栈。

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <deque>

using namespace std;

vector<int> direction{-1, 0, 1, 0, -1};

void dfs(std::vector<std::vector<int>> &grid, deque<pair<int, int>> &island, int m, int n)
{
    deque<pair<int, int>> temp;

    temp.push_back(pair<int, int>(m, n));

    while (1)
    {
        if (temp.empty() == true)
        {
            std::cout << "finish loop" << std::endl;
            std::cout << "island size " << island.size() << std::endl;
            break;
        }

        pair<int, int> item = temp.front();

        int r = item.first;
        int l = item.second;

        std::cout << "r = " << r << " l = " << l << std::endl;

        grid[r][l] = 2;
        temp.pop_front();

        //std::cout << "temp size " << temp.size() << std::endl;

        island.push_back(item);

        for (int i = 0; i < 4; i++)
        {
            int r_ = r + direction[i];
            int l_ = l + direction[i + 1];

            if (r_ >= 0 && r_ < grid.size() && l_ >= 0 && l_ < grid[0].size())
            {
                if (grid[r_][l_] == 1)
                {
                    temp.push_back(pair<int, int>(r_, l_));
                    grid[r_][l_] = 2;
                    //island.push_back(pair<int, int>(r_, l_));
                }
            }
        }

        //std::cout << "emd" << std::endl;
    }

    return;
}

int minDistance(std::vector<std::vector<int>> &grid)
{
    //find one of island

    deque<pair<int, int>> points;
    int m, n;
    bool is_found = false;

    for (int i = 0; i < grid.size(); i++)
    {

        if (is_found == true)
        {
            std::cout << "m = " << m << " n = " << n << std::endl;
            break;
        }

        for (int j = 0; j < grid[0].size(); j++)
        {
            if (grid[i][j] == 1)
            {
                m = i;
                n = j;
                is_found = true;
                break;
            }
        }
    }

    deque<pair<int, int>> island;
    dfs(grid, island, m, n);

    std::cout << "island size " << island.size() << std::endl;

    int min_distance = 0;

    while (!island.empty())
    {
        int p_count = island.size();
        std::cout << "p_count " << p_count << std::endl;
        min_distance++;

        while (p_count--)
        {
            pair<int, int> item = island.front();
            island.pop_front();

            int r = item.first;
            int l = item.second;

            //std::cout << "r " << r << " l " << l << std::endl;

            for (int i = 0; i < 4; i++)
            {
                int r_ = r + direction[i];
                int l_ = l + direction[i + 1];

                if (r_ >= 0 && r_ < grid.size() && l_ >= 0 && l_ < grid[0].size())
                {
                    if (grid[r_][l_] == 1)
                    {
                        return min_distance - 1;
                    }
                    else if (grid[r_][l_] == 2)
                    {
                        continue;
                    }
                    else
                    {
                        //std::cout << "r_ " << r_ << " l_ " << l_ << std::endl;
                        island.push_back(pair<int, int>(r_, l_));
                        grid[r_][l_] = 2;
                    }
                }
            }
        }
    }

    return min_distance - 1;
}

int main(int, char **)
{
    std::cout << "Hello, world!\n";

    std::vector<std::vector<int>> grid = {{1, 1, 1, 1, 1, 1, 1},
                                          {1, 0, 0, 0, 0, 0, 1},
                                          {1, 0, 0, 0, 0, 0, 1},
                                          {1, 0, 0, 1, 0, 1, 1},
                                          {1, 0, 0, 0, 0, 0, 1},
                                          {1, 0, 0, 0, 0, 0, 1},
                                          {1, 1, 1, 1, 1, 1, 1}};

    std::cout << "min distance " << minDistance(grid) << std::endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可峰科技

生活不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值