您的位置:首页 > 财经 > 金融 > 图论第二天

图论第二天

2024/11/16 2:32:48 来源:https://blog.csdn.net/weixin_41902984/article/details/139337241  浏览:    关键词:图论第二天

最近加班时间又多了,随缘吧,干不动就辞呗。真是想歇几天了,题不能停!!今天目前只做了一道题,先用两种方式把他搞出来。

695. 岛屿的最大面积

class Solution {
public:int neighbor[4][2] = {1,0,0,-1,-1,0,0,1};int count = 0;int maxAreaOfIsland(vector<vector<int>>& grid) {int n = grid.size();int m = grid[0].size();int result = 0;vector<vector<bool>>visited(n,vector<bool>(m,false));for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(visited[i][j] == 0 && grid[i][j] == 1){count = 1;visited[i][j] = 1;dfs(grid,visited,i,j);result = max(result,count);} }}return result;}void dfs(vector<vector<int>>& grid,vector<vector<bool>>&visited,int x,int y){for(int i = 0;i < 4;i++){int nextx = x + neighbor[i][0];int nexty = y + neighbor[i][1];if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())continue;if(visited[nextx][nexty] == 0 && grid[nextx][nexty] == 1){count++;visited[nextx][nexty] = 1;dfs(grid,visited,nextx,nexty);}}}
};

BFS方法默写如下

class Solution {
public:int result = 0;int neighbor[4][2] = {1,0,0,-1,-1,0,0,1};queue<pair<int,int>>que;int maxAreaOfIsland(vector<vector<int>>& grid) {int count = 0;int n = grid.size(),m = grid[0].size();vector<vector<bool>>visited(n,vector<bool>(m,false));for(int i = 0;i < n;i++){for(int j = 0;j<m;j++){if(visited[i][j] == false && grid[i][j] == 1){count = 1;visited[i][j] = true;bfs(grid,visited,i,j,count);result = max(count,result);}}}return result; }void bfs(vector<vector<int>>& grid,vector<vector<bool>>&visited,int x,int y,int &count){que.push({x,y});while(!que.empty()){pair<int,int>cur = que.front();que.pop();for(int i = 0;i < 4;i++){int nextx = cur.first + neighbor[i][0];int nexty = cur.second + neighbor[i][1];if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())continue;if(visited[nextx][nexty] == false && grid[nextx][nexty] == 1){visited[nextx][nexty] = true;count++;que.push({nextx,nexty});}}}}
};

所以那个count可以这么用,&改的话,还是改了那个地址的值的,如果没有&的话,那他在函数里就不变啦。

1020. 飞地的数量

目前感觉dfs和bfs的用法有一个认识了,dfs由于需要做回溯,感觉更难一点,后面优先练dfs。这题的dfs有点难度。。。

思路很牛逼,需要先将周边(四周)是陆地的变成海洋,再重新算一下陆地的数量即可。

class Solution {
public:int count  = 0;int neighbor[4][2] = {1,0,0,-1,-1,0,0,1};int numEnclaves(vector<vector<int>>& grid) {int n = grid.size();int m = grid[0].size();for(int i = 0;i < n;i++){if(grid[i][0] == 1 )dfs(grid,i,0);if(grid[i][m-1] == 1)dfs(grid,i,m-1);}for(int j = 1;j<m-1;j++){if(grid[0][j] == 1 )dfs(grid,0,j);if(grid[n-1][j] == 1)dfs(grid,n-1,j);}count = 0;for(int i = 0;i < n;i++){for(int j = 0;j < m;j++){if(grid[i][j] == 1)dfs(grid,i,j);}}return count;}void dfs(vector<vector<int>>& grid,int x,int y){grid[x][y] = 0;count++;for(int i = 0;i < 4;i++){int nextx = x + neighbor[i][0];int nexty = y + neighbor[i][1];if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())continue;if( grid[nextx][nexty] == 1){dfs(grid,nextx,nexty);}}}
};

力扣炸了???绝,提测交给明天。。。然后再改改看

BFS看起来是一样的思路,我就不做第二遍啦(count++似乎容易漏while里外都要有count++,估计提交就能了解这个错误。不多逼逼)

好累啊今天,打把炉石睡觉!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com