首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在java中寻找迷宫的解决方案

在java中寻找迷宫的解决方案
EN

Stack Overflow用户
提问于 2014-09-19 10:56:53
回答 1查看 340关注 0票数 0

我有一个项目,在我随机生成的迷宫的第一个盒子里有一个红点,这个点应该沿着它的路线穿过迷宫,找到迷宫的尽头。现在,如果它走进了死胡同,它应该回到它开始的地方,而不是回到那条道路上,这会导致死胡同。我这样做是为了让每个盒子代表#1,这样当红点经过盒子时,它会递增1,这样它就可以意识到它在哪里。它总是被认为是尽可能低的数字,所以它永远不会回到它已经去过的死胡同。我能够到达迷宫的尽头,但我遇到了两个问题。

我编写的完成所有这些工作的方法是solve()函数。我不明白为什么会发生两件事。第一件事是,当红点到达一个死胡同的分支时,有时它只会到一个死胡同,到另一个死胡同,回到相同的死胡同。移动到相同的“数字”,当我试图让它只去有1的盒子或只有较低的数字。第二件事是,一旦它不可避免地到达迷宫的尽头..红点进入绿色区域,我特别指出,在while循环中,它不能在绿色框中。

如果My = 0,它是一个绿色的盒子,如果它=1,它是一个黑盒子。任何高于1的值也将位于框内。

非常感谢你的帮助,因为我已经被这个问题困扰了几个小时,似乎找不到问题所在。

这个问题仍然存在于solve()方法中

代码语言:javascript
运行
复制
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.*;

public class mazedfs extends JFrame implements KeyListener
{
/* default values: */
private static int bh = 16;     // height of a graphical block
private static int bw = 16;    // width of a graphical block
private int mh = 41;    // height and width of maze
private int mw = 51;
private int ah, aw;    // height and width of graphical maze
private int yoff = 40;    // init y-cord of maze
private Graphics g;
private int dtime = 40;   // 40 ms delay time
byte[][] M;    // the array for the maze
public static final int SOUTH = 0;
public static final int EAST = 1;
public static final int NORTH = 2;
public static final int WEST = 3;

public static boolean showvalue = true; // affects drawblock

// args determine block size, maze height, and maze width
public mazedfs(int bh0, int mh0, int mw0)
 { 
   bh = bw = bh0;  mh = mh0;  mw = mw0;
   ah = bh*mh;
   aw = bw*mw;
   M = new byte[mh][mw];  // initialize maze (all  0's - walls).
   this.setBounds(0,0,aw+10,10+ah+yoff);    
   this.setVisible(true);
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   try{Thread.sleep(500);} catch(Exception e) {} // Synch with system
   this.addKeyListener(this);  
   g = getGraphics();    //g.setColor(Color.red);
   setup();
 }

public void paint(Graphics g) {} // override automatic repaint

public void setup()
   { 
     g.setColor(Color.green);
     g.fill3DRect(0,yoff,aw,ah,true);  // fill raised rectangle
     g.setColor(Color.black);
     //     showStatus("Generating maze...");
     digout(mh-2,mw-2); // start digging!
     // digout exit
     M[mh-1][mw-2] = M[mh-2][mw-1] = 1;
     drawblock(mh-2,mw-1);
     solve();  // this is the function you will write for parts 1 and 2
     play();   // for part 3
   }   

    public static void main(String[] args)
    {
       int blocksize = bh, mheight = 41, mwidth = 41; // need to be odd
       if (args.length==3)
       {
           mheight=Integer.parseInt(args[0]);
           mwidth=Integer.parseInt(args[1]);
           blocksize=Integer.parseInt(args[2]);
       }
       mazedfs W = new mazedfs(blocksize,mheight,mwidth);
    }

public void drawblock(int y, int x)
    {
    g.setColor(Color.black);
    g.fillRect(x*bw,yoff+(y*bh),bw,bh);
    g.setColor(Color.yellow);
    // following line displays value of M[y][x] in the graphical maze:
    if (showvalue)
      g.drawString(""+M[y][x],(x*bw)+(bw/2-4),yoff+(y*bh)+(bh/2+6));
    }

    void drawdot(int y, int x)
    {
    g.setColor(Color.red);
    g.fillOval(x*bw,yoff+(y*bh),bw,bh);               
        try{Thread.sleep(dtime);} catch(Exception e) {} 
    }

    /////////////////////////////////////////////////////////////////////

/* function to generate random maze */
public void digout(int y, int x)
 {
     M[y][x] = 1;  // digout maze at coordinate y,x
     drawblock(y,x);  // change graphical display to reflect space dug out


     int dir = (int)(Math.random()*4);

     for (int i=0;i<4;i++){
     int [] DX = {0,0,2,-2};
     int [] DY = {-2,2,0,0};
     int newx = x + DX[dir];
     int newy = y + DY[dir];
     if(newx>=0 && newx<mw && newy>=0 && newy<mh && M[newy][newx]==0)
         {
         M[y+DY[dir]/2][x+DX[dir]/2] = 1;
         drawblock(y+DY[dir]/2,x+DX[dir]/2);
         digout(newy,newx);
         }
     dir = (dir + 1)%4;}
 } // digout


  public  void solve()  // This is the method i need help with. 
  {
    int x=1, y=1;
    drawdot(y,x);
    while(y!=mh-1 || x!=mw-1 && M[y][x]!=0){
          int min = 0x7fffffff;
          int  DX = 0;
          int  DY = 0;
        if (y-1>0 && min>M[y-1][x] && M[y-1][x]!=0){
            min = M[y-1][x];
            DX = 0;
            DY = -1;
        }//ifNORTH
        if (y+1>0 && min>M[y+1][x] && M[y+1][x]!=0){
            min = M[y+1][x];
            DY = 1;
            DX = 0;
        }//ifSOUTH
        if (x-1>0 && min>M[y][x-1] && M[y][x-1]!=0){
            min = M[y][x-1];
            DX = -1;
            DY = 0;
        }//ifWEST
        if (x+1>0 && min>M[y][x+1] && M[y][x+1]!=0){
            min = M[y][x+1];
          DX = 1;
          DY = 0;
        }//ifEAST

        M[y][x]++;
        drawblock(y,x); 
        x = x+DX;
        y = y+DY;
        drawdot(y,x); 
    }//while


      // modify this function to move the dot to the end of the maze.  That
      // is, when the dot reaches y==mh-2, x==mw-2
  } // solve


    ///////////////////////////////////////////////////////////////
    /// For part three (save a copy of part 2 version first!), you
    // need to implement the KeyListener interface.

    public void play() // for part 3
    {
    // code to setup game
    }
    // for part 3 you may also define some other instance vars outside of
    // the play function.

   // for KeyListener interface
   public void keyReleased(KeyEvent e) {}
   public void keyTyped(KeyEvent e) {}
   public void keyPressed(KeyEvent e) // change this one
    {
    int key = e.getKeyCode();       // code for key pressed      
    System.out.println("YOU JUST PRESSED KEY "+key);
    }

} // mazedfs


////////////
// define additional classes (stack) you may need here.
EN

回答 1

Stack Overflow用户

发布于 2014-09-19 11:38:01

导致您面临的第二个问题(圆点移动到绿色框)的问题存在于while循环条件y!=mh-1 || x!=mw-1 && M[y][x]!=0中。该条件的计算结果为y!=mh-1 ||(x!=mw-1 && M[y][x]!=0),因为&&的优先级高于||,而||只需要其中一个操作数为true。在您的例子中,y!=mh-1在迷宫的末尾仍然是正确的。因此,循环继续,点移动到绿色区域。要解决此问题,请将条件修改为(y!=mh-1 || x!=mw-1) && M[y][x]!=0。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25925664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档