1. 递归思路
基本情况 :当前位置 为 终点,设置必要的数字(用于显示),返回true向基本情况靠近 :上下左右移动,即改变当前位置坐标调用自身 :将新坐标作为本方法的参数
2. 代码实现
public class Maze {
int [ ] [ ] mazeMap = new int [ 11 ] [ 22 ] ;
final int [ ] startPosition = new int [ ] { 8 , 15 } ;
final int [ ] endPosition = new int [ ] { 1 , 21 } ;
public Maze ( ) {
this . generateMazeMap ( ) ;
this . mazeMap[ this . startPosition[ 0 ] ] [ this . startPosition[ 1 ] ] = 8 ;
}
private void generateMazeMap ( ) {
this . mazeMap[ this . startPosition[ 0 ] ] [ this . startPosition[ 1 ] ] = 7 ;
this . mazeMap[ this . endPosition[ 0 ] ] [ this . endPosition[ 1 ] ] = 9 ;
for ( int i = 0 ; i < 22 ; i++ ) {
this . mazeMap[ 0 ] [ i] = 1 ;
}
this . mazeMap[ 1 ] [ 0 ] = 1 ;
this . mazeMap[ 1 ] [ 4 ] = 1 ;
this . mazeMap[ 1 ] [ 8 ] = 1 ;
this . mazeMap[ 1 ] [ 9 ] = 1 ;
this . mazeMap[ 1 ] [ 11 ] = 1 ;
this . mazeMap[ 1 ] [ 12 ] = 1 ;
this . mazeMap[ 1 ] [ 18 ] = 1 ;
this . mazeMap[ 2 ] [ 0 ] = 1 ;
this . mazeMap[ 2 ] [ 2 ] = 1 ;
this . mazeMap[ 2 ] [ 6 ] = 1 ;
this . mazeMap[ 2 ] [ 14 ] = 1 ;
this . mazeMap[ 2 ] [ 15 ] = 1 ;
this . mazeMap[ 2 ] [ 16 ] = 1 ;
this . mazeMap[ 2 ] [ 18 ] = 1 ;
this . mazeMap[ 2 ] [ 20 ] = 1 ;
this . mazeMap[ 2 ] [ 21 ] = 1 ;
this . mazeMap[ 3 ] [ 0 ] = 1 ;
this . mazeMap[ 3 ] [ 2 ] = 1 ;
this . mazeMap[ 3 ] [ 4 ] = 1 ;
this . mazeMap[ 3 ] [ 7 ] = 1 ;
this . mazeMap[ 3 ] [ 8 ] = 1 ;
this . mazeMap[ 3 ] [ 11 ] = 1 ;
this . mazeMap[ 3 ] [ 12 ] = 1 ;
this . mazeMap[ 3 ] [ 13 ] = 1 ;
this . mazeMap[ 3 ] [ 14 ] = 1 ;
this . mazeMap[ 3 ] [ 18 ] = 1 ;
this . mazeMap[ 3 ] [ 20 ] = 1 ;
this . mazeMap[ 3 ] [ 21 ] = 1 ;
for ( int i = 0 ; i < 10 ; i++ ) {
this . mazeMap[ 4 ] [ i] = 1 ;
}
for ( int i = 14 ; i < 17 ; i++ ) {
this . mazeMap[ 4 ] [ i] = 1 ;
}
this . mazeMap[ 4 ] [ 18 ] = 1 ;
this . mazeMap[ 4 ] [ 21 ] = 1 ;
this . mazeMap[ 5 ] [ 0 ] = 1 ;
this . mazeMap[ 5 ] [ 11 ] = 1 ;
this . mazeMap[ 5 ] [ 12 ] = 1 ;
this . mazeMap[ 5 ] [ 15 ] = 1 ;
this . mazeMap[ 5 ] [ 16 ] = 1 ;
this . mazeMap[ 5 ] [ 21 ] = 1 ;
for ( int i = 0 ; i < 5 ; i++ ) {
this . mazeMap[ 6 ] [ i] = 1 ;
}
for ( int i = 6 ; i < 6 + 6 ; i++ ) {
this . mazeMap[ 6 ] [ i] = 1 ;
}
for ( int i = 15 ; i < 15 + 5 ; i++ ) {
this . mazeMap[ 6 ] [ i] = 1 ;
}
this . mazeMap[ 6 ] [ 21 ] = 1 ;
this . mazeMap[ 7 ] [ 0 ] = 1 ;
this . mazeMap[ 7 ] [ 6 ] = 1 ;
for ( int i = 10 ; i < 10 + 7 ; i++ ) {
this . mazeMap[ 7 ] [ i] = 1 ;
}
this . mazeMap[ 7 ] [ 19 ] = 1 ;
this . mazeMap[ 7 ] [ 21 ] = 1 ;
this . mazeMap[ 8 ] [ 0 ] = 1 ;
for ( int i = 2 ; i < 9 ; i++ ) {
this . mazeMap[ 8 ] [ i] = 1 ;
}
this . mazeMap[ 8 ] [ 17 ] = 1 ;
this . mazeMap[ 8 ] [ 21 ] = 1 ;
this . mazeMap[ 9 ] [ 0 ] = 1 ;
this . mazeMap[ 9 ] [ 17 ] = 1 ;
this . mazeMap[ 9 ] [ 19 ] = 1 ;
this . mazeMap[ 9 ] [ 20 ] = 1 ;
this . mazeMap[ 9 ] [ 21 ] = 1 ;
for ( int i = 0 ; i < 22 ; i++ ) {
this . mazeMap[ 10 ] [ i] = 1 ;
}
}
public void printMaze ( ) {
for ( int line = 0 ; line < mazeMap. length; line++ ) {
for ( int column = 0 ; column < mazeMap[ 0 ] . length; column++ ) {
if ( this . mazeMap[ line] [ column] == 0 ) {
System. out. printf ( " " + "\t" ) ;
} else if ( this . mazeMap[ line] [ column] == 1 ) {
System. out. printf ( "♿️" + "\t" ) ;
} else if ( this . mazeMap[ line] [ column] == 2 ) {
System. out. printf ( "❌" + "\t" ) ;
} else if ( this . mazeMap[ line] [ column] == 7 ) {
System. out. printf ( "🏀" + "\t" ) ;
} else if ( this . mazeMap[ line] [ column] == 9 ) {
System. out. printf ( "🏁" + "\t" ) ;
} else if ( this . mazeMap[ line] [ column] == 3 ) {
System. out. printf ( "🍀" + "\t" ) ;
} else {
throw new RuntimeException ( "map error" ) ;
}
}
System. out. println ( ) ;
}
}
public boolean hasSolution ( int line, int column) {
if ( this . mazeMap[ line] [ column] == 8 ) {
this . mazeMap[ line] [ column] = 0 ;
}
if ( this . mazeMap[ line] [ column] == 0 ) {
this . mazeMap[ line] [ column] = 3 ;
if ( hasSolution ( line + 1 , column) ) {
return true ;
}
else if ( hasSolution ( line, column + 1 ) ) {
return true ;
}
else if ( hasSolution ( line - 1 , column) ) {
return true ;
}
else if ( hasSolution ( line, column - 1 ) ) {
return true ;
}
else {
this . mazeMap[ line] [ column] = 2 ;
return false ;
}
}
else if ( this . mazeMap[ line] [ column] == 9 ) {
this . mazeMap[ line] [ column] = 3 ;
this . mazeMap[ this . startPosition[ 0 ] ] [ this . startPosition[ 1 ] ] = 7 ;
this . mazeMap[ this . endPosition[ 0 ] ] [ this . endPosition[ 1 ] ] = 9 ;
return true ;
}
else {
return false ;
}
}
}
3. 探索结果图示