代码随想录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

相关文章

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F