题目出处点这里
很明显又是广搜模板题
代码:
package search;
import java.awt.Point;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class P2298 {
static char[][] arr;
static int n, m, x, y;
static int[] xx = { 0, 0, 1, -1 };
static int[] yy = { 1, -1, 0, 0 };
static boolean[][] vis = new boolean[2005][2005];
static Queue<Point> p = new LinkedList<Point>();
static Queue<Integer> s = new LinkedList<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
arr = new char[n][m];
for (int i = 0; i < n; i++) {
String temp = sc.next();
if (temp.indexOf("m") != -1) {
x = i;
y = temp.indexOf("m");
}
arr[i] = temp.toCharArray();
}
bfs();
}
public static void bfs() {
p.add(new Point(x, y));
s.add(0);
vis[x][y] = true;
while (!p.isEmpty()) {
Point pp = p.poll();
int ss = s.poll();
for (int i = 0; i < 4; i++) {
int row = pp.x + xx[i], col = pp.y + yy[i];
if (row >= 0 && row < n && col >= 0 && col < m && !vis[row][col] &&arr[row][col] != '#') {
p.add(new Point(row, col));
s.add(ss + 1);
if (arr[row][col] == 'd') {
System.out.println(ss+1);
return;
}
vis[row][col] = true;
}
}
}
System.out.println("No Way!");
}
}