ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

矩阵中任意多个点到某一点最短距离

2021-10-09 19:01:03  阅读:218  来源: 互联网

标签:vector temp int max 矩阵 points grid 短距离 点到


输入

5 5 
0 0 0 0 0
8 8 8 0 0
0 0 8 0 0
8 8 8 8 0
0 0 0 0 0 

4 
1 1 
1 5 
5 1 
5 5 
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

void dfs(queue<pair<int, int>>& points, vector<vector<int>> &grid, int m, int n
        , int i, int j) {
    if (i < 0 || j < 0 || i == m || j == n || grid[i][j] == 2 || grid[i][j] == 8) {
        return;
    }
    if (grid[i][j] == 0) {
        points.push({i, j});
        return;
    }
    grid[i][j] = 2;
    dfs(points, grid, m, n, i - 1, j);
    dfs(points, grid, m, n, i + 1, j);
    dfs(points, grid, m, n, i, j - 1);
    dfs(points, grid, m, n, i, j + 1);
}


int shortestDist(vector<vector<int>> grid,int x1,int y1,int x2,int y2) {

    grid[x2][y2] = 1;

    int m = grid.size(), n = grid[0].size();
    queue<pair<int, int>> points;
    points.push({x1, y1});
    grid[x1][y1] = 2;

// bfs寻找grid[x2][y2],并把过程中经过的0赋值为2
    int x, y;
    int level = 0;
    while (!points.empty()){
        ++level;
        int n_points = points.size();
        while (n_points--) {
            auto [r, c] = points.front();
            points.pop();
            for (int k = 0; k < 4; ++k) {
                x = r + direction[k], y = c + direction[k+1];
                if (x >= 0 && y >= 0 && x < m && y < n) {
                    if (grid[x][y] == 2) {
                        continue;
                    }
                    if (grid[x][y] == 8) {          //障碍
                        continue;
                    }
                    if (grid[x][y] == 1) {          //找到另一端
                        return level;
                    }
                    points.push({x, y});
                    grid[x][y] = 2;
                }
            }
        }
    }
    return -1;          //不通
}

int judge(vector<vector<int>> data,vector<vector<int>> &input)
{
    int min = INT_MAX;
    for(int i=0;i<data.size();i++)
    {
        for(int j=0;j<data[0].size();j++)
        {
            int max = 0;
            for(int l = 0;l<input.size();l++)
            {
                if(data[i][j] == 8)
                {
                    max = INT_MAX;
                    break;
                }
                else
                {
                    if(i == input[l][0] && j == input[l][1])
                        continue;
                    else{
                        int temp = shortestDist(data,input[l][0],input[l][1],i,j);
                        if(temp == -1)
                        {
                            max = INT_MAX;
                            break;
                        }
                        if(max < temp)
                            max = temp;
                    }
                }
            }
            cout<<"data["<<i<<"]["<<j<<"] temp is "<<max<<endl;
            if(i == input[0][0] && j == input[0][1] && max == -1)
            {
                return -1;
            }
            if(min > max )
                min = max;

        }
    }
    return min;
}


int main()
{

    freopen("D:\\desktop\\Leecode_dbg\\data.txt","r",stdin);
    int m,n;
    cin>>m>>n;
    vector<vector<int>> dist(m,vector<int>(n));
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>dist[i][j];
        }
    }
    int k;
    cin>>k;
    vector<vector<int>> loc(k,vector<int>(2));

    for(int i=0;i<k;i++)
    {
        int temp;
        for(int j=0;j<2;j++)
        {
            cin>>temp;
            loc[i][j] = temp -1;
        }
    }

    cout<<judge(dist,loc)<<endl;
    return 0;
}




标签:vector,temp,int,max,矩阵,points,grid,短距离,点到
来源: https://blog.csdn.net/pop541111/article/details/120677265

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有