宝岛探险 BFS DFS

本文通过广度优先搜索(BFS)和深度优先搜索(DFS)两种算法计算从指定位置出发的岛屿面积,并采用着色法统计地图上的岛屿总数。代码示例清晰展示了不同算法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.计算岛屿面积

1.1 BFS

#include <stdio.h>
int a[10][10] = {
    {1,2,1,0,0,0,0,0,2,3},
    {3,0,2,0,1,2,1,0,1,2},
    {4,0,1,0,1,2,3,2,0,1},
    {3,2,0,0,0,1,2,4,0,1},
    {0,0,0,0,0,0,1,5,3,0},
    {0,1,2,1,0,1,5,4,3,0},
    {0,1,2,3,1,3,6,2,1,0},
    {0,0,3,4,8,9,7,5,0,0},
    {0,0,0,3,7,8,6,0,1,2},
    {0,0,0,0,0,0,0,0,1,0}
    };
/*
0表示海洋,1-9表示陆地的海拔,降落在(6,8)处,计算出该岛的面积(有多少个格子)。
即从(6,8)处进行广度优先搜索, sum ++
*/
struct note{
    int x;
    int y;
};
int main(){
    struct note que[101];
    int head,tail;
    int book[10][10]={0};
    int startx = 6, starty = 8;
    int sum=0;
    int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};//右下左上

    //初始化队列
    head = 1;
    tail = 1;
    que[tail].x = startx;
    que[tail].y = starty;
    book[startx][starty] = 1;
    tail++;
    sum++;
    while(head < tail){
        for(k = 0; k <= 3; k++){
            tx = que[head].x + next[k][0];
            ty = que[head].y + next[k][1];
            if(tx < 0 || tx > 9 || ty < 0 || ty > 9){
            continue;
            }
            if(a[tx][ty] > 0 && book[tx][ty] == 0){
                book[tx][ty] = 1;
                sum++;
                que[tail].x = tx;
                que[tail].y = ty;
                tail++;
            }

        }
        head++;
    }
    printf("该岛屿面积为%d.\n",sum);
    getchar();
    return 0;

}

1.2 DFS

#include <stdio.h>
int a[10][10] = {
    {1,2,1,0,0,0,0,0,2,3},
    {3,0,2,0,1,2,1,0,1,2},
    {4,0,1,0,1,2,3,2,0,1},
    {3,2,0,0,0,1,2,4,0,1},
    {0,0,0,0,0,0,1,5,3,0},
    {0,1,2,1,0,1,5,4,3,0},
    {0,1,2,3,1,3,6,2,1,0},
    {0,0,3,4,8,9,7,5,0,0},
    {0,0,0,3,7,8,6,0,1,2},
    {0,0,0,0,0,0,0,0,1,0}
    };
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};//右下左上
/*
0表示海洋,1-9表示陆地的海拔,降落在(6,8)处,计算出该岛的面积(有多少个格子)。
即从(6,8)处进行深度优先搜索, sum ++
*/
int book[10][10] = {0};
int sum;
void dfs(int x,int y){
    int k,tx,ty;

    for(k=0;k<=3;k++){
        tx = x + next[k][0];
        ty = y + next[k][1];
        if(tx < 0 || tx > 9 || ty < 0 || ty > 9){
            continue;
        }
        if(a[tx][ty] > 0 && book[tx][ty] == 0){
            sum ++;
            book[tx][ty] = 1;
            dfs(tx,ty);
        }
    }
    return ;
}

int main(){
    int i,j,startx = 6,start = 8;
    book[startx][starty] = 1;
    sum = 1;
    dfs(startx.starty);
    printf("%d\n",sum);
    getchar();
    return 0;

}

2.一共有几个岛(独立子图个数)

着色法

#include <stdio.h>
int a[10][10] = {
    {1,2,1,0,0,0,0,0,2,3},
    {3,0,2,0,1,2,1,0,1,2},
    {4,0,1,0,1,2,3,2,0,1},
    {3,2,0,0,0,1,2,4,0,1},
    {0,0,0,0,0,0,1,5,3,0},
    {0,1,2,1,0,1,5,4,3,0},
    {0,1,2,3,1,3,6,2,1,0},
    {0,0,3,4,8,9,7,5,0,0},
    {0,0,0,3,7,8,6,0,1,2},
    {0,0,0,0,0,0,0,0,1,0}
    };

/*
0表示海洋,1-9表示陆地的海拔。求有几个岛屿。
*/
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};//右下左上
int book[10][10] = {0};
int sum;

void dfs(int x,int y, int color){
    int k, tx, ty;
    a[x][y] = color;
    for(k = 0; k <= 3; k++){
        tx = x + next[k][0];
        ty = y + next[k][1];
        if(tx < 0 || tx > 9 || ty < 0 || ty > 9){
            continue;
        }
        if(a[tx][ty] > 0 && book[tx][ty] == 0){
            book[tx][ty] = 1;
            sum++;
            dfs(tx,ty,color);
        }
    }
    return ;
}

int main(){
    int i,j,num = 0;
    for(i=0; i<=9;i++){
        for(j=0;j<=9;j++){
            if(a[i][j] > 0){
                num--;
                book[i][j] = 1;
                dfs(i,j,num);
            }
        }
    }
    //输出着色后的地图
    for(i=0; i<=9;i++){
        for(j=0;j<=9;j++){
            printf("%3d",a[i][j]);
        }
        printf("\n");
    }
    printf("共有%d个小岛。",-num);
    getchar();
    return 0;
}
### BFSDFS 算法的区别及使用场景 BFS(广度优先搜索)和 DFS(深度优先搜索)是两种常见的图遍历算法,它们在思想、实现方式以及适用场景上存在显著差异。 #### 1. **基本思想** - **BFS** 是一种逐层扩展的搜索方式,从起始节点出发,优先访问距离当前节点最近的节点。这种特性使得 BFS 具有最短路径性质,适用于寻找无权图中的最短路径[^2]。 - **DFS** 是一种沿着路径深入的搜索方式,从起始节点出发,尽可能深地探索每个分支,直到无法继续前进时才回溯。这种特性使得 DFS 更适合探索所有可能的路径。 #### 2. **数据结构** - **BFS** 使用队列(Queue)来管理待访问的节点,确保先访问的节点的邻居节点也会优先被访问。 - **DFS** 使用栈(Stack)或递归实现,递归方式更为常见,但容易导致栈溢出。 #### 3. **时间与空间复杂度** - **BFS** 和 **DFS** 的时间复杂度均为 $O(V + E)$,其中 $V$ 是顶点数,$E$ 是边数。 - **BFS** 的空间复杂度通常较高,因为它需要存储每一层的所有节点;而 **DFS** 的空间复杂度取决于递归深度,通常在树的深度较小时表现更好。 #### 4. **适用场景** - **BFS** 更适合以下情况: - 寻找无权图中的最短路径。 - 层次遍历问题,例如按层打印二叉树。 - 需要优先访问距离较近的节点的问题,例如社交网络中查找最近的朋友。 - **DFS** 更适合以下情况: - 探索所有可能的路径,例如迷宫问题。 - 需要回溯的场景,例如图的连通性判断。 - 深度优先的特性使得它在某些问题中更容易实现,例如拓扑排序、判断图中是否存在环。 #### 5. **示例代码** 以下是 **BFS** 和 **DFS** 的简单实现示例: **BFS 实现** ```python from collections import deque def bfs(graph, start): visited = set() queue = deque([start]) visited.add(start) while queue: node = queue.popleft() print(node) for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) ``` **DFS 实现** ```python def dfs(graph, start, visited=None): if visited is None: visited = set() visited.add(start) print(start) for neighbor in graph[start]: if neighbor not in visited: dfs(graph, neighbor, visited) ``` #### 6. **总结** - **BFS** 更适合寻找最短路径或层次遍历问题,而 **DFS** 更适合探索所有可能路径或需要回溯的场景。 - **BFS** 使用队列实现,**DFS** 使用栈或递归实现。 - 在实际应用中,选择哪种算法取决于具体问题的需求和数据结构的特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值