BFS DFS (2) 走迷宫 炸弹人

1.走迷宫

迷宫的行n和列m不超过50
广度优先搜索(Breadth First Search, BFS)
用队列模拟这个过程

#include<stdio.h>

struct note{
    int x;
    int y;
    int s;
};
int main(){
    struct note que[2501];
    int book[51][51]={0};
    int a[6][5] = {
    {0,0,0,0,0},
    {0,0,0,1,0},
    {0,0,0,0,0},
    {0,0,0,1,0},
    {0,0,1,0,0},
    {0,0,0,0,1}};
    int startx,starty,p,q;
    int head,tail,tx,ty,k;
    int flag = 0;
    int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
    //设置起点和终点
    do{
    printf("请输入起点和终点 如 1 1 4 3表示起点为(1,1),终点为(4,3):\n");
    scanf("%d %d %d %d",&startx,&starty,&p,&q);
    }while(startx < 1 || startx > 50 || starty < 1 || starty > 50 || p < 1 || p > 50 || q < 1 || q > 50);

    //初始化队列
    head = 1;
    tail = 1;
    que[tail].x = startx;
    que[tail].y = starty;
    que[tail].s = 0;
    book[startx][starty] = 1;
    tail ++;

    //BFS
    while(head < tail){
        for(k = 0; k<=3; k++){
            tx = que[tail].x + next[k][0];
            ty = que[tail].y + next[k][1];
            if(tx < 1 || tx > 50 || ty < 1 || ty > 50){
                continue;
            }
            if(a[tx][ty] == 0 && book[tx][ty] == 0){
                book[tx][ty] = 1;
                que[tail].x = tx;
                que[tail].y = ty;
                que[tail].s = que[head].s + 1; 
                tail++;
            }
            if(tx == p && ty == q){
                flag = 1;
                break;
            }

        }
        if(flag = 1){
            break;
        }
        head++;

    }
    printf("从起点到终点要用 %d 步。", que[tail-1].s);
    getchar();
    getchar();
    return 0;
}

2.炸弹人

找出哪些点是炸弹人可以到达的。可以用BFS或DFS来枚举出所有小人可以到达的点,然后在这些可以到达的点上分别统计可以消灭的敌人数。

2.0 某个位置消灭敌人个数


int get_num(int x, int y){
    int sum,i,j;
    i = x;
    j = y;
    //上
    while(a[i[j] != '#'){
        if(a[i][j] == 'G'){
            sum++;
        }
        x--;
    }
    //下
    i = x;
    j = y;
    while(a[i[j] != '#'){
        if(a[i][j] == 'G'){
            sum++;
        }
        x++;
    }
    //左
    i = x;
    j = y;
    while(a[i[j] != '#'){
        if(a[i][j] == 'G'){
            sum++;
        }
        y--;
    }
    //右
    i = x;
    j = y;
    while(a[i[j] != '#'){
        if(a[i][j] == 'G'){
            sum++;
        }
        y++;
    }
    return sum;
}

2.1 广度优先搜索

struct note{
    int x;
    int y;
};
char a[21][21];
int main(){
    int book[21][21];
    struct note que[401];
    int head,tail;
    int i,j,k,sum,maxNum = 0,maxX,maxY,n,m,startx,starty,tx,ty;
    int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //右下左上
    //地图初始化
    scanf("%d %d %d %d",&n,&m,&startx,&starty);
    for(i=1;i<=n;i++)
        scanf("%s",&a[i]);
    //队列初始化
    head = 1;
    tail = 1;
    que[tail].x = startx;
    que[tail].y = starty;
    tail++;
    book[startx][starty] = 1;
    maxNum = get_num(startx,starty);
    maxX = startx;
    maxY = starty;
    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 < 1 || tx > n || ty < 1 || ty > m) continue;
            if(a[tx][ty] == '.' && book[tx][ty] == 0){
                book[tx][ty] = 1;
                que[tail].x = tx;
                que[tail].y = ty;
                tail++;
                sum = get_num(tx,ty);
                if(sum > maxNum){
                    maxNum = sum;
                    maxX = tx;maxY = ty;
                }
            }
        }
        head++;
    }
    printf("将炸弹放置在(%d,%d)处,可以消灭%d个敌人。\n",maxX,maxY,maxNum);
    getchar();
    return 0;
}

2.2 深度优先搜索

void bomb_dfs(int x,int y){
    int sum,mx,my,k,tx,ty;
    book[x][y] = 1;
    sum = get_num(x,y);
    if(sum > maxNum){
        maxX = mx;
        maxY = my;
        maxNum = sum;
    }
    for(k = 0;k <= 3;k++){
        tx = x + next[k][0];
        ty = y + next[k][1];
        //判断越界
        if(tx < 1 || tx > n || ty < 1 || ty > m) continue;
        if(a[tx][ty] == '.' && book[tx][ty]==0){
            book[tx][ty] = 1;
            bomb_dfs(tx, ty);
        }
    }

    return ;

}
int main(){
    ...
    bomb_dfs(startx,starty);
    printf("将炸弹放置在(%d,%d)处,可以消灭%d个敌人。\n",maxX,maxY,maxNum);
    getchar();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值