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
- BFS广度优先搜索 炸弹人
- 炸弹人(广度优先搜索)
- 广度优先搜索-炸弹人问题
- BFS深度优先搜索 炸弹人
- 广度优先搜索-BFS
- BFS广度优先搜索
- 广度优先搜索 BFS
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- BFS (广度优先搜索)
- BFS广度优先搜索
- 广度优先搜索(BFS)
- bfs 广度优先搜索
- bfs广度优先搜索
- 广度优先搜索bfs
- BFS广度优先搜索
- 广度优先搜索(BFS)
- BFS深度优先搜索 炸弹人
- Leetcode-标签为Tree 226. Invert Binary Tree
- win7用虚机搭建docker开发测试环境的网络配置,免去端口映射烦恼
- Java基础:访问限制修饰符
- The import android.support cannot be resolved解决方法
- BFS广度优先搜索 炸弹人
- Unit --面试常问题一
- CAE开发日志(3):Condition类
- Spark 开发环境搭建(2)IDEA 安装新版本IntelliJ IDEA 2017.1
- 浅谈守护进程
- maven下载jar包过慢
- opencv mat数据剪裁感兴趣的部分处理方法
- LED闪烁
- 创业,合伙人很重要