算法训练营|图论第二天 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;
}
相关推荐
jiao000011 小时前
数据结构——队列
c语言·数据结构·算法
迷迭所归处2 小时前
C++ —— 关于vector
开发语言·c++·算法
leon6253 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林3 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z3 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
Aic山鱼3 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
天玑y4 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
sjsjs114 小时前
【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
数据结构·算法·leetcode
redcocal4 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
码了三年又三年4 小时前
【算法】滑动窗口—找所有字母异位词
算法