99. 岛屿数量
题目链接
文章讲解
dfs
#include<bits/stdc++.h>
using namespace std;
vector<int> path;
int ans;
int direct[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
void dfs(vector<vector<int>>& grid, vector<vector<bool>>& visit, int x, int y)
{
if(grid[x][y] == 0 || visit[x][y]) return;
visit[x][y] = true;
for(int i = 0; i < 4; i++)
{
int nextx = x + direct[i][0];
int nexty = y + direct[i][1];
if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
dfs(grid, visit, nextx, nexty);
}
}
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<bool>> visit(n, vector<bool>(m, false));
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] == 1 && !visit[i][j])
{
ans++;
dfs(grid, visit, i, j);
}
}
}
cout << ans;
}
bfs
文章讲解
#include<bits/stdc++.h>
using namespace std;
int ans;
int direct[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<bool>> visit(n, vector<bool>(m, false));
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(!visit[i][j] && grid[i][j] == 1)
{
ans++;
queue<pair<int, int>> q;
q.push({i, j});
visit[i][j] = true;
while(!q.empty())
{
pair<int, int> cur = q.front();
q.pop();
int curx = cur.first;
int cury = cur.second;
for(int i = 0; i < 4; i++)
{
int nextx = curx + direct[i][0];
int nexty = cury + direct[i][1];
if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
if(grid[nextx][nexty] == 1 && !visit[nextx][nexty])
{
q.push({nextx, nexty});
visit[nextx][nexty] = true;
}
}
}
}
}
}
cout << ans;
}
#include<bits/stdc++.h>
using namespace std;
vector<int> path;
int ans;
int direct[4][2]={0,1,1,0,-1,0,0,-1};
void bfs(vector<vector<int>>& grid,vector<vector<bool>>& visit,int x,int y)
{
queue<pair<int,int>> q;
q.push({x,y});
visit[x][y]=true;
while(!q.empty())
{
pair<int,int> cur=q.front();
q.pop();
int curx=cur.first;
int cury=cur.second;
for(int i=0;i<4;i++)
{
int nextx=curx+direct[i][0];
int nexty=cury+direct[i][1];
if(nextx<0||nextx>=grid.size()||nexty<0||nexty>=grid[0].size()) continue;
if(grid[nextx][nexty]==1&&!visit[nextx][nexty])
{
q.push({nextx,nexty});
visit[nextx][nexty]=true;
}
}
}
}
int main(){
int n,m;
cin>>n>>m;
vector<vector<int>> grid(n,vector<int>(m,0));
vector<vector<bool>> visit(n,vector<bool>(m,false));
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(!visit[i][j]&&grid[i][j]==1) {
ans++;
bfs(grid,visit,i,j);
}
}
}
cout<<ans;
}
100. 岛屿的最大面积
题目链接
文章讲解
bfs
#include<bits/stdc++.h>
using namespace std;
vector<int> path;
int ans;
int res;
int direct[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
void bfs(vector<vector<int>>& grid, vector<vector<bool>>& visit, int x, int y, int& sum)
{
queue<pair<int, int>> q;
q.push({x, y});
visit[x][y] = true;
while(!q.empty())
{
pair<int, int> cur = q.front();
q.pop();
int curx = cur.first;
int cury = cur.second;
for(int i = 0; i < 4; i++)
{
int nextx = curx + direct[i][0];
int nexty = cury + direct[i][1];
if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
if(grid[nextx][nexty] == 1 && !visit[nextx][nexty])
{
q.push({nextx, nexty});
visit[nextx][nexty] = true;
sum++;
}
}
}
}
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m, 0));
vector<vector<bool>> visit(n, vector<bool>(m, false));
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(!visit[i][j] && grid[i][j] == 1)
{
ans++;
int sum = 1;
bfs(grid, visit, i, j, sum);
res = max(sum, res);
}
}
}
cout << res;
}