样例输入1
8 8
.@##…#
#…#.#
#.#.##…
…#.###.
#.#…#.
…###.#.
…#.*…
.#…###
样例输出1
10
样例输入2
9 6
.#…#.
.#.*.#
.####.
…#…
…#…
…#…
…#…
#.@.##
.#…#.
样例输出2
-1
dfs
ps:
使用dfs会运行超时,30组测试数据只能通过部分,其实这种最短路径、最少操作的问题最好还是靠bfs解决。
import java.util.Scanner;
//最短路径
public class Main{
static int min=9999; //步数
static int []dx = {
-1,0,0,1};
static int []dy = {
0,-1,1,0};
static boolean flag = false;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //n行
int m = sc.nextInt(); //m列
sc.nextLine();
char [][]table = new char[n][];
int [][]vis = new int[n][m]; //标记是否已访问
for(int i=0;i<n;i++)
table[i] = sc.nextLine().toCharArray();
for(int i=0