99.岛屿数量
99. 岛屿数量 (kamacoder.com)
DFS
思路没问题,内存超限了。
#include<iostream>
#include<vector>using namespace std;int times = 0;inline bool valid(int i, int j, const int &m, const int &n){return i >= 0 && i < n && j >= 0 && j < m;
}void dfs(int i, int j, vector<vector<int>>& isVisited, vector<vector<int>> &island, const int &n,const int &m){isVisited[i][j] = 1;if(valid(i, j + 1, m, n) && island[i][j + 1] == 1 && isVisited[i][j + 1] == 0){dfs(i, j + 1, isVisited, island, n, m);}if(valid(i + 1, j, m, n) && island[i + 1][j] == 1 && isVisited[i + 1][j] == 0){dfs(i + 1, j, isVisited, island, n, m);}if(valid(i - 1, j, m, n) && island[i - 1][j] == 1 && isVisited[i - 1][j] == 0){dfs(i - 1, j, isVisited, island, n, m);}if(valid(i, j - 1, m, n) && island[i][j - 1] == 1 && isVisited[i][j - 1] == 0){dfs(i, j - 1, isVisited, island, n, m);}
}int main(){int n = 0, m = 0;cin >> n >> m;vector<vector<int>> island(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> island[i][j];}}vector<vector<int>> isVisited(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(island[i][j] == 1 && isVisited[i][j] == 0){times++;dfs(i, j, isVisited, island, n, m);}}}cout << times << endl;
}
BFS
#include<iostream>
#include<vector>
#include<queue>
using namespace std;int times = 0;inline bool valid(int i, int j, const int &m, const int &n){return i >= 0 && i < n && j >= 0 && j < m;
}void bfs(int x, int y, vector<vector<int>>& isVisited, vector<vector<int>> &island, const int &n,const int &m){queue<pair<int, int>> qPair;qPair.push({x, y});isVisited[x][y] = 1;while(!qPair.empty()){pair<int, int> curPair = qPair.front();qPair.pop();int i = curPair.first, j = curPair.second;if(valid(i, j + 1, m, n) && island[i][j + 1] == 1 && isVisited[i][j + 1] == 0){qPair.push({i, j + 1});isVisited[i][j + 1] = 1;}if(valid(i + 1, j, m, n) && island[i + 1][j] == 1 && isVisited[i + 1][j] == 0){qPair.push({i + 1, j});isVisited[i + 1][j] = 1;}if(valid(i - 1, j, m, n) && island[i - 1][j] == 1 && isVisited[i - 1][j] == 0){qPair.push({i - 1, j});isVisited[i - 1][j] = 1;}if(valid(i, j - 1, m, n) && island[i][j - 1] == 1 && isVisited[i][j - 1] == 0){qPair.push({i, j - 1});isVisited[i][j - 1] = 1;}}
}int main(){int n = 0, m = 0;cin >> n >> m;vector<vector<int>> island(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> island[i][j];}}vector<vector<int>> isVisited(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(island[i][j] == 1 && isVisited[i][j] == 0){times++;bfs(i, j, isVisited, island, n, m);}}}cout << times << endl;
}
100.岛屿的最大面积
DFS
#include<iostream>
#include<vector>using namespace std;int times = 0;inline bool valid(int i, int j, const int &m, const int &n){return i >= 0 && i < n && j >= 0 && j < m;
}void dfs(int i, int j, vector<vector<int>>& isVisited, vector<vector<int>> &island, const int &n,const int &m){isVisited[i][j] = 1;times++;if(valid(i, j + 1, m, n) && island[i][j + 1] == 1 && isVisited[i][j + 1] == 0){dfs(i, j + 1, isVisited, island, n, m);}if(valid(i + 1, j, m, n) && island[i + 1][j] == 1 && isVisited[i + 1][j] == 0){dfs(i + 1, j, isVisited, island, n, m);}if(valid(i - 1, j, m, n) && island[i - 1][j] == 1 && isVisited[i - 1][j] == 0){dfs(i - 1, j, isVisited, island, n, m);}if(valid(i, j - 1, m, n) && island[i][j - 1] == 1 && isVisited[i][j - 1] == 0){dfs(i, j - 1, isVisited, island, n, m);}
}int main(){int n = 0, m = 0;cin >> n >> m;vector<vector<int>> island(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> island[i][j];}}int ans = 0;vector<vector<int>> isVisited(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(island[i][j] == 1 && isVisited[i][j] == 0){dfs(i, j, isVisited, island, n, m);ans = max(ans, times);times = 0;}}}cout << ans << endl;
}
BFS
#include<iostream>
#include<vector>
#include<queue>
using namespace std;int times = 0;inline bool valid(int i, int j, const int &m, const int &n){return i >= 0 && i < n && j >= 0 && j < m;
}void bfs(int x, int y, vector<vector<int>>& isVisited, vector<vector<int>> &island, const int &n,const int &m){queue<pair<int, int>> qPair;qPair.push({x, y});isVisited[x][y] = 1;times++;while(!qPair.empty()){pair<int, int> curPair = qPair.front();qPair.pop();int i = curPair.first, j = curPair.second;if(valid(i, j + 1, m, n) && island[i][j + 1] == 1 && isVisited[i][j + 1] == 0){qPair.push({i, j + 1});isVisited[i][j + 1] = 1;times++;}if(valid(i + 1, j, m, n) && island[i + 1][j] == 1 && isVisited[i + 1][j] == 0){qPair.push({i + 1, j});isVisited[i + 1][j] = 1;times++;}if(valid(i - 1, j, m, n) && island[i - 1][j] == 1 && isVisited[i - 1][j] == 0){qPair.push({i - 1, j});isVisited[i - 1][j] = 1;times++;}if(valid(i, j - 1, m, n) && island[i][j - 1] == 1 && isVisited[i][j - 1] == 0){qPair.push({i, j - 1});isVisited[i][j - 1] = 1;times++;}}
}int main(){int n = 0, m = 0;cin >> n >> m;vector<vector<int>> island(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){cin >> island[i][j];}}int ans = 0;vector<vector<int>> isVisited(n, vector<int>(m, 0));for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(island[i][j] == 1 && isVisited[i][j] == 0){bfs(i, j, isVisited, island, n, m);ans = max(ans, times);times = 0;}}}cout << ans << endl;
}