算法训练营|图论第二天 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;
}
相关推荐
chao_78916 分钟前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
十盒半价30 分钟前
从递归到动态规划:手把手教你玩转算法三剑客
javascript·算法·trae
GEEK零零七36 分钟前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战1 小时前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
秋说1 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
WebInfra1 小时前
如何在程序中嵌入有大量字符串的 HashMap
算法·设计模式·架构
森焱森2 小时前
APM与ChibiOS系统
c语言·单片机·算法·架构·无人机
★Orange★2 小时前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
尘世闲鱼2 小时前
解数独(C++版本)
开发语言·c++·算法·解数独
qqxhb2 小时前
零基础数据结构与算法——第四章:基础算法-排序(中)
数据结构·算法·排序算法·归并·快排·堆排