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

建筑工程网站大全大学生做推送的网站

建筑工程网站大全,大学生做推送的网站,房地产销售流程详细,北京发布会直播算法&#xff1a;Flood Fill bfs / dfs 统计被完全淹没的岛屿 两种方法&#xff1a; 1. 使用 total 和 bound 记录岛屿格子的数量和被淹没的格子数量&#xff0c;如果 bound total&#xff0c;说明这个岛屿被完全淹没了。 #include <iostream> #include <cstri…

算法:Flood Fill

bfs / dfs

统计被完全淹没的岛屿

两种方法:

1. 使用 total 和  bound 记录岛屿格子的数量和被淹没的格子数量,如果 bound == total,说明这个岛屿被完全淹没了。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std; typedef pair<int, int> PII;
#define x first
#define y secondconst int N = 1010;int n;
bool st[N][N];
char s[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};void bfs(PII start, int& total, int& bound) { //使用引用可以影响原变量queue<PII> q;q.push(start);st[start.x][start.y] = true;while (!q.empty()) {PII t = q.front();q.pop();total ++ ;//每次有一个格子出栈总数量加一bool is_bound = false;for (int i = 0; i < 4; i++){int x = t.x + dx[i], y = t.y + dy[i];if (x < 0 || x >= n || y < 0 || y >= n) continue;if (st[x][y]) continue;if (s[x][y] == '.'){is_bound = true;//周围有海洋则标记当前格子continue;}st[x][y] = true;q.push({x, y});}if(is_bound) bound ++ ;//当前格子被标记了则bound++}
}int main() {cin >> n;for (int i = 0; i < n; i++) cin >> s[i];// 原始岛屿数量int cnt = 0;for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)if (s[i][j] == '#' && !st[i][j]){int total = 0, bound = 0;bfs({i, j}, total, bound);if(total == bound) cnt ++ ;}}cout << cnt << endl;return 0;
}

2. 使用vector<vector<PII>> islands 记录岛屿数量  vector<PII>记录每个岛屿内部的格子 ,模拟一遍淹没的过程将岛屿变成海洋,如果一个岛屿内部所有的格子都别淹没了res ++ ;(学习vector的使用方法)

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>using namespace std;typedef pair<int, int> PII;
#define x first
#define y secondconst int N = 1010;int n;
bool st[N][N];
char g[N][N];  // 改用g存储原始地图
bool drown[N][N];  // 淹没标记数组
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
vector<vector<PII>> islands;  // 存储所有原始岛屿void bfs(PII start, vector<PII>& island) {queue<PII> q;q.push(start);st[start.x][start.y] = true;island.push_back(start);while (!q.empty()) {auto t = q.front();q.pop();for (int i = 0; i < 4; i++) {int a = t.x + dx[i], b = t.y + dy[i];if (a < 0 || a >= n || b < 0 || b >= n) continue;if (st[a][b] || g[a][b] != '#') continue;st[a][b] = true;q.push({a, b});island.push_back({a, b});}}
}int main() {cin >> n;for (int i = 0; i < n; i++) cin >> g[i];// BFS:记录所有原始岛屿for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (g[i][j] == '#' && !st[i][j]) {vector<PII> island;bfs({i, j}, island);islands.push_back(island);}}}for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (g[i][j] != '#') continue;for (int k = 0; k < 4; k++) {int a = i + dx[k], b = j + dy[k];if (a >= 0 && a < n && b >= 0 && b < n && g[a][b] == '.') {drown[i][j] = true;break;}}}}// 统计完全被淹没的岛屿数量int res = 0;for (auto& island : islands) {bool all_drowned = true;for (auto& p : island) {if (!drown[p.x][p.y]) {  // 存在未被淹没的陆地all_drowned = false;break;}}if (all_drowned) res++;}cout << res << endl;return 0;
}


文章转载自:

http://cUE88uaL.fmswb.cn
http://3oDVRwVj.fmswb.cn
http://SGPrXdsC.fmswb.cn
http://eIi9BewZ.fmswb.cn
http://GCSqxMBF.fmswb.cn
http://uCjhOU6M.fmswb.cn
http://aZeSwyfM.fmswb.cn
http://sO4T1T73.fmswb.cn
http://Siv7ZSsH.fmswb.cn
http://AO6vxTl2.fmswb.cn
http://NzYPwwrW.fmswb.cn
http://44x5vfey.fmswb.cn
http://87Hy3qdA.fmswb.cn
http://637GqMJj.fmswb.cn
http://ubT2G0OX.fmswb.cn
http://0uQTNlaT.fmswb.cn
http://HRRsP8fk.fmswb.cn
http://FMa8wB9d.fmswb.cn
http://q9iQr0n3.fmswb.cn
http://H7ggQ9RG.fmswb.cn
http://iXo3Lyav.fmswb.cn
http://Dou0NJ6v.fmswb.cn
http://wDU5tNAG.fmswb.cn
http://06lFCZAw.fmswb.cn
http://mXbbxNMB.fmswb.cn
http://Sxy19PFW.fmswb.cn
http://2knRQxgn.fmswb.cn
http://gDABZKUX.fmswb.cn
http://0dapiYDe.fmswb.cn
http://CnAvsPGs.fmswb.cn
http://www.dtcms.com/wzjs/629628.html

相关文章:

  • 主流网站开发技术做网站想要中立
  • 好的做网站的苏州市住房城乡建设局网站首页
  • 佛山html5网站建设怎么做企业招聘网站
  • 长丰县住房和城乡建设局网站seo建站技巧
  • 烟台搭建网站建设制作建筑网排焊机
  • 四川建设企业网站wordpress文章末尾广告位
  • 导航类网站模板株洲市住房和城乡建设局网站
  • 使用flashfxp上传网站二级域名怎么注册
  • 邢台移动网站建设纹理网站推荐
  • 建设银行手机登陆网站营销有哪些基本内容
  • 富阳营销型网站建设成视频app下无限看ios7
  • 网站开发 培训 周末班余姚公司做网站
  • 青岛网站排名外包公司名字查询
  • flash国外网站app使用什么做的网站吗
  • 云南省建设测量员注册网站南山附近公司做网站建设多少钱
  • 怎么建网站赚钱中国十大装修公司
  • 正规网站建设代理中国国际贸易单一窗口网站
  • 网站关键词的写法实体企业做网站好么
  • 广东建设信息网站百度网站的安全建设方案
  • 物流网站给做软件下载wordpress插件汉化教程视频
  • 中山皇冠建设开发有限公司网站房地产三道红线
  • 哪个网站做螺丝生意好西丽网站设计
  • 手机网站排名优化建站网站源码
  • 桥头镇网站建设ps怎么做网站特效
  • 泰安建设厅网站音乐网站设计总结
  • 泰州网站建设物美价廉汕头市潮南区紧急提醒
  • 网站后台登陆密码破解做dnf辅助官方网站
  • 牡丹江网站建设公司建站公司成功案例
  • wordpress网站二次开发中文域名指向同一个网站
  • 长春网站建设新格郑州短视频拍摄公司