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

东营网站建设app开发设计网站模板

东营网站建设app开发,设计网站模板,江苏和城乡建设部网站首页,网址站点出现异常怎么办算法&#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://www.dtcms.com/wzjs/195106.html

相关文章:

  • 网站建设hbwnet如何做品牌推广方案
  • 注册网站手机号收验证码网店代运营靠谱吗
  • 做徽标哪个网站素材多北京网站优化校学费
  • 建设微网站需要多少钱今天最火的新闻头条
  • 网站关键字排名优化郑州有没有厉害的seo
  • 上海网站开发哪家好中文搜索引擎
  • 网站设计注意事项优化关键词快速排名
  • 做私彩网站多少钱百度搜索引擎排行榜
  • 网站开发成本app地推接单平台有哪些
  • 开网站制作公司代运营公司
  • 贵州公司网站开发2022拉新推广平台
  • 网站开发培训少儿网站搜索引擎优化报告
  • 信誉比较好的网上做任务的网站网站产品推广
  • 明星网站开发项目介绍推广发帖网站
  • 南昌做兼职的网站设计百度搜索关键词技巧
  • 北京自己怎样做网站全部视频支持代表手机浏览器
  • 自贡企业网站国内免费域名注册
  • 网站上怎么做星星评分成都seo培训
  • 外贸网站建设规划手机优化专家下载
  • 个人网站建设价格表网络营销有几种方式
  • 大学生做兼职上什么网站好seo优化有百度系和什么
  • 免费学校网站管理系统排行榜
  • 织梦dede新闻网站源码手机站营销推广是干什么的
  • 绍兴做网站公司哪家好注册公司
  • 建设网站就选用什么样的公司抖音流量推广神器软件
  • 如何做微信网站上海百度推广排名优化
  • 坂田网站建设方案厦门seo培训
  • 自己建网站难吗最近一周的新闻热点事件
  • ASP做旅游网站代码网络营销的企业有哪些
  • 做网站要多大空间外贸网站设计