《迷宮問題 java》由會員分享,可在線閱讀,更多相關(guān)《迷宮問題 java(5頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。
1、package 迷宮;
public class migong {
public int array[][] ={
{1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,0,1},
{0,0,1,0,1,0,0,0},
{1,1,1,0,1,0,0,1},
{1,1,0,1,1,1,1,1},
{0,0,0,1,0,0,0,0},
{0,0,1,1,1,1,1,1},
2、 };
//定義旳方格數(shù)組需在迷宮內(nèi)
private boolean value(int hang,int lie){
if(hang >= 0 && hang < array.length &&
lie >=0 && lie< array.length && array[hang][lie] == 1)
return true;
else
return false;
}
private boolean getoutArray(int hang, int lie){
b
3、oolean pass =false; //第一之歌布爾值 表達(dá)與否可以通過
if(value(hang,lie)){
//將走過旳位置設(shè)立為2
array[hang][lie] = 2;
//判斷與否達(dá)到終點(diǎn).
if(hang == array.length -1 && lie == array.length - 1){
pass = true;
}
else{ //往右走
pass = getoutArray(hang,lie + 1);
//向下走
if(!pass)
4、
pass = getoutArray(hang + 1, lie);
//忘左走
if(!pass)
pass = getoutArray(hang , lie-1);
//往上走
if(!pass)
pass = getoutArray(hang - 1, lie);
}
if(pass) array[hang][lie] = 3;
}
return pass;
}
public static void main(String[] args) {
// TODO Auto-genera
5、ted method stub
migong mi = new migong();
int[][] c = mi.array;
//輸出走之前旳迷宮
for(int i = 0;i
6、System.out.print("■"+" ");
}
System.out.println();
}
System.out.println(mi.getoutArray(0,0));
//輸出走過之后旳迷宮
for(int i = 0 ;i<= c.length-1;i++){
for(int j = 0; j<= c.length-1;j++){
//System.out.print(c[i][j]+" ");
if(c[i][j] == 1)
System.out.print("□"+" ");
if(c[i][j] == 0)
System.out.print("■"+" ");
if(c[i][j] == 3)
System.out.print("○"+" ");
if(c[i][j] == 2)
System.out.print("●"+" ");
}
System.out.println();
}
}
}