*99. 岛屿数量
DFS
深度优先遍历。类似于找连通分量。
时间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
// c++
#include<bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y){
// 上下左右 四个方向
int direction[4][2] ={0, 1, 0, -1, -1, 0, 1, 0};
for(int i=0; i<4; i++){
int nextx = x + direction[i][0];
int nexty = y + direction[i][1];
// 防止越界
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(matrix[nextx][nexty] && !visited[nextx][nexty]){
visited[nextx][nexty] = true;
dfs(matrix, visited, nextx, nexty);
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> matrix(n, vector<int>(m, 0));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>matrix[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(matrix[i][j] && !visited[i][j]){
result++;
visited[i][j] = true;
dfs(matrix, visited, i, j);
}
}
}
cout<<result;
return 0;
}
BFS
时间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
// c++
#include<bits/stdc++.h>
using namespace std;
void dfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y){
int direction[4][2] ={0, 1, 0, -1, 1, 0, -1, 0};
for(int i=0; i<4; i++){
int nextx = x + direction[i][0];
int nexty = y + direction[i][1];
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(matrix[nextx][nexty] && !visited[nextx][nexty]){
visited[nextx][nexty] = true;
dfs(matrix, visited, nextx, nexty);
}
}
}
void bfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y){
queue<pair<int, int>> que;
que.push({x, y});
visited[x][y] = true;
int direction[4][2] ={0, 1, 0, -1, 1, 0, -1, 0};
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 + direction[i][0];
int nexty = cury + direction[i][1];
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(matrix[nextx][nexty] && !visited[nextx][nexty]){
que.push({nextx, nexty});
visited[nextx][nexty] = true;
}
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> matrix(n, vector<int>(m, 0));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>matrix[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(matrix[i][j] && !visited[i][j]){
result++;
// visited[i][j] = true;
// dfs(matrix, visited, i, j);
bfs(matrix, visited, i, j);
}
}
}
cout<<result;
return 0;
}
100. 岛屿的最大面积
DFS
时间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
// c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y,
int &cnt){
for(int i=0; i<4; i++){
int nextx = x + direction[i][0];
int nexty = y + direction[i][1];
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(!visited[nextx][nexty] && matrix[nextx][nexty]){
cnt++;
visited[nextx][nexty] = true;
dfs(matrix, visited, nextx, nexty, cnt);
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> matrix(n, vector<int>(m, 0));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>matrix[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] && matrix[i][j]){
int cnt = 1;
visited[i][j] = true;
dfs(matrix, visited, i, j, cnt);
if(cnt > result) result = cnt;
}
}
}
cout<<result;
return 0;
}
BFS
时间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度:
O
(
n
2
)
O(n^2)
O(n2)
//c++
#include<bits/stdc++.h>
using namespace std;
int direction[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
void dfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y,
int &cnt){
for(int i=0; i<4; i++){
int nextx = x + direction[i][0];
int nexty = y + direction[i][1];
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(!visited[nextx][nexty] && matrix[nextx][nexty]){
cnt++;
visited[nextx][nexty] = true;
dfs(matrix, visited, nextx, nexty, cnt);
}
}
}
void bfs(const vector<vector<int>> &matrix,
vector<vector<bool>> &visited,
int x, int y,
int &cnt){
queue<pair<int, int>> que;
que.push({x, y});
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 + direction[i][0];
int nexty = cury + direction[i][1];
if(nextx>=matrix.size() || nexty>=matrix[0].size() || nextx<0 || nexty<0) continue;
if(!visited[nextx][nexty] && matrix[nextx][nexty]){
cnt++;
visited[nextx][nexty] = true;
que.push({nextx, nexty});
}
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> matrix(n, vector<int>(m, 0));
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>matrix[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] && matrix[i][j]){
int cnt = 1;
// visited[i][j] = true;
// dfs(matrix, visited, i, j, cnt);
bfs(matrix, visited, i, j, cnt);
if(cnt > result) result = cnt;
}
}
}
cout<<result;
return 0;
}