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;
}
相关推荐
用户979984807184 小时前
200 行代码写一个能跑的 AI Agent:不依赖任何框架,只靠 tool-calling 原理
算法
threerocks4 小时前
【Muse实战】X 流量作战室搭建保姆级教程 - 拥有你自己的运营团队
算法
GreenTea4 小时前
深度拆解 ScienceBuddy:如何用“双层递归自进化”构建高可靠科研 Agent Harness
前端·后端·算法
花椒技术5 小时前
2.46 秒生成 5 秒视频:拆解 H3 Max 的模型、推理栈与硬件协同
算法·音视频开发·视频编码
vivo互联网技术5 小时前
ART:妆容迁移框架,重新定义高保真妆容迁移 | ECCV 2026
人工智能·算法·图像识别
用户0441440924495 小时前
卫星信号模拟器里的四个坐标转换公式
算法
橘和柠5 小时前
阿里 open-code-review (AI代码审查工具)实战:安装、四层规则链、自定义规则格式与实测避坑
算法·面试
得物技术5 小时前
别再只卷向量检索了,得物交易搜索如何用“生成式”实现召回范式跃迁?
人工智能·算法·llm
罗西的思考5 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(4)--- Rollout实现细节
人工智能·算法
罗西的思考5 小时前
机器人 / 物理 Agent Harness 综合分析与对比:从「更强的模型」到「更好的系统」
人工智能·算法·机器学习