# BFS/DFS algorithm for bomb man problem

Posted by middleman666 on Sat, 02 May 2020 15:28:31 +0200

Problem Description: the villain can place a bomb anywhere in the maze. The bomb can kill the monster in the cross direction with the point as the center, but it can no longer deliver the attack after touching the wall. Where can I put a bomb to kill more monsters?? Input: 13 13
#############
#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#
#############
3 3

Output:
7 11 10
That is, 10 monsters can be killed at coordinates (7, 11)

Idea 1: traverse every point in the graph. If it is not a wall, the monster will calculate how many monsters it can kill, cycle through and find the biggest one (Note: Unfortunately, this method is not applicable to some special points, such as (1, 11) in the graph)

Train of thought 2: BFS/DFS, first screen out the reachable points, and then calculate

BFS solution

```import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class note {
int x;
int y;
note(int x, int y) {
this.x = x;
this.y = y;
}
}
public class BFS {
static char[][] a = new char;
static int[][] book = new int;
static Queue<note> queue = new LinkedList<>();
static Scanner input = new Scanner(System.in);
static int n, m;
static int max=0, mx, my;

public static void main(String[] args) {
/**
*  "#"For walls, "G" for monsters, "and". "For bomb locations
* */
n = input.nextInt();
m = input.nextInt();
for (int l = 0; l < n; l++) {
String str  = input.next();
a[l] = str.toCharArray();
}
int startX = input.nextInt();
int startY = input.nextInt();

queue.offer(new note(startX, startY));
max = getnum(startX, startY);
mx = startX;
my = startY;

bfs();

System.out.println(mx + " " + my + " " + max);
}

public static void bfs() {
int tx, ty, sum;
int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while (!queue.isEmpty()) {
for (int l = 0; l < 4; l++) {
tx = queue.peek().x + next[l];
ty = queue.peek().y + next[l];

if (tx < 0 || tx > n - 1 || ty < 0 || ty > m - 1) {
continue;
}
if (a[tx][ty] == '.' && book[tx][ty] == 0) {
book[tx][ty] = 1;
queue.offer(new note(tx, ty));
sum = getnum(tx, ty);
if (sum > max) {
max = sum;
mx = tx;
my = ty;
}
}
}
queue.remove();
}
}
/**
* Get the number of monsters that can be killed by placing bombs at a certain point
* */
public static int getnum(int i, int j) {
int sum, x, y;
sum = 0;

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

DFS

```package Bomberman;

import java.util.Scanner;

public class DFS {
static char[][] a = new char;
static int[][] book = new  int;
static Scanner input = new Scanner(System.in);
static int max = 0;
static int mx, my;
static int n, m;
public static void main(String[] args) {
n = input.nextInt();
m = input.nextInt();
for (int j = 0; j < n; j++) {
String str = input.next();
a[j] = str.toCharArray();
}
int startX = input.nextInt();
int startY = input.nextInt();
book[startX][startY] = 1;
max = getsum(startX, startY);
mx = startX;
my = startY;
dfs(startX, startY);

System.out.println(mx + " " + my + " " + max);
}
public static void dfs(int x, int y) {
/**
* Right, down, left, up
* */
int sum, tx, ty;
int[][] next = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
sum = getsum(x, y);
if (sum > max) {
max = sum;
mx = x;
my = y;
}
for (int i = 0; i < 4; i++) {
tx = x + next[i];
ty = y + next[i];
if (tx < 0 || tx > n - 1 || ty < 0 || ty > m - 1) {
continue;
}
if (a[tx][ty] == '.' && book[tx][ty] == 0) {
book[tx][ty] = 1;
dfs(tx, ty);
}
}
return;
}
public static int getsum(int i, int j) {
int x, y;
int sum = 0;
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;
}
}
```

Topics: Java