BFS深度优先搜索 炸弹人

时间:2025-02-28 23:37:38

题面:一个人在一个坐标放炸弹,请问可以可以杀死的敌人数目最大是,并且输出该点的坐标

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;
}