当前位置: 首页 > news >正文

算法训练营day66-孤岛总面积-沉没孤岛-水流问题-建造最大岛屿

题目1:101. 孤岛的总面积 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};
int count = 0;
void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, int x, int y, bool flag) {queue<pair<int, int>> qu;qu.push(pair<int, int>(x, y));vistied[x][y] = true;if(flag) map[x][y] = 0;else count++;while(!qu.empty()) {pair<int, int> cur = qu.front();qu.pop();int newx = cur.first;int newy = cur.second;for(int i = 0;i < 4;i++) {int nextx = newx + dir[i][0];int nexty = newy + dir[i][1];if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;if(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {vistied[nextx][nexty] = true;qu.push(pair<int, int>(nextx, nexty));if(flag) map[nextx][nexty] = 0;else count++;}}}
}int main() {int n,m;cin >> n >> m;vector<vector<int>> map(n, vector<int>(m));for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {cin >> map[i][j]; }}vector<vector<bool>> vistied(n, vector<bool>(m, false));for(int j = 0;j < m;j++) {if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, 0, j, true);if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, n - 1, j, true);}for(int i = 0;i < n;i++) {if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, i, 0, true);if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, i, m - 1, true);}for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {if(!vistied[i][j] && map[i][j] == 1) {// if(i != 0 && i != n - 1 && j != 0 && j != m - 1) {//     result++;// }bfs(map,vistied,i,j, false);}}}cout << count << endl;return 0;
}

题目2:102. 沉没孤岛 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, vector<vector<int>>& result, int x, int y, bool flag) {queue<pair<int, int>> qu;qu.push(pair<int, int>(x, y));vistied[x][y] = true;if(flag) map[x][y] = 0;else result[x][y] = 0;while(!qu.empty()) {pair<int, int> cur = qu.front();qu.pop();int newx = cur.first;int newy = cur.second;for(int i = 0;i < 4;i++) {int nextx = newx + dir[i][0];int nexty = newy + dir[i][1];if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;if(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {vistied[nextx][nexty] = true;qu.push(pair<int, int>(nextx, nexty));if(flag) map[nextx][nexty] = 0;else result[nextx][nexty] = 0;}}}
}int main() {int n,m;cin >> n >> m;vector<vector<int>> map(n, vector<int>(m));vector<vector<int>> result(n, vector<int>(m));for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {cin >> map[i][j];result[i][j] = map[i][j];}}vector<vector<bool>> vistied(n, vector<bool>(m, false));for(int j = 0;j < m;j++) {if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, result, 0, j, true);if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, result, n - 1, j, true);}for(int i = 0;i < n;i++) {if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, result, i, 0, true);if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, result, i, m - 1, true);}for(int i = 0;i < n;i++) {for(int j = 0;j < m;j++) {if(!vistied[i][j] && map[i][j] == 1) {bfs(map,vistied, result,i,j, false);}}}for(int i = 0;i < n;i++) {for(int j = 0;j < m - 1;j++) {cout << result[i][j] << ' ';}cout << result[i][m - 1] << endl;}return 0;
}

题目3:103. 水流问题 (kamacoder.com)

题目4:104. 建造最大岛屿 (kamacoder.com)

相关文章:

  • Xcode will continue when the operation completes
  • MySQL 数据库 Navicat Premium 16.01 安装教程
  • Zookeeper基础教程
  • 计算机网络:网络层 - IP数据报的转发
  • docker方式启动的redis如何使用自定义配置文件
  • word常用的通配符大全
  • [Vulnhub] Troll FTP匿名登录+定时任务权限提升
  • 【Android 11】AOSP Settings添加屏幕旋转按钮
  • 每天一个数据分析题(三百八十三)- 聚类
  • 怎么看电脑实时充电功率
  • 关于Notebook环境的安装记录
  • Ubuntu20.04中复现FoundationPose
  • 永磁同步电机驱动死区补偿
  • TIM: A Time Interval Machine for Audio-Visual Action Recognition
  • Gflags的使用
  • 77. Combinations
  • Angular Elements 及其运作原理
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • Java的Interrupt与线程中断
  • Linux gpio口使用方法
  • node 版本过低
  • PAT A1050
  • Redis 中的布隆过滤器
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • vue从创建到完整的饿了么(11)组件的使用(svg图标及watch的简单使用)
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 测试如何在敏捷团队中工作?
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 关于字符编码你应该知道的事情
  • 基于Vue2全家桶的移动端AppDEMO实现
  • 我这样减少了26.5M Java内存!
  • 移动端解决方案学习记录
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • ​【已解决】npm install​卡主不动的情况
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • (4)事件处理——(7)简单事件(Simple events)
  • (android 地图实战开发)3 在地图上显示当前位置和自定义银行位置
  • (Matlab)使用竞争神经网络实现数据聚类
  • (二)pulsar安装在独立的docker中,python测试
  • (分布式缓存)Redis哨兵
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (佳作)两轮平衡小车(原理图、PCB、程序源码、BOM等)
  • (四)JPA - JQPL 实现增删改查
  • (转)Sublime Text3配置Lua运行环境
  • .MyFile@waifu.club.wis.mkp勒索病毒数据怎么处理|数据解密恢复
  • .NET Core WebAPI中封装Swagger配置
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .NET LINQ 通常分 Syntax Query 和Syntax Method
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • @entity 不限字节长度的类型_一文读懂Redis常见对象类型的底层数据结构
  • @serverendpoint注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)