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

算法训练营|图论第二天 99.岛屿数量 100.岛屿的最大面积

题目:99.岛屿数量

题目链接:

99. 岛屿数量 (kamacoder.com)

代码:

深度优先搜索:

#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
	for (int i = 0; i < 4; i++) {
		int nextx = x + dir[i][0];
		int nexty = y + dir[i][1];
		if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;
		if (grid[nextx][nexty] == 1 && !visited[nextx][nexty]) {
			visited[nextx][nexty] = true;
			dfs(grid, visited, nextx, nexty);
		}
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	vector<vector<bool>>visited(n, vector<bool>(m, false));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (grid[i][j] == 1 && visited[i][j] == false) {
				dfs(grid, visited, i, j);
				result++;
			}
		}
	}
	cout << result << endl;
}

广度优先搜索:

#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
	queue<pair<int, int>>que;
	que.push({ x,y });
	while (!que.empty()) {
		pair<int, int> cur = que.front();
		que.pop();
		int curx = cur.first;
		int cury = cur.second;
		for (int i = 0; i < 4; i++) {
			int nextx = curx + dir[i][0];
			int nexty = cury + dir[i][1];
			if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;
			if (grid[nextx][nexty] && !visited[nextx][nexty]) {
				que.push({ nextx,nexty });
				visited[nextx][nexty] = true;
				bfs(grid, visited, nextx, nexty);
			}
		}
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	vector<vector<bool>>visited(n, vector<bool>(m, false));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (grid[i][j] == 1 && !visited[i][j]) {
				result++;
				bfs(grid, visited, i, j);
			}
		}
	}
	cout << result << endl;
}

题目:100.岛屿的最大面积

题目链接:

100. 岛屿的最大面积 (kamacoder.com)

代码:

广搜:
 

#include<bits/stdc++.h>
using namespace std;
int ans;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
	queue<pair<int, int>>que;
	que.push({ x,y });
	ans++;
	visited[x][y] = true;
	while (!que.empty()) {
		pair<int, int>cur = que.front();
		que.pop();
		int curx = cur.first;
		int cury = cur.second;
		for (int i = 0; i < 4; i++) {
			int nextx = curx + dir[i][0];
			int nexty = cury + dir[i][1];
			if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;
			if (grid[nextx][nexty] == 1 && !visited[nextx][nexty]) {
				visited[nextx][nexty] = true;
				que.push({ nextx,nexty });
				ans++;
			}
		}
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	vector<vector<bool>>visited(n, vector<bool>(m, false));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!visited[i][j] && grid[i][j]) {
				ans = 0;
				bfs(grid, visited, i, j);
				result = max(result, ans);
			}
		}
	}
	cout << result << endl;
}

 深搜:

#include<bits/stdc++.h>
using namespace std;
int ans;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
	if (grid[x][y] == 0 || visited[x][y]) return;
	visited[x][y] = true;
	ans++;
	for (int i = 0; i < 4; i++) {
		int nextx = x + dir[i][0];
		int nexty = y + dir[i][1];
		if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;
		dfs(grid, visited, nextx, nexty);
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	vector<vector<bool>>visited(n, vector<bool>(m, false));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!visited[i][j] && grid[i][j]) {
				ans = 0;
				dfs(grid, visited, i, j);
				result = max(result, ans);
			}
		}
	}
	cout << result << endl;
}

 深搜思路2:

#include<bits/stdc++.h>
using namespace std;
int ans;
int dir[4][2] = { 0,1,1,0,-1,0,0,-1 };
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
	for (int i = 0; i < 4; i++) {
		int nextx = x + dir[i][0];
		int nexty = y + dir[i][1];
		if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size()) continue;
		if (!visited[nextx][nexty] && grid[nextx][nexty]) {
			visited[nextx][nexty] = true;
			ans++;
			dfs(grid, visited, nextx, nexty);
		}
	}
}
int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<int>>grid(n, vector<int>(m, 0));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int result = 0;
	vector<vector<bool>>visited(n, vector<bool>(m, false));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (!visited[i][j] && grid[i][j]) {
				ans = 0;
				dfs(grid, visited, i, j);
				result = max(result, ans);
			}
		}
	}
	cout << result << endl;
}


http://www.kler.cn/a/284650.html

相关文章:

  • 【AI日记】24.11.14 复习和准备 RAG 项目 | JavaScript RAG Web Apps with LlamaIndex
  • Elasticsearch基本概念及使用
  • c++写一个死锁并且自己解锁
  • Leecode热题100-35.搜索插入位置
  • Elastic Observability 8.16:增强的 OpenTelemetry 支持、高级日志分析和简化的入门流程
  • 大语言模型:解锁自然语言处理的无限可能
  • 1999-2023年上市公司年报文本数据(PDF+TXT)
  • 一文解决CLion控制台(cmd)问题【超详细】
  • SQL SUM() 函数
  • Mac环境下Python3虚拟环境创建、Flask安装以及创建运行第一个最小的Flask项目
  • k8s-pod 实战五 (Startup Probe 详细分析)
  • YoloV8实战:使用YoloV8实现OBB框检测
  • C++(Qt)-GIS开发-QGraphicsView显示瓦片地图简单示例2
  • hadoop技术
  • mmitmproxy 抓包工具使用
  • 本地搭建XSS 漏洞接收平台实践分享
  • 【Centos】制作一键安装包.bin 文件
  • 观察者模式(Observer Pattern)
  • 【鸿蒙开发从0到1-day03】
  • 什么是Java以及Java的发展历程
  • list类底层逻辑实现
  • Mysql的命令大全
  • 黑神话悟空带火了哪些三维建模技术
  • LeetCode全排列2(js简单快通法)
  • 深入探索:Windows Edge对CSS Grid布局Subgrid特性的支持与实践
  • 猫头虎分享:Python库 TensorFlow 的简介、安装、用法详解入门教程