代码随想录day51 99.岛屿数量 深搜 99.岛屿数量 广搜 100.岛屿的最大面积

代码随想录day51 99.岛屿数量 深搜 99.岛屿数量 广搜 100.岛屿的最大面积

99.岛屿数量 深搜

代码随想录

#include <bits/stdc++.h>
using namespace std;

int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {
    for (int i = 0; i < 4; i++) {
        int nextRow = row + dir[i][0];
        int nextCol = col + dir[i][1];
        if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {
            continue;
        }
        if (graph[nextRow][nextCol] == 1 && !visted[nextRow][nextCol]) {
            visted[nextRow][nextCol] = true;
            dfs(graph, visted, nextRow, nextCol);
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visted(N, vector<bool>(M, false));
    int result = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (graph[i][j] == 1 && !visted[i][j]) {
                result++;
                dfs(graph, visted, i, j);
            }
        }
    }
    cout << result << "\n";
    return 0;
}

99.岛屿数量 广搜

代码随想录

#include <bits/stdc++.h>
using namespace std;

int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
void bfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {
    queue<pair<int, int>> que;
    que.push({row, col});
    visted[row][col] = true;
    while (!que.empty()) {
        pair<int, int> cur = que.front();
        que.pop();
        int curRow = cur.first;
        int curCol = cur.second;
        for (int i = 0; i < 4; i++) {
            int nextRow = curRow + dir[i][0];
            int nextCol = curCol + dir[i][1];
            if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {
               continue;
            }
            if (graph[nextRow][nextCol] != 0 && !visted[nextRow][nextCol]) {
                que.push({nextRow, nextCol});
                visted[nextRow][nextCol] = true;
            }
        } 
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visted(N, vector<bool>(M, false));
    int result = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (graph[i][j] == 1 && !visted[i][j]) {
                result++;
                bfs(graph, visted, i, j);
            }
        }
    }
    cout << result << "\n";
    return 0;
}

100.岛屿的最大面积

代码随想录

#include <iostream>
#include <vector>
using namespace std;


int count;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {
    count++;
    for (int i = 0; i < 4; i++) {
        int nextRow = row + dir[i][0];
        int nextCol = col + dir[i][1];
        if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {
            continue;
        }
        if (graph[nextRow][nextCol] == 1 && !visted[nextRow][nextCol]) {
            visted[nextRow][nextCol] = true;
            dfs(graph, visted, nextRow, nextCol);
        }
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visted(N, vector<bool>(M, false));
    int result = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (graph[i][j] == 1 && !visted[i][j]) {
                count = 0;
                visted[i][j] = true;
                dfs(graph, visted, i, j);
                result = max(result, count);
            }
        }
    }
    cout << result << "\n";
    return 0;
}
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int count;
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
void bfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {
    queue<pair<int, int>> que;
    que.push({row, col});
    visted[row][col] = true;
    count++;
    while (!que.empty()) {
        pair<int, int> cur = que.front();
        que.pop();
        int curRow = cur.first;
        int curCol = cur.second;
        for (int i = 0; i < 4; i++) {
            int nextRow = curRow + dir[i][0];
            int nextCol = curCol + dir[i][1];
            if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size()) {
               continue;
            }
            if (graph[nextRow][nextCol] != 0 && !visted[nextRow][nextCol]) {
                que.push({nextRow, nextCol});
                visted[nextRow][nextCol] = true;
                count++;
            }
        } 
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<vector<int>> graph(N, vector<int>(M));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visted(N, vector<bool>(M, false));
    int result = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (graph[i][j] == 1 && !visted[i][j]) {
                count = 0;
                bfs(graph, visted, i, j);
                result = max(result, count);
            }
        }
    }
    cout << result << "\n";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值