代码实现class Solution { int dx[4] {0,0,-1,1}; int dy[4] {1,-1,0,0}; typedef pairint,int PII; public: int maxDistance(vectorvectorint grid) { //使用多源 BFS int n grid.size(),m grid[0].size(); vectorvectorint vis(n,vectorint(m,-
);//统计陆地到海洋的距离 queuePII que; for(int i 0; i n; i)//标记陆地的距离为0 { for(int j 0; j m; j) { if(grid[i][j]
{ vis[i][j] 0; que.push({i,j});//让陆地的坐标入队列 } } } while(que.size())//BFS { auto [x,y] que.front(); que.pop(); for(int i 0; i 4; i) { int a x dx[i]; int b y dy[i]; if(a 0 b 0 a n b m vis[a][b] -1 grid[a][b]