当前位置: 首页 > 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://www.dtcms.com/wzjs/816686.html

相关文章:

  • 乐清网站建设哪家好营销团队
  • 没有网站如何做营销仿花瓣 wordpress
  • html企业网站模板电信网站开发语言主要用什么
  • 温州公司建设网站制作北京网络营销技术培训
  • 设计师素材库如何对seo进行优化
  • 设计师服务平台破解房门户网站如何做优化
  • 网站怎么防止黑客攻击宝安网站公司
  • html网页的基本结构seo搜索引擎优化薪酬
  • 购买网站做友情链接拥有域名后怎么建设网站
  • 字体设计网站大全网站建设费用会计处理
  • 网站建设定制开发湖南城乡建设部网站首页
  • 门户网站有什么特点wordpress xmlrpc
  • 蚂蚁网站建设游戏试玩平台代理
  • 营销型网站建设的目标是最简洁wordpress主题
  • app开发网站模板深圳展厅设计
  • seo建站是什么意思潍坊做网站哪家好
  • 商城网站建设平台网站策划书主题
  • 网站应包括的基本功能和高级功能自己网站上做淘宝搜索
  • 工商局网站怎么做股东实名认证网站建设价格由什么决定
  • 象山县住房建设局网站网站域名如何管理
  • 设备做外贸哪个网站好临沂市建设官方网站
  • 吉林建设厅网站首页电脑平面设计培训班多少钱
  • 松江新城投资建设有限公司网站泰州北京网站建设
  • 女子医院网站设计怎么做政协网站信息化建设的作用
  • 用ip地址做网站做内衣的网站好
  • 网上购物的网站有哪些昆明做网站建设怎么样
  • 当当网站建设优点网站开发中网页之间的连接形式
  • 上海做高端网站the7 wordpress 下载
  • 大连网站建设找哪家河南化工厂
  • 天津网站建设品牌推广wordpress交互插件