99. 岛屿数量
给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。
输入描述
第一行包含两个整数 N, M,表示矩阵的行数和列数。
后续 N 行,每行包含 M 个数字,数字为 1 或者 0。
输出描述
> 输出一个整数,表示岛屿的数量。如果不存在岛屿,则输出 0。
输入示例
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
3
深度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水
visited[x][y] = true; // 标记访问过
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || 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];
}
}
vector<vector<bool>> visited(n, vector<bool>(m, false));
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
result++; // 遇到没访问过的陆地,+1
dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
}
}
}
cout << result << endl;
return 0;
}
广度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void bfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y) {
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 + dir[i][0];
int nexty = cury + dir[i][1];
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) {
que.push({nextx, nexty});
visited[nextx][nexty] = true; // 只要加入队列立刻标记
}
}
}
}
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];
}
}
vector<vector<bool>> visited(n, vector<bool>(m, false));
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
result++; // 遇到没访问过的陆地,+1
bfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
}
}
}
cout << result << endl;
return 0;
}
100. 岛屿的最大面积
题目描述
给定一个由 1(陆地)和 0(水)组成的矩阵,计算岛屿的最大面积。岛屿面积的计算方式为组成岛屿的陆地的总数。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。
输入描述
第一行包含两个整数 N, M,表示矩阵的行数和列数。后续 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。
输出描述
输出一个整数,表示岛屿的最大面积。如果不存在岛屿,则输出 0。
输入示例
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
4
深度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void dfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y,int& s) {
if (visited[x][y] || grid[x][y] == 0) return; // 终止条件:访问过的节点 或者 遇到海水
s++;
visited[x][y] = true; // 标记访问过
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过
dfs(grid, visited, nextx, nexty, s);
}
}
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];
}
}
vector<vector<bool>> visited(n, vector<bool>(m, false));
int maxs = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
int s = 0; // 遇到没访问过的陆地,+1
dfs(grid, visited, i, j, s); // 将与其链接的陆地都标记上 true
maxs = max(maxs, s);
}
}
}
cout << maxs << endl;
return 0;
}
广度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1}; // 四个方向
void bfs(const vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y, int& s) {
queue<pair<int,int>> que;
que.push({x, y});
s++;
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 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue; // 越界了,直接跳过
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) {
s++;
que.push({nextx, nexty});
visited[nextx][nexty] = true; // 只要加入队列立刻标记
}
}
}
}
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];
}
}
vector<vector<bool>> visited(n, vector<bool>(m, false));
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && grid[i][j] == 1) {
int s = 0; // 遇到没访问过的陆地,+1
bfs(grid, visited, i, j, s); // 将与其链接的陆地都标记上 true
result = max(result, s);
}
}
}
cout << result << endl;
return 0;
}
101. 孤岛的总面积
题目描述
给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域,且完全被水域单元格包围。孤岛是那些位于矩阵内部、所有单元格都不接触边缘的岛屿。
现在你需要计算所有孤岛的总面积,岛屿面积的计算方式为组成岛屿的陆地的总数。
输入描述
第一行包含两个整数 N, M,表示矩阵的行数和列数。之后 N 行,每行包含 M 个数字,数字为 1 或者 0。
输出描述
输出一个整数,表示所有孤岛的总面积,如果不存在孤岛,则输出 0。
输入示例
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
1
深度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};//四个方向
int res;
void dfs(vector<vector<int>>& grid, int x, int y){
grid[x][y] = 0;
res++;
for(int i = 0; i < 4; i++){
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size()) continue;
if(grid[nextx][nexty] == 0) continue;
dfs(grid, nextx, nexty);
}
return;
}
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m));
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++){
if(grid[i][0] == 1)
dfs(grid, i, 0);
if(grid[i][m - 1] == 1)
dfs(grid, i, m - 1);
}
for(int j = 0; j < m; j++){
if(grid[0][j] == 1)
dfs(grid, 0, j);
if(grid[n - 1][j] == 1)
dfs(grid, n - 1, j);
}
res = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(grid[i][j] == 1)
res++;
}
}
cout << res << endl;
return 0;
}
广度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};//四个方向
int res;
void bfs(vector<vector<int>>& grid, int x, int y){
queue<pair<int,int>> que;
que.push({x, y});
grid[x][y] = 0;
res++;
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 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())
continue;
if(grid[nextx][nexty] == 1){
que.push({nextx, nexty});
res++;
grid[nextx][nexty] = 0;
}
}
}
}
int main(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m));
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++){
if(grid[i][0] == 1)
bfs(grid, i, 0);
if(grid[i][m - 1] == 1)
bfs(grid, i, m - 1);
}
for(int j = 0; j < m; j++){
if(grid[0][j] == 1)
bfs(grid, 0, j);
if(grid[n - 1][j] == 1)
bfs(grid, n - 1, j);
}
res = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(grid[i][j] == 1)
res++;
}
}
cout << res << endl;
return 0;
}
102. 沉没孤岛
题目描述
给定一个由 1(陆地)和 0(水)组成的矩阵,岛屿指的是由水平或垂直方向上相邻的陆地单元格组成的区域,且完全被水域单元格包围。孤岛是那些位于矩阵内部、所有单元格都不接触边缘的岛屿。
现在你需要将所有孤岛“沉没”,即将孤岛中的所有陆地单元格(1)转变为水域单元格(0)。
输入描述
第一行包含两个整数 N, M,表示矩阵的行数和列数。
之后 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。
输出描述
输出将孤岛“沉没”之后的岛屿矩阵。 注意:每个元素后面都有一个空格
输入示例
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
1 1 0 0 0
1 1 0 0 0
0 0 0 0 0
0 0 0 1 1
深度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
void dfs(vector<vector<int>>& grid, int x, int y){
grid[x][y]++;
for(int i = 0;i < 4; i++){
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if(nextx < 0 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())
continue;
if (grid[nextx][nexty] == 0 || grid[nextx][nexty] == 2)
continue;
dfs(grid, 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];
}
}
for(int i = 0; i < n; i++){
if(grid[i][0] == 1)
dfs(grid, i, 0);
if(grid[i][m - 1] == 1)
dfs(grid, i, m - 1);
}
for(int j = 0; j < m; j++){
if(grid[0][j] == 1)
dfs(grid, 0, j);
if(grid[n - 1][j] == 1)
dfs(grid, n - 1, j);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(grid[i][j] > 0)
grid[i][j]--;
cout << grid[i][j] << " ";
}
cout << endl;
}
return 0;
}
广度优先搜索
#include<bits/stdc++.h>
using namespace std;
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
void bfs(vector<vector<int>>& grid, int x, int y){
queue<pair<int,int>> que;
que.push({x, y});
grid[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 || nextx >= grid.size() || nexty < 0 || nexty >= grid[0].size())
continue;
if(grid[nextx][nexty] == 1){
que.push({nextx, nexty});
grid[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];
}
}
for(int i = 0; i < n; i++){
if(grid[i][0] == 1)
bfs(grid, i, 0);
if(grid[i][m - 1] == 1)
bfs(grid, i, m - 1);
}
for(int j = 0; j < m; j++){
if(grid[0][j] == 1)
bfs(grid, 0, j);
if(grid[n - 1][j] == 1)
bfs(grid, n - 1, j);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(grid[i][j] > 0)
grid[i][j]--;
cout << grid[i][j] << " ";
}
cout << endl;
}
return 0;
}