在前面的二维数组中,# 代表迷宫的墙壁,圆点代表穿过迷宫的可能路径中的正方形。只能移动到数组中包含点的位置......我需要一个递归方法 mazeTraverse 来“行走”迷宫。它应该接收数组和迷宫的起始位置作为参数。当它试图定位迷宫的出口时,它应该在路径的每个方格中放置字符“X”。该方法应在每次移动后显示迷宫,以便用户可以观看迷宫已解决。
我不确定如何超越这一点。感谢任何可以提供帮助的人。
public class BonusMaze {
public static void main(String args[]){
char field[][]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','•','•','•','#','•','•','•','•','•','•','#',},
{'•','•','#','•','#','•','#','#','#','#','•','#',},
{'#','#','#','•','#','•','•','•','•','#','•','#',},
{'#','•','•','•','•','#','#','#','•','#','•','•',},
{'#','#','#','#','•','#','•','#','•','#','•','#',},
{'#','•','•','#','•','#','•','#','•','#','•','#',},
{'#','#','•','#','•','#','•','#','•','#','•','#',},
{'#','•','•','•','•','•','•','•','•','#','•','#',},
{'#','#','#','#','#','#','•','#','#','#','•','#',},
{'#','•','•','•','•','•','•','#','•','•','•','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#'},
};
printField(field);
mazeTraverse(field,2,0);
}
public static void mazeTraverse(char[][] field, int x, int y){
field[2][0]='X';
if(field[x+1][y]=='•'){
field[x+1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y]=='•'){
field[x-1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='•'){
field[x][y+1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y-1]=='•'){
field[x][y-1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
}
public static void printField(char[][] field){
for(int x=0; x<11; x++){
for(int y=0; y<11; y++){
System.out.print(field[x][y]);
}
System.out.println();
}
System.out.print("\n\n");
}
}
我已经把它改成了这个,但这一切都很古怪:
public class BonusMaze {
public static boolean east=true, north=false, south=false, west=false;
/*
east=false;
north=false;
south=false;
west=false;
*/
public static void main(String args[]){
char field[][]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','•','•','•','#','•','•','•','•','•','•','#',},
{'•','•','#','•','#','•','#','#','#','#','•','#',},
{'#','#','#','•','#','•','•','•','•','#','•','#',},
{'#','•','•','•','•','#','#','#','•','#','•','•',},
{'#','#','#','#','•','#','•','#','•','#','•','#',},
{'#','•','•','#','•','#','•','#','•','#','•','#',},
{'#','#','•','#','•','#','•','#','•','#','•','#',},
{'#','•','•','•','•','•','•','•','•','#','•','#',},
{'#','#','#','#','#','#','•','#','#','#','•','#',},
{'#','•','•','•','•','•','•','#','•','•','•','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#'},
};
printField(field);
field[2][0]='X';
mazeTraverse(field,2,0);
}
public static void mazeTraverse(char[][] field, int x, int y){
if(x==2&&y==0){
field[2][1]='X';
mazeTraverse(field,2,1);
printField(field);
}
if(east){
if(field[x][y+1]=='•' || field[x+1][y]=='X'){
field[x][y+1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(west){
if(field[x-1][y]=='•' || field[x-1][y]=='X'){
field[x-1][y]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(north){
if(field[x][y+1]=='•' || field[x][y+1]=='X'){
field[x][y+1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(south){
if(field[x][y-1]=='•' || field[x][y-1]=='X'){
field[x][y-1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
/*if(field[x+1][y]=='•'){
field[x+1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y]=='•'){
field[x-1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='•'){
field[x][y+1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y-1]=='•'){
field[x][y-1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}*/
}
public static void printField(char[][] field){
for(int x=0; x<12; x++){
for(int y=0; y<12; y++){
System.out.print(field[x][y]);
}
System.out.println();
}
System.out.print("\n\n");
}
}