BFS深度优先搜索 炸弹人

本文介绍了一个寻找最佳炸弹放置位置的算法实现,通过遍历地图来确定能够消灭最多敌人的坐标点,采用DFS深度优先搜索算法并结合障碍物判断,最终输出坐标及最大击杀数。

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

题面:一个人在一个坐标放炸弹,请问可以可以杀死的敌人数目最大是,并且输出该点的坐标
G代表敌人
.代表该位置可以走
“#”代表该位置存在障碍物 并且防止炸弹的蔓13 13 3 3
.#############
.#GG.GGG#GGG.#
.###.#G#G#G#G#
.#…….#..G#
.#G#.###.#G#G#
.#GG.GGG.#.GG#
.#G#.#G#.#.#.#
.##G…G…..#
.#G#.#G###.#G#
.#…G#GGG.GG#
.#G#.#G#G#.#G#
.#GG.GGG#G.GG#
.#############

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
char map[25][25];
int mark[25][25];
int startx,starty,n,m;
int next[4][2]= {0,1,1,0,0,-1,-1,0}; 

int Max=0;
struct node {
    int gox;
    int goy;
    int step;
};
node cmp;
int getnum(int x,int y) {
    int tx,ty,sum=0;
    tx=x;
    ty=y;
    while(map[tx][ty]!='#') {
        if(map[tx][ty]=='G') sum++;
        tx--;
    }
    tx=x;
    ty=y;
    while(map[tx][ty]!='#') {
        if(map[tx][ty]=='G') sum++;
        tx++;
    }
    tx=x;
    ty=y;
    while(map[tx][ty]!='#') {
        if(map[tx][ty]=='G') sum++;
        ty--;
    }
    tx=x;
    ty=y;
    while(map[tx][ty]!='#') {
        if(map[tx][ty]=='G') sum++;
        ty++;
    }
    return sum;
}
void dfs(int curx,int cury) {


     mark[curx][cury]=1; 
     int ans=getnum(curx,cury);
     if(ans>Max) {
        cmp.gox=curx;
        cmp.goy=cury;
        Max=ans;
     }
     int tx,ty;
     for(int i=0;i<4;i++) {
        tx=curx+next[i][0];
        ty=cury+next[i][1];
        if(curx<0||curx>n-1||cury<0||cury>m-1)
            continue;
        if(mark[tx][ty]==0&&map[tx][ty]=='.') {         
            dfs(tx,ty);
            mark[tx][ty]=0;//恢复状态 
         }
     }
}
int main() {
//  freopen("input6.txt","r",stdin);

    scanf("%d%d%d%d",&n,&m,&startx,&starty);
    for(int i=0;i<n;i++) {
        for(int j=0;j<m;j++) {
            cin>>map[i][j];
        }
    }
    dfs(startx,starty);
    printf("%d %d %d",cmp.gox,cmp.goy,Max);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值