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

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Java死锁问题解决方案及示例详解

《Java死锁问题解决方案及示例详解》死锁是指两个或多个线程因争夺资源而相互等待,导致所有线程都无法继续执行的一种状态,本文给大家详细介绍了Java死锁问题解决方案详解及实践样例,需要的朋友可以参考下... 目录1、简述死锁的四个必要条件:2、死锁示例代码3、如何检测死锁?3.1 使用 jstack3.2

解决JSONField、JsonProperty不生效的问题

《解决JSONField、JsonProperty不生效的问题》:本文主要介绍解决JSONField、JsonProperty不生效的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录jsONField、JsonProperty不生效javascript问题排查总结JSONField

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos