题目
迷宫
代码
// write your code here
import java.util.*;
public class Main{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
char[][] maze = new char[10][10];
int[][] map = new int[10][10];
for(int i = 0; i < 10; i++){
String str = sc.next();
for(int j = 0; j < 10; j++){
maze[i][j] = str.charAt(j);
map[i][j] = Integer.MAX_VALUE;
}
}
map[0][1] = 0;
dfs(0, 1, maze, map);
System.out.println(map[9][8]);
}
}
public static void dfs(int x,int y,char[][]maze,int [][]map){
int[][] direction = {{0,-1}, {-1,0}, {0,1}, {1,0}};
for(int i=0;i<4;i++){
int xx=x+direction[i][0];
int yy=y+direction[i][1];
if(xx >= 0 && xx < 10 && yy >= 0 && yy < 10
&& maze[xx][yy] == '.' && map[xx][yy] > map[x][y]+1){
map[xx][yy]=map[x][y]+1;
dfs(xx,yy,maze,map);
}
}
}
}