/*
@author: fangweijie;
@date: 2021-08-02;
@title: mouse pathfinding
@note:
"1"means we can go and "0" means we can't
"[1][1]" means the stating point ,"[6][5]" means the end point
"2"means we passed the unit
*/
//maze
public class Maze{
public static void main(String[] args) {
/*
create maze (use dim array),
*/
int mazeMap[][] = { {0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0}};
Menthod M = new Menthod();
boolean b = M.findWay(mazeMap, 1, 1);
//output array
for(int i = 0; i <= 7; i++){
for(int j = 0; j <= 6; j++){
System.out.print(mazeMap[i][j] + "\t");
}
System.out.print("\n");
}
}
}
class Menthod{
public boolean findWay(int mazeMap[][], int i, int j){
if(mazeMap[6][5] == 2){
return true;
}else{
if(mazeMap[i][j] == 1){
mazeMap[i][j] = 2;
//Pouting firections have different priorities:
//down->right->up->left \ up->right->down->left
if(findWay(mazeMap, i+1, j)){
return true;
}else if(findWay(mazeMap, i, j+1)){
return true;
}else if(findWay(mazeMap, i-1, j)){
return true;
}else if(findWay(mazeMap, i, j-1)){
return true;
}else{
mazeMap[i][j] = 3;
return false;
}
}else{
return false;
}
}
}
}
java-递归寻路
最新推荐文章于 2024-10-18 13:02:57 发布