题目:99.岛屿数量
题目链接:
代码:
深度优先搜索:
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.岛屿的最大面积
题目链接:
代码:
广搜:
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;
}