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

题目:99.岛屿数量

题目链接:

99. 岛屿数量 (kamacoder.com)

代码:

深度优先搜索:

cpp 复制代码
#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;
}

广度优先搜索:

cpp 复制代码
#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)

代码:

广搜:

cpp 复制代码
#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;
}

深搜:

cpp 复制代码
#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:

cpp 复制代码
#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;
}
相关推荐
言之。5 分钟前
【K-Means】
算法·机器学习·kmeans
hummhumm38 分钟前
第 10 章 - Go语言字符串操作
java·后端·python·sql·算法·golang·database
Jeffrey_oWang43 分钟前
软间隔支持向量机
算法·机器学习·支持向量机
算法歌者1 小时前
[算法]入门1.矩阵转置
算法
用户8134411823612 小时前
分布式训练
算法
林开落L2 小时前
前缀和算法习题篇(上)
c++·算法·leetcode
远望清一色2 小时前
基于MATLAB边缘检测博文
开发语言·算法·matlab
tyler_download2 小时前
手撸 chatgpt 大模型:简述 LLM 的架构,算法和训练流程
算法·chatgpt
SoraLuna2 小时前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷2 小时前
中文分词模拟器
开发语言·python·算法