代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿

本文主要是介绍代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿

101孤岛的总面积

代码随想录

#include <iostream>
#include <vector>using namespace std;
int count = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void dfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 0;count++;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {dfs(graph, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {dfs(graph, i, 0);}if (graph[i][M - 1] == 1) {dfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {dfs(graph, 0, j);}if (graph[N - 1][j] == 1) {dfs(graph, N - 1, j);}}count = 0;for (int i = 1; i < N - 1; i++) {for (int j = 1; j < M - 1; j++) {if (graph[i][j] == 1) {dfs(graph, i, j);}}}cout << count;return 0;
}
#include <iostream>
#include <vector>
#include <queue>using namespace std;int count = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void bfs(vector<vector<int>> &graph, int row, int col) {count++;graph[row][col] = 0;queue<pair<int, int>> que;que.push({row, col});while (!que.empty()){pair<int, int> pa = que.front();que.pop();for (int i = 0; i < 4; i++) {int nextRow = pa.first + dir[i][0];int nextCol = pa.second + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {count++;graph[nextRow][nextCol] = 0;que.push({nextRow, nextCol});}}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {bfs(graph, i, 0);}if (graph[i][M - 1] == 1) {bfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {bfs(graph, 0, j);}if (graph[N - 1][j] == 1) {bfs(graph, N - 1, j);}}count = 0;for (int i = 1; i < N - 1; i++) {for (int j = 1; j < M - 1; j++) {if (graph[i][j] == 1) {bfs(graph, i, j);}}}cout << count;return 0;
}

102沉没孤岛

代码随想录

#include <iostream>
#include <vector>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void dfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 2;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {dfs(graph, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {dfs(graph, i, 0);}if (graph[i][M - 1] == 1) {dfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {dfs(graph, 0, j);}if (graph[N - 1][j] == 1) {dfs(graph, N - 1, j);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] != 0) {graph[i][j]--;}cout << graph[i][j] << " ";}cout << "\n";}return 0;
}
#include <iostream>
#include <vector>
#include <queue>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1 ,0}, {0, -1}};void bfs(vector<vector<int>> &graph, int row, int col) {graph[row][col] = 2;queue<pair<int, int>> que;que.push({row, col});while (!que.empty()) {int curRow = que.front().first;int curCol = que.front().second;que.pop();for (int i = 0; i < 4; i++) {int nextRow = curRow + dir[i][0];int nextCol = curCol + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1) {que.push({nextRow, nextCol});graph[nextRow][nextCol] = 2;}}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {if (graph[i][0] == 1) {bfs(graph, i, 0);}if (graph[i][M - 1] == 1) {bfs(graph, i, M - 1);}}for (int j = 0; j < M; j++) {if (graph[0][j] == 1) {bfs(graph, 0, j);}if (graph[N - 1][j] == 1) {bfs(graph, N - 1, j);}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] != 0) {graph[i][j]--;}cout << graph[i][j] << " ";}cout << "\n";}return 0;
}

103水流问题

代码随想录

#include <vector>
#include <iostream>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {visted[row][col] = true;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] >= graph[row][col] && !visted[nextRow][nextCol]) {dfs(graph, visted, nextRow, nextCol);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}vector<vector<bool>> firstBoard(N, vector<bool>(M, false));vector<vector<bool>> secondBoard(N, vector<bool>(M, false));for (int i = 0; i < N; i++) {dfs(graph, firstBoard, i, 0);dfs(graph, secondBoard, i, M - 1);}for (int j = 0; j < M; j++) {dfs(graph, firstBoard, 0, j);dfs(graph, secondBoard, N - 1, j);}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (firstBoard[i][j] && secondBoard[i][j]) {cout << i << " " << j << "\n";}}}return 0;
}

104.建造最大岛屿

代码随想录

#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int count;
void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col, int mark) {count++;visted[row][col] = true;graph[row][col] = mark;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}if (graph[nextRow][nextCol] == 1 && !visted[nextRow][nextCol]) {dfs(graph, visted, nextRow, nextCol, mark);}}
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}bool isAllLand = true;vector<vector<bool>> visted(N, vector<bool>(M, false));unordered_map<int, int> umap;int mark = 2;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 0) {isAllLand = false;}if (graph[i][j] == 1 && !visted[i][j]) {count = 0;dfs(graph, visted, i, j, mark);umap[mark] = count;mark++;}}}if (isAllLand) {cout << M * N;return 0;}int result = 0;unordered_set<int> vistedLand;for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 0) {count = 1;vistedLand.clear();for (int k = 0; k < 4; k++) {int nextRow = i + dir[k][0];int nextCol = j + dir[k][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {continue;}mark = graph[nextRow][nextCol];if (vistedLand.count(mark) != 0 || mark < 2) {continue;}count += umap[mark];vistedLand.insert(mark);}result = max(result, count);}}}cout << result;return 0;
}

这篇关于代码随想录day52 101孤岛的总面积 102沉没孤岛 103水流问题 104建造最大岛屿的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101202

相关文章

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

python实现pdf转word和excel的示例代码

《python实现pdf转word和excel的示例代码》本文主要介绍了python实现pdf转word和excel的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价... 目录一、引言二、python编程1,PDF转Word2,PDF转Excel三、前端页面效果展示总结一

在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码

《在MyBatis的XML映射文件中<trim>元素所有场景下的完整使用示例代码》在MyBatis的XML映射文件中,trim元素用于动态添加SQL语句的一部分,处理前缀、后缀及多余的逗号或连接符,示... 在MyBATis的XML映射文件中,<trim>元素用于动态地添加SQL语句的一部分,例如SET或W

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

vue解决子组件样式覆盖问题scoped deep

《vue解决子组件样式覆盖问题scopeddeep》文章主要介绍了在Vue项目中处理全局样式和局部样式的方法,包括使用scoped属性和深度选择器(/deep/)来覆盖子组件的样式,作者建议所有组件... 目录前言scoped分析deep分析使用总结所有组件必须加scoped父组件覆盖子组件使用deep前言