算法训练营day66-孤岛总面积-沉没孤岛-水流问题-建造最大岛屿

发布于:2024-06-28 ⋅ 阅读:(18) ⋅ 点赞:(0)

题目1:101. 孤岛的总面积 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};
int count = 0;
void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, int x, int y, bool flag) {
    queue<pair<int, int>> qu;
    qu.push(pair<int, int>(x, y));
    vistied[x][y] = true;
    if(flag) map[x][y] = 0;
    else count++;
    while(!qu.empty()) {
        pair<int, int> cur = qu.front();
        qu.pop();
        int newx = cur.first;
        int newy = cur.second;
        for(int i = 0;i < 4;i++) {
            int nextx = newx + dir[i][0];
            int nexty = newy + dir[i][1];
            if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
            if(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {
                vistied[nextx][nexty] = true;
                qu.push(pair<int, int>(nextx, nexty));
                if(flag) map[nextx][nexty] = 0;
                else count++;
            }
        }
    }
}

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j]; 
        }
    }
    
    vector<vector<bool>> vistied(n, vector<bool>(m, false));
    for(int j = 0;j < m;j++) {
        if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, 0, j, true);
        if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, n - 1, j, true);
    }
    for(int i = 0;i < n;i++) {
        if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, i, 0, true);
        if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, i, m - 1, true);
    }

    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(!vistied[i][j] && map[i][j] == 1) {
                // if(i != 0 && i != n - 1 && j != 0 && j != m - 1) {
                //     result++;
                // }
                bfs(map,vistied,i,j, false);
            }
        }
    }
    cout << count << endl;
    return 0;
}

题目2:102. 沉没孤岛 (kamacoder.com)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int dir[4][2] = {0,-1,-1,0,0,1,1,0};

void bfs(vector<vector<int>>& map, vector<vector<bool>>& vistied, vector<vector<int>>& result, int x, int y, bool flag) {
    queue<pair<int, int>> qu;
    qu.push(pair<int, int>(x, y));
    vistied[x][y] = true;
    if(flag) map[x][y] = 0;
    else result[x][y] = 0;
    while(!qu.empty()) {
        pair<int, int> cur = qu.front();
        qu.pop();
        int newx = cur.first;
        int newy = cur.second;
        for(int i = 0;i < 4;i++) {
            int nextx = newx + dir[i][0];
            int nexty = newy + dir[i][1];
            if(nextx < 0 || nextx >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
            if(!vistied[nextx][nexty] && map[nextx][nexty] == 1) {
                vistied[nextx][nexty] = true;
                qu.push(pair<int, int>(nextx, nexty));
                if(flag) map[nextx][nexty] = 0;
                else result[nextx][nexty] = 0;
            }
        }
    }
}

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    vector<vector<int>> result(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
            result[i][j] = map[i][j];
        }
    }
    
    vector<vector<bool>> vistied(n, vector<bool>(m, false));
    for(int j = 0;j < m;j++) {
        if(map[0][j] == 1 && !vistied[0][j]) bfs(map, vistied, result, 0, j, true);
        if(map[n - 1][j] == 1 && !vistied[n - 1][j]) bfs(map, vistied, result, n - 1, j, true);
    }
    for(int i = 0;i < n;i++) {
        if(map[i][0] == 1 && !vistied[i][0]) bfs(map, vistied, result, i, 0, true);
        if(map[i][m - 1] == 1 && !vistied[i][m - 1]) bfs(map, vistied, result, i, m - 1, true);
    }

    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(!vistied[i][j] && map[i][j] == 1) {
                bfs(map,vistied, result,i,j, false);
            }
        }
    }
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m - 1;j++) {
             cout << result[i][j] << ' ';
        }
        cout << result[i][m - 1] << endl;
    }
   
    return 0;
}

题目3:103. 水流问题 (kamacoder.com)

#include<iostream>
#include<vector>

using namespace std;
int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};

void dfs(vector<vector<int>>& map, vector<vector<bool>>& visited, int x, int y) {
    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 >= map.size() || nexty < 0 || nexty >= map[0].size()) continue;
        if(!visited[nextx][nexty] && map[nextx][nexty] >= map[x][y]) {
            dfs(map, visited, nextx, nexty);
        }
    }
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    vector<vector<bool>> first(n, vector<bool>(m, false));
    vector<vector<bool>> second(n, vector<bool>(m, false));
    
    for(int i = 0;i < n;i++) {
        dfs(map, first, i, 0);
        dfs(map, second, i, m - 1);
    }
    for(int j = 0;j < m;j++) {
        dfs(map, first, 0, j);
        dfs(map, second, n - 1,j);
    }
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(first[i][j] && second[i][j]) cout << i << ' ' << j << endl;
        }
    }
    return 0;
}

题目4:104. 建造最大岛屿 (kamacoder.com)

#include<iostream>
#include<vector>
#include<unordered_map>
#include<map>
#include<unordered_set>

using namespace std;
unordered_map<int, int> mp;

int n, m;
int dir[4][2] = {0, -1, -1, 0, 0, 1, 1, 0};
void dfs(vector<vector<int>>& map,  vector<vector<bool>>& visited, int x, int y, int mark) {
    visited[x][y] = true;
    mp[mark]++;
    map[x][y] = mark;
    for(int i = 0;i < 4;i++) {
        int nextx = x + dir[i][0];
        int nexty = y + dir[i][1];
        if(nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue;
        if(!visited[nextx][nexty] && map[nextx][nexty] == 1) {
            dfs(map, visited, nextx, nexty, mark);
        }
    }
}

int main() {
    int result = 0;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(m));
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            cin >> map[i][j];
        }
    }
    bool allgrid = true;
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    int mark = 2;
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 0) allgrid = false;
            if(map[i][j] == 1 && !visited[i][j]) {
                dfs(map, visited, i, j, mark);
                mark++;
            }
        }
    }
    
    // for(int i = 0;i < n;i++) {
    //     for(int j = 0;j < m;j++) {
    //         cout << map[i][j] << ' ';
    //     }
    //     cout << endl;
    // }
    // for (auto it = mp.begin(); it != mp.end(); ++it) { 
    //     cout << it->first << ' ' << it->second << endl;
    // // 使用key和value进行操作 
    // }
    if(allgrid) {
        cout << n * m << endl;
        return 0;
    }
    
    unordered_set<int> visited_mp;
    for(int i = 0;i < n;i++) {
        for(int j = 0;j < m;j++) {
            if(map[i][j] == 0) {
                int size = 1;
                visited_mp.clear();
                // 这里扩展的时候不能对同一个岛屿再次相加
                for(int ii = 0;ii < 4;ii++) {
                    int next_x = i + dir[ii][0];
                    int next_y = j + dir[ii][1];
                    if(next_x < 0 || next_x >= n || next_y < 0 || next_y >= m) continue;
                    if(visited_mp.count(map[next_x][next_y])) continue;
                    auto iter = mp.find(map[next_x][next_y]);
                    if(iter != mp.end()) {
                            size += iter->second;
                            visited_mp.insert(map[next_x][next_y]);
                    }
                }
                // cout << "size : " << size << endl;
                result = max(result, size);
            }
        }
    }
    cout << result << endl;
    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到