howarezhao 发表于 2008-4-2 00:55

【求助】刚试着做的迷宫求解...却不懂错在哪里

我的想迷宫以二维数组sqrs[5][5]表示,0为可通行的,1为不通行的,并且通行过后以2作为标记。入口设定为数组sqrs[1][0],出口设定为sqrs[5][1],用nextfoot来作为前进的方法,用Node_type类step作为通行道具,却发现不能通行...请高手们帮我看看啊...
package mazetest;
class Node_type{
            public  int item;
            public  Node_type next;
            int x;
            int y;
            Node_type(int x,int y){
                    this.x=x;
                    this.y=y;
            }
}

public class Maze {
          Node_type top=null;
          int i,j;
          public static int sqrs[][]={{1,0,1,1,1,1},
                                        {1,0,0,1,1,1},
                                        {1,1,0,0,1,1},
                                        {1,0,0,0,1,1},
                                        {1,3,1,1,1,1}
                                                     };
                                    
     /**
         * @param args
         */
        public void push_stack(Node_type obj){
                  Node_type prt=obj;
                  
                  prt.next=top;
                  top=prt;
                                           //入栈Node_type类
        }
        public Node_type pop_stack(){
                 Node_type clear=null;
                 if(top==null)
                         System.out.println("stack is emtry");
                 else
                         clear=top;
                     top=top.next;
                     return clear;        //出栈Node_type类
                     
                         
        }
        public Node_type set(int x,int y){
                Node_type obj=new Node_type(x,y);
                obj.item=sqrs[x][y];
                return obj;
        }
        boolean end(int x,int y){
                if(x!=5&&y!=1)
                   return true;
                   else
                    return false;
        }
        public void nextfoot(){
                 
                   int x,y;
                   x=0;
                   y=1;
                   Node_type step=set(0,1);
                  
                   while(end(step.x,step.y)){
                          
                   if(step.y-1>=0){
                           if(sqrs[step.x][step.y-1]==0||sqrs[step.x][step.y-1]==3){
                                   push_stack(step);
                                   step.y--;
                               step=set(step.x,step.y);
                               sqrs[step.x][step.y]=2;}
                   }
                               if(step.y+1<=5){
                                       if(sqrs[step.x][step.y+1]==0||sqrs[step.x][step.y-1]==3){
                              push_stack(step);
                               step.y++;
                               step=set(step.x,step.y);
                               sqrs[step.x][step.y]=2;}
                          }
                               if(step.x+1<=5){
                           if(sqrs[step.x+1][step.y]==0||sqrs[step.x][step.y-1]==3){
                                  push_stack(step);
                                   step.x++;
                           step=set(step.x,step.y);
                           sqrs[step.x][step.y]=2;}
                   }
                   else
                   step.equals(pop_stack());
                           } ;
                   //确定下一步要走的位置,先判定左右,若不超出边界和可以通行并没有通行过,则前进,否则后退,并把0标记为2;
                       
                       
                        /*if(step.y-1>=0)
                                System.out.print(step.x+" "+step.y);*/
                        //sqrs[step.x][step.y]=2;
                           /*while(x!=5){
                                   System.out.print(x+" ");
                                   x++;
                                  
                           }*/
                                     
                        for(int i=0;i<5;i++){
                                for(int j=0;j<5;j++)
                                        System.out.print(sqrs[i][j]);
                                          
                                        System.out.print("\n");
                                        }
                  
        }

        public static void main(String[] args) {
                  
                // TODO 自动生成方法存根
                Maze obj=new Maze();
                obj.nextfoot();
               

    }
}

[[it] 本帖最后由 howarezhao 于 2008-4-2 11:46 编辑 [/it]]


页: [1]

编程论坛