一個JAVA編寫的迷宮演算法。。自動找迷宮出口
import java.awt.Color;
import java.awt.GridLayout;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FindPath {
private static final int wid = 10;
private static final int hei = 10;
JFrame frame; //窗體,是整個迷宮的容器
JPanel panel;
JButton button[];
private Stack stack = new Stack();
public static void main(String[] args) {
byte[] map ={
1,1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,0,0,1,
1,0,1,1,1,0,0,0,0,1,
1,0,0,0,1,0,0,0,0,1,
1,0,1,0,0,0,1,0,0,1,
1,0,1,1,1,0,1,1,0,1,
1,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,
};
int i=0;
FindPath ai = new FindPath();
ai.find(map, 11, 88);
}
FindPath()
{
int i;
frame = new JFrame("孫堯的神奇迷宮");
frame.setBounds(300,240,500,500); //調整迷宮出現的位置及大小
frame.setResizable(false);//窗體不可拉伸
panel = new JPanel();
frame.getContentPane().add(panel); //將面板新增到窗體中
panel.setLayout(new GridLayout(10,10)); //panel用網格佈局,10行10列
button = new JButton[100];
for ( i = 0; i < button.length; i++)
{
button[i]=new JButton(Integer.toString(i)); //建立按鍵的名字,Integer型的名字為i的字串
if((i>=0&&i<=9)||(i>=90&&i<=99)||i%10==0||i%10==9||i==13||i==17||i==23||i==27||i==35||i==36||i==42||i==43||i==44||i==54||i==62||i==66||i==72||i==73||i==74||i==76||i==77||i==81)
{
button[i].setBackground(Color.red);
}
else
{
button[i].setBackground(Color.white);
}
button[i].setSize(10,10);
panel.add(button[i]);
}
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); //宣告點“X”圖示後結束窗體所在的應用程式
frame.setVisible(true); //表明以上建立的所有窗體、按鍵等元件都是可見的
}
public void find(byte[] map, int origin, int target) {
int[] step = new int[2];
step[1] = origin;
stack.addElement(step);
if(findPath(map,origin,target)){
System.out.println("succ");
for (int i = 0; i < stack.size(); i++) {
int[] temp = (int[])stack.elementAt(i);
System.out.println(i+" /t"+(char)temp[0]+" "+temp[1]);
button[temp[1]].setBackground(Color.blue);
}
}else{
System.out.println("fail");
}
}
public boolean findPath(byte[] map, int origin, int target) {
if (canMoveTo(map, origin, target, 'l')) {
return true;
}
if (canMoveTo(map, origin, target, 'r')) {
return true;
}
if (canMoveTo(map, origin, target, 'u')) {
return true;
}
if (canMoveTo(map, origin, target, 'd')) {
return true;
}
stack.pop();// 如果四個方向都試過,全部不行,那麼把當前步驟彈出
return false;
}
private boolean canMoveTo(byte[] map, int origin, int target, char direct) {
int next = 0;
switch (direct) {
case 'l':
next = origin - 1;
break;
case 'r':
next = origin + 1;
break;
case 'u':
next = origin - wid;
break;
case 'd':
next = origin + wid;
break;
}
if (map[next] == 0) {//如果目標位置可以進入
if (next == target) {
int[] step = new int[2];
step[0] = direct;// 移動方向
step[1] = next;// 到達的新位置
stack.addElement(step);
return true;
}
if (!inStack(next)) {
int[] step = new int[2];
step[0] = direct;
step[1] = next;
stack.addElement(step);
if (findPath(map, next, target)) {
return true;
}
}
}
return false;
}
private boolean inStack(int posi) {
int[] temp;
for (int i = stack.size() - 1; i >= 0; i--) {
temp = (int[]) stack.elementAt(i);
if (posi == temp[1]) {
return true;
}
}
return false;
}
}