题目如图片:

处理的办法及代码:
1、迷宫中每个元素,有值、行、列,用一个类写入。
public class MazeNode {
private int value;//值
private int i;//x
private int j;//y
private boolean []wayState;//状态
public MazeNode(int value,int i,int j) {
this.value=value;
this.i=i;
this.j=j ;
wayState=new boolean[Contant.INITSIZE];
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void setI(int i) {
this.i = i;
}
public void setJ(int j) {
this.j = j;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public boolean getWayState(int direction) {
return wayState[direction];
}
public void setWayState(int direction,boolean isAble) {
wayState[direction] = isAble;
}
}
2、然后进行分析,每个迷宫中元素其实是有其特性的。我们利用可以利用接口来实现。
四个方向 :上、下、左、右;
两个状态:可走、不可走
//接口里的方向
public interface Contant {
public static final int INITSIZE = 4;
public static final int WAY_EAST = 0;
public static final int WAY_SOUTH = 1;
public stati

本文介绍了一种使用Java通过栈来解决迷宫问题的方法。首先定义一个包含值、行、列信息的类,利用接口处理迷宫元素的特性。接着创建Maze类,用于定义迷宫并实现走迷宫操作。测试类验证了算法的正确性。最后,文章提出了一个puzzle问题,要求递归或栈/队列实现,将标记移动到行的远端0位置,对于任意大小的方格数和随机数字,程序需要能够检测不可解结构并优化执行时间。
最低0.47元/天 解锁文章
1618

被折叠的 条评论
为什么被折叠?



