您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > linux/Unix相关 > 迷宫问题 java
package迷宫;publicclassmigong{publicintarray[][]={{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},};//定义的方格数组需在迷宫内privatebooleanvalue(inthang,intlie){if(hang=0&&hangarray.length&&lie=0&&liearray.length&&array[hang][lie]==1)returntrue;elsereturnfalse;}privatebooleangetoutArray(inthang,intlie){booleanpass=false;//第一之歌布尔值表示是否可以通过if(value(hang,lie)){//将走过的位置设置为2array[hang][lie]=2;//判断是否到达终点.if(hang==array.length-1&&lie==array.length-1){pass=true;}else{//往右走pass=getoutArray(hang,lie+1);//向下走if(!pass)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;}returnpass;}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubmigongmi=newmigong();int[][]c=mi.array;//输出走之前的迷宫for(inti=0;ic.length;i++){for(intj=0;jc.length;j++){//System.out.print(mi.array[i][j]+);if(mi.array[i][j]==1)System.out.print(□+);if(mi.array[i][j]==0)System.out.print(■+);}System.out.println();}System.out.println(mi.getoutArray(0,0));//输出走过之后的迷宫for(inti=0;i=c.length-1;i++){for(intj=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();}}}
本文标题:迷宫问题 java
链接地址:https://www.777doc.com/doc-7028046 .html