AcWing 173.矩阵距离

首先就是上一个时间超时的做法:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include <iomanip>
#include<sstream>
#include<numeric>
#include<map>
#include<limits.h>
#include<set>
#define int long long
#define MAX 1050
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef pair<int, int> PII;
int n, m;
int counts=INT_MAX;
char maps[MAX][MAX];
int dist[MAX][MAX];
int brr[MAX][MAX];
int a, b;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
queue<PII>q;
vector<int>nextVisit;
void bfs(int x, int y) {
    memset(dist, -1, sizeof dist);
    q.push({ x,y });
    dist[x][y] = 0;
    while (!q.empty()) {
        auto  t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
            int a = t.first + dx[i];
            int b = t.second + dy[i];
            if (dist[a][b] >= 0)
                continue;
            if (a<1 || a>n || b<1 || b>m)
                continue;

            q.push({ a,b });
            dist[a][b] = abs(x - a) + abs(y - b);
       }
    }
    if (maps[x][y] == '0') {
        _for(i, 1, n + 1) {
            _for(j, 1, m + 1) {
                    if (maps[i][j] == '1') {
                        counts = min(counts, dist[i][j]);
                    }
            }
        }
        brr[x][y] = counts;
    }
    else
        brr[x][y] = 0;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m;
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1)
            cin >> maps[i][j];
    }
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1) {
            bfs(i, j);
            counts = INT_MAX;
        }
    }
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1) {
            cout << brr[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

具体原因不明,但是对于1000左右这里的数是不适用的,其他情况下都是可以的。

后来看了题解之后试着优化了一下。

其实和上面的思路是一样的,就是对于每一个点都进行遍历BFS,算出到各自点的距离,之后,再统一处理b矩阵,直接把距离最短的放里面。

但这样处理好像会很费劲,对于多个数组进行赋值和交换和变值,会超出时间限制也是正常的。

这里可以换一种思路进行优化:

说到底,如果对于某一点来说,这一点字符是'1',距离就直接是0了,不用担心。

如果是这一点字符是'0',那么我们需要计算这一点到'1'的最短距离,其实换过来说,也就是所有字符是'1'到达这一点的距离的最小值,也就是多源BFS的问题,所有'1'字符同时扩散,看看各自的点距离。

上代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include <iomanip>
#include<sstream>
#include<numeric>
#include<map>
#include<limits.h>
#include<set>
#define int long long
#define MAX 1050
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef pair<int, int> PII;
int n, m;
int counts=INT_MAX;
char maps[MAX][MAX];
int dist[MAX][MAX];
int brr[MAX][MAX];
int a, b;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
queue<PII>q;
vector<int>nextVisit;
void bfs() {
    memset(dist, -1, sizeof dist);
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1) {
            if (maps[i][j] == '1')
            {
                q.push({ i,j });
                dist[i][j] = 0;
            }
        }
    }
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        _for(i, 0, 4) {
            int a = dx[i] + t.first;
            int b = dy[i] + t.second;
            if (a<1 || a>n || b<1 || b>m)
                continue;
            if (dist[a][b] >= 0)
                continue;
            
            q.push({ a,b });
            dist[a][b] = dist[t.first][t.second] + 1;
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> n >> m;
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1)
            cin >> maps[i][j];
    }
    bfs();
    _for(i, 1, n + 1) {
        _for(j, 1, m + 1) {
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
相关推荐
CV-King10 分钟前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
代码雕刻家42 分钟前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
雨中rain42 分钟前
算法 | 位运算(哈希思想)
算法
小飞猪Jay2 小时前
C++面试速通宝典——13
jvm·c++·面试
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
sp_fyf_20243 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
rjszcb3 小时前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想4 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财4 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
我是哈哈hh4 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝