| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 882 人关注过本帖
标题:【求助】刚试着做的迷宫求解...却不懂错在哪里
只看楼主 加入收藏
howarezhao
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2006-12-9
收藏
 问题点数:0 回复次数:0 
【求助】刚试着做的迷宫求解...却不懂错在哪里
我的想迷宫以二维数组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]]
搜索更多相关主题的帖子: 迷宫 Node int type public 
2008-04-02 00:55
快速回复:【求助】刚试着做的迷宫求解...却不懂错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036457 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved