卡码网 99 岛屿数量(深搜版)
基本就是回溯算法 翻版了
#include <iostream>
#include <vector>
using namespace std;
int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
void dfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int i_index, int j_index) {
if (!grid[i_index][j_index] || visited[i_index][j_index]) return;
visited[i_index][j_index] = 1;
for (int i = 0; i < 4; i++) {
if (i_index + dir[i][0] >= 0 && i_index + dir[i][0] < grid.size()) {
if (j_index + dir[i][1] >= 0 && j_index + dir[i][1] < grid[0].size()) {
dfs(grid, visited, i_index + dir[i][0], j_index + dir[i][1]);
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<int>> visited(n, vector<int>(m, 0));
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] && !visited[i][j]) {
num++;
dfs(grid, visited, i, j);
}
}
}
cout << num << endl;
return 0;
}
卡码网 99 岛屿数量(广搜版)
类比二叉树层序遍历可以由上面的代码修改得到
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
void bfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int i_index, int j_index) {
queue<pair<int, int>> que;
que.push(pair<int, int>(i_index, j_index));
while (!que.empty()) {
// int num = que.size();
// while (num--) {
i_index = que.front().first;
j_index = que.front().second;
for (int i = 0; i < 4; i++) {
if (i_index + dir[i][0] >= 0 && i_index + dir[i][0] < grid.size()) {
if (j_index + dir[i][1] >= 0 && j_index + dir[i][1] < grid[0].size()) {
if (grid[i_index+dir[i][0]][j_index+dir[i][1]] &&
!visited[i_index+dir[i][0]][j_index+dir[i][1]]) {
visited[i_index+dir[i][0]][j_index+dir[i][1]] = 1;
que.push(pair<int, int>(i_index+dir[i][0],j_index+dir[i][1]));
}
}
}
}
que.pop();
// }
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<int>> visited(n, vector<int>(m, 0));
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] && !visited[i][j]) {
num++;
visited[i][j] = 1;
bfs(grid, visited, i, j);
}
}
}
cout << num << endl;
return 0;
}
卡码网 100 岛屿的最大面积
加了if语句后很神奇地会出问题,改用max,可能判断语句优先级比bfs函数内部执行优先级要高,采用max函数就能解决这个问题
代码如下:
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
int bfs(vector<vector<int>>& grid, vector<vector<int>>& visited, int i_index, int j_index) {
int sum = 0;
queue<pair<int, int>> que;
que.push(pair<int, int>(i_index, j_index));
while (!que.empty()) {
// int num = que.size();
// while (num--) {
i_index = que.front().first;
j_index = que.front().second;
for (int i = 0; i < 4; i++) {
if (i_index + dir[i][0] >= 0 && i_index + dir[i][0] < grid.size()) {
if (j_index + dir[i][1] >= 0 && j_index + dir[i][1] < grid[0].size()) {
if (grid[i_index+dir[i][0]][j_index+dir[i][1]] &&
!visited[i_index+dir[i][0]][j_index+dir[i][1]]) {
visited[i_index+dir[i][0]][j_index+dir[i][1]] = 1;
sum++;
que.push(pair<int, int>(i_index+dir[i][0],j_index+dir[i][1]));
}
}
}
}
que.pop();
// }
}
return sum + 1;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<int>> visited(n, vector<int>(m, 0));
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] && !visited[i][j]) {
visited[i][j] = 1;
// if (sum < bfs(grid, visited, i, j))
sum = max(sum, bfs(grid, visited, i, j));
// cout << sum << endl;
}
}
}
cout << sum << endl;
return 0;
}