红与黑 bfs 广度优先搜索

在这里插入图片描述
在这里插入图片描述

#include<stdio.h>

struct pos{
    int x;
    int y;
};
struct pos que[10010];
int move[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char bfs[25][25];
int book[25][25];
int maxn;
int count=1;

int main(){
    int w,h,head,tail;
    scanf("%d %d",&w,&h);
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++)
            scanf(" %c",&bfs[i][j]);  
    for(int i=0;i<h;i++)
        for(int j=0;j<w;j++)
        {
            if(bfs[i][j]=='@'){
                book[i][j]=1;
                que[0].x=i;
                que[0].y=j;
            }
        }
    tail++;
    while(head<tail){
        for(int i=0;i<4;i++){
            if(book[que[head].x+move[i][0]][que[head].y+move[i][1]]==1){
                continue;
            }
            if(bfs[que[head].x+move[i][0]][que[head].y+move[i][1]]=='#'){
                continue;
            }
            if(que[head].x+move[i][0]>=0&&que[head].x+move[i][0]<h&&que[head].y+move[i][1]>=0&&que[head].y+move[i][1]<w){
                que[tail].x=que[head].x+move[i][0];
                que[tail].y=que[head].y+move[i][1];
                book[que[tail].x][que[tail].y]=1;
                count++;
                tail++;
            }
        }
        head++;
    }
    printf("%d\n",count);
    return 0;
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值