package com.xjj.Ah;
import java.util.LinkedList;
/*---枚举 + 广度搜索 ----炸弹人----
* 1. 炸弹能到达的地方最多能炸死的敌人数
* 2. 用枚举法来获得每个空格放置炸弹能炸死的敌人数,用广度搜索法来获得能达到的地方
* 3. 用一个队列来保存能达到的地方,入队出队来依次深入...,book[][] 标记是否已经入队
*
*
* */
public class Ah_4_4 {
static int N = 13;
static int M = 13;
class Dis{
int x;
int y;
}
//‘#’为墙,‘H’为空格,‘G’为敌人
static char[][] a = {{'#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','G','G','H','G','G','G','#','G','G','G','H','#'},
{'#','#','#','H','#','G','#','G','#','G','#','G','#'},
{'#','H','H','H','H','H','H','H','#','H','H','G','#'},
{'#','G','#','H','#','#','#','H','#','G','#','G','#'},
{'#','G','G','H','G','G','G','H','#','H','G','G','#'},
{'#','G','#','H','#','G','#','H','#','H','#','H','#'},
{'#','#','G','H','H','H','G','H','H','H','H','H','#'},
{'#','G','#','H','#','G','#','#','#','H','#','G','#'},
{'#','H','H','H','G','#','G','G','G','H','G','G','#'},
{'#','G','#','H','#','G','#','G','#','H','#','G','#'},
{'#','G','G','H','G','G','G','#','G','H','G','G','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#'},
};
//返回该点可炸死敌人的数目,枚举法
public int getNum(int i, int j){
int sum=0,x,y;
//向上统计
x = i;
y = j;
while(a[x][y] != '#'){
if (a[x][y] == 'G')
sum++;
x--;
}
//向下统计
x = i;
y = j;
while(a[x][y] != '#'){
if (a[x][y] == 'G')
sum++;
x++;
}
//向左统计
x = i;
y = j;
while(a[x][y] != '#'){
if (a[x][y] == 'G')
sum++;
y--;
}
//向右统计
x = i;
y = j;
while(a[x][y] != '#'){
if (a[x][y] == 'G')
sum++;
y++;
}
return sum;
}
//广度搜索法
public void bfs(int startx, int starty){
int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}}; //方向矩阵-右下左上
int[][] book = new int[N][M]; //标记是否入队
int max = 0,sum,X = 0,Y = 0;
Dis d = new Dis(); //新的坐标入队
d.x = startx;
d.y = starty;
LinkedList list = new LinkedList<>();
list.add(d);
book[startx][starty] = 1; //标记入队
//直到队列为空
while(!list.isEmpty()){
//获得头结点能炸死的敌人数
sum = getNum(list.getFirst().x, list.getFirst().y);
//更新
if (sum > max) {
max = sum;
X = list.getFirst().x;
Y = list.getFirst().y;
}
//枚举四个方向
for(int k = 0; k <= 3; k++){
//先向右移动
int x = list.getFirst().x + dir[k][0];
int y = list.getFirst().y + dir[k][1];
//判断是否出界
if (x > N-1 || x < 1 || y > M-1 || y < 1) {
continue;
}
//为空格且未入队
if (a[x][y] == 'H' && book[x][y] == 0) {
d = new Dis();
d.x = x;
d.y = y;
list.add(d);
book[x][y] = 1;
}
}
list.removeFirst();//千万不能忘记,移除头结点才能往下扩展
}
System.out.println("坐标为: " + X + " " + Y);
System.out.println("最大敌人为: " + max);
}
public static void main(String[] args) {
Ah_4_4 ah = new Ah_4_4();
ah.bfs(3, 3);
}
}