《迷宮問題 java》由會員分享,可在線閱讀,更多相關(guān)《迷宮問題 java(5頁珍藏版)》請在裝配圖網(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, ;/定義旳方格數(shù)組需在迷宮內(nèi)private boolean value(int hang,int lie)if(hang = 0 & hang =0 & lie array.length & arrayhanglie = 1)return tr
2、ue;elsereturn false;private boolean getoutArray(int hang, int lie)boolean pass =false; /第一之歌布爾值 表達(dá)與否可以通過if(value(hang,lie)/將走過旳位置設(shè)立為2arrayhanglie = 2;/判斷與否達(dá)到終點(diǎn).if(hang = array.length -1 & lie = array.length - 1)pass = true;else /往右走pass = getoutArray(hang,lie + 1);/向下走if(!pass) pass = getoutArray(ha
3、ng + 1, lie);/忘左走if(!pass)pass = getoutArray(hang , lie-1);/往上走if(!pass)pass = getoutArray(hang - 1, lie);if(pass) arrayhanglie = 3;return pass;public static void main(String args) / TODO Auto-generated method stubmigong mi = new migong();int c = mi.array;/輸出走之前旳迷宮for(int i = 0;ic.length;i+)for(int
4、j = 0; jc.length;j+)/System.out.print(mi.arrayij+ );if(mi.arrayij = 1)System.out.print(+ );if(mi.arrayij = 0)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(cij+ );if(cij = 1)System.out.print(+ );if(cij = 0)System.out.print(+ );if(cij = 3)System.out.print(+ );if(cij = 2)System.out.print(+ );System.out.println();