BFS广度优先搜索 炸弹人

来源:互联网 发布:iphone7p淘宝店铺 编辑:程序博客网 时间:2024/05/21 10:19

题面:一个人在一个坐标放炸弹,请问可以可以杀死的敌人数目最大是,并且输出该点的坐标
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 <iostream>#include <cstring>#include <algorithm>#include <queue>using namespace std;char map[25][25];int mark[25][25];int next[4][2]= {0,1,1,0,0,-1,-1,0};struct node {    int gox;    int goy;    int step;};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 bfs(int n,int m,int startx,int starty) {    int max=0;    queue<node> que;    while(!que.empty()) que.pop();//当多组数据时    node one;    one.gox=startx;    one.goy=starty;    one.step=0;    mark[startx][starty]=1;    que.push(one);    node fir,ans;    while(!que.empty()) {        fir=que.front();        que.pop();        int Sum=getnum(fir.gox,fir.goy);        int tx,ty;        for(int i=0; i<4; i++) {            tx=fir.gox+next[i][0];            ty=fir.goy+next[i][1];            if(tx<0||tx>n-1||ty<0||ty>m-1)                continue;            if(mark[tx][ty]==0&&map[tx][ty]=='.') {                mark[tx][ty]=1;                node cmp;                cmp.gox=tx;                cmp.goy=ty;                cmp.step=fir.step+1;                que.push(cmp);            }        }        if(Sum>max)  {            ans.gox=fir.gox;            ans.goy=fir.goy;            ans.step=fir.step+1;            max=Sum;        }    }    printf("%d %d %d",ans.gox,ans.goy,max);}int main() {    freopen("input6.txt","r",stdin);    int startx,starty,n,m;    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];        }    }    bfs(n,m,startx,starty);    return 0;}
1 0
原创粉丝点击