宝岛探险(DFS/BFS)

小哼通过秘密方法获得钓鱼岛的航拍地图,计划使用深度优先搜索或广度优先搜索算法计算从(6,8)位置开始的岛屿面积。此算法需遍历相连的陆地格子,统计岛屿大小。

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

 

标题:宝岛探险
标签:搜索 深度优先搜索 广度优先搜索
详情:

小哼通过秘密方法得到一张不完整的钓鱼岛航拍地图。钓鱼岛由一个主岛和一些附属岛屿组成,小哼决定去钓鱼岛探险。下面这个10*10的二维矩阵就是钓鱼岛的航拍地图。图中数字表示海拔,0表示海洋,1~9都表示陆地。小哼的飞机将会降落在(6,8)处,现在需要计算出小哼降落所在岛的面积(即有多少个格子)。注意此处我们把与小哼降落点上下左右相链接的陆地均视为同一岛屿。

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

0

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

输入格式:
一行4个整数,前两个整数表示n行m列,后两个整数表示降落的坐标x行y列
输出格式:
一个整数表示岛屿的面积
限制:n<=100
m<=100
样例:

输入

10 10 6 8
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 0
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

输出

38

 

/* BFS */
#include <stdio.h>
#define maxn 105
int ar[maxn][maxn], flag[maxn][maxn];
int next[4][2] = {
    0, 1,
    1, 0,
    0, -1,
    -1, 0,
};
struct node{
    int x;
    int y;
}que[10050];

int main()
{
    int i, j, k, tx, ty, n, m, p, q, head = 1, tail = 1, step = 1;
    scanf("%d %d %d %d", &n, &m, &p, &q);
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            scanf("%d", &ar[i][j]);
    flag[p][q] = 1;
    que[tail].x = p;
    que[tail].y = q;
    tail++;
    while(head < tail){
        for(k = 0; k < 4; k++)
        {
            tx = que[head].x + next[k][0];
            ty = que[head].y + next[k][1];
            if(tx > n || ty > m || tx < 1 || ty < 1)
                continue;
            if(ar[tx][ty] != 0 && flag[tx][ty] == 0)
            {
                que[tail].x = tx;
                que[tail].y = ty;
                flag[tx][ty] = 1;
                tail++;
                step++;
            }
        }
        head++;
    }
    printf("%d", step);
    return 0;
}
/* DFS */
#include <stdio.h>
#define maxn 105
int ar[maxn][maxn], flag[maxn][maxn];
int n, m, p, q, step=1;
int next[4][2] = {
    0, 1,
    1, 0,
    0, -1,
    -1, 0,
};

void dfs(int x, int y)
{
    int k, tx, ty;
    for(k = 0; k < 4; k++)
    {
        tx = x + next[k][0];
        ty = y + next[k][1];
        if(tx > n || ty > m || tx < 1 || ty < 1)
            continue;
        if(ar[tx][ty] != 0 && flag[tx][ty] == 0)
        {
            flag[tx][ty] = 1;
            step++;
            dfs(tx,ty);
        }
    }
    return;
}
int main()
{
    int i, j;
    scanf("%d %d %d %d", &n, &m, &p, &q);
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            scanf("%d", &ar[i][j]);
    flag[p][q] = 1;
    dfs(p,q);
    printf("%d", step);

    return 0;
}

在原本dfs的基础上,添加少量代码即可实现简单的FLOODFILL漫水填充法:

#include <stdio.h>
#define maxn 105
int ar[maxn][maxn], flag[maxn][maxn];
int n, m, p, q, step=1;
int next[4][2] = {
    0, 1,
    1, 0,
    0, -1,
    -1, 0,
};

void dfs(int x, int y, int color)
{
    int k, tx, ty;
    ar[x][y] = color; /**/
    for(k = 0; k < 4; k++)
    {
        tx = x + next[k][0];
        ty = y + next[k][1];
        if(tx > n || ty > m || tx < 1 || ty < 1)
            continue;
        if(ar[tx][ty] != 0 && flag[tx][ty] == 0)
        {
            flag[tx][ty] = 1;
            step++;
            dfs(tx,ty,color);
        }
    }
    return;
}
int main()
{
    int i, j;
    scanf("%d %d %d %d", &n, &m, &p, &q);
    for(i = 1; i <= n; i++)
        for(j = 1; j <= m; j++)
            scanf("%d", &ar[i][j]);
    flag[p][q] = 1;
    dfs(p, q, -1); /**/
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++){
            printf("%3d", ar[i][j]);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值