老鼠走迷宫

package datasystem;
/**  
* 描述:老鼠走迷宫 MAZE[i][j]=1 此处有墙 无法通过      MAZE[i][j]=0  此处无墙,可通过
*               MAZE[1][1] 入口,MAZE[ExitX][ExitY] 出口。MAZE[i][j]=2 表示已经走过的路径
*  
* @author ASUS  
* @date 2018年6月17日
*/
//用一个链表记录路径
class TraceRecord {
	 /**
	   * 定义节点的属性
	   */
	  class Entry {
	      int X;
	      int Y;
	      Entry next;

	      public Entry(int X, int Y) {
	          this.X = X;
	          this.Y = Y;
	      }
	  }
  public Entry First;
  public Entry Last;

  /**
   * 判断节点是否为空
   */
  public boolean isEmpty() {
      return First == null;
  }

  /**
   * 增加节点
   */
  public void insert(int X, int Y) {
	  Entry n = new Entry(X, Y);
      if (isEmpty()) {
          First = n;
          Last = n;
      } else {
          Last.next = n;
          Last = n;
      }
  }

  /**
   * 删除节点
   */
  public void delete() {

      if (isEmpty()) {
          System.out.println("链表已经是空了");
      } else {
    	  Entry n = First;
          while (n.next != Last) {
              n = n.next;
          }
          n.next = Last.next;
          Last = n;
      }
  }

 

}
public class MouseRanMaze {
	public static int ExitX = 8;// 定义出口x坐标在第八行
    public static int ExitY = 10;// 定义出口的y坐标在第10列
    public static int[][] MAZE = {
    		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1 },
            { 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    };

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TraceRecord path = new TraceRecord();
        System.out.println("下图是迷宫的路径:");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 12; j++) {
                System.out.print(MAZE[i][j] + " ");
            }
            System.out.println();
        }

        int x = 1, y = 1;
        while (x <= ExitX && y <= ExitY) {
            // 将走过的路径设为2
            MAZE[x][y] = 2;

            if (MAZE[x - 1][y] == 0) {// 判断向上走
                path.insert(--x, y);
            } else if (MAZE[x + 1][y] == 0) {// 判断向下走
                path.insert(++x, y);
            } else if (MAZE[x][y + 1] == 0) {// 判断向右走
                path.insert(x, ++y);
            } else if (MAZE[x][y - 1] == 0) {// 判断向左走
                path.insert(x, --y);
            } else if (x == ExitX && y == ExitY) {
                break;
            } else {
                x = path.Last.X;
                y = path.Last.Y;
                path.delete();
            }
        }
        // 走到出口
        System.out.println("打印路径(2)");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 12; j++) {
                System.out.print(MAZE[i][j] + " ");
            }
            System.out.println();
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_41974391/article/details/80719138