Java非递归实现迷宫问题
Posted 妄想症T
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java非递归实现迷宫问题相关的知识,希望对你有一定的参考价值。
这个题目是本人的一次课程设计,也是我第一次独立做完的一个小玩意,说实话,昨晚的那一刻很有成就感。整个人开心到在自习室蹦起来。因为之前一直是自学的Java,从没有自己做过任何一个项目,这一个课程设计就花费了我三天的时间,其实应该是两天半,两天半我做出来之后和室友去炫耀,老哥看完说一句,要是把之前的路堵死,从新换一条路呢。然后就炸了。。。。。。。。。。。。。。。在做完之后我也只开心了三秒,因为兴奋之后确实无尽的空虚,不知道该向谁去分享自己的这个成就,单身狗伤不起啊。话不多说,直接上代码
界面构造部分
package 迷宫问题;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyFrame extends JFrame {
private int FIELDSIZE = 50;
public JLabel[][] labs;
public MyFrame() {
setTitle("迷宫问题");
// setName("test");
setBounds(400, 200, 800, 850);
setResizable(false);
JPanel boardPane = new JPanel();
boardPane.setLayout(null);
add(boardPane);
labs = new JLabel[16][16];
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
JLabel backgroundLabel = new JLabel();
backgroundLabel.setOpaque(true);
backgroundLabel.setBounds(x * FIELDSIZE, y * FIELDSIZE, FIELDSIZE, FIELDSIZE);
boardPane.add(backgroundLabel, new Integer(1), 0);
labs[x][y] = backgroundLabel;
}
}
setColor(labs);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void setColor(JLabel[][] labs) {
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
if (x == 0) {
labs[x][y].setBackground(Color.BLACK);
}
}
}
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
if (x == 15) {
labs[x][y].setBackground(Color.BLACK);
}
}
}
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
if (y == 0) {
labs[x][y].setBackground(Color.BLACK);
}
}
}
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
if (y == 15) {
labs[x][y].setBackground(Color.BLACK);
}
}
}
for (int y = 0; y < 8; y++) {
labs[2][y].setBackground(Color.BLACK);
}
for (int y = 9;y < 15; y++) {
labs[2][y].setBackground(Color.BLACK);
}
for (int x= 3; x < 7;x++) {
labs[x][7].setBackground(Color.BLACK);
}
for (int x= 6; x < 12;x++) {
labs[x][4].setBackground(Color.BLACK);
}
labs[6][9].setBackground(Color.BLACK);
labs[6][10].setBackground(Color.BLACK);
for (int x= 3; x < 10;x++) {
labs[x][10].setBackground(Color.BLACK);
}
for (int x= 4; x < 11;x++) {
labs[x][13].setBackground(Color.BLACK);
}
labs[8][15].setBackground(Color.BLACK);
for (int y = 2;y < 4; y++) {
labs[11][y].setBackground(Color.BLACK);
}
for (int y = 6;y < 14; y++) {
labs[11][y].setBackground(Color.BLACK);
}
for (int y = 7;y < 14; y++) {
labs[13][y].setBackground(Color.BLACK);
}
labs[1][1].setBackground(Color.RED);
labs[14][14].setBackground(Color.GREEN);
labs[9][6].setBackground(Color.black);
labs[9][5].setBackground(Color.black);
labs[13][1].setBackground(Color.black);
labs[13][2].setBackground(Color.black);
labs[13][3].setBackground(Color.black);
labs[14][3].setBackground(Color.black);
labs[14][4].setBackground(Color.black);
labs[11][5].setBackground(Color.black);
// labs[14][5].setBackground(Color.black);
labs[12][13].setBackground(Color.black);
//labs[3][13].setBackground(Color.black);
labs[14][6].setBackground(Color.black);
}//构造界面
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
分析:
红色为入口,绿色为出口。当访问至绿色,程序结束。
}
自己实现栈的定义,因为我们的课程设计不允许直接调用API
package 迷宫问题;
public class MyStack_Text {
Object[] stacks;
int size;
int top;
int len;
public MyStack_Text(int size) {
super();
this.size = size;
this.stacks = new Object[this.size];
this.top = -1;
}
public Object peek() {
return this.stacks[top];
}
public boolean empty() {
return top == (-1);
}
public boolean isFull() {
return top == (size - 1);
}
public void push(Object value) {
len++;
stacks[++this.top] = value;
}
public Object pop() {
len--;
return stacks[this.top--];
}
public int len() {
return this.len;
}
}
核心算法部分
分析:不用分析,就是基础的数据结构栈的构建
package 迷宫问题;
import java.awt.Color;
import java.util.Stack;
import java.util.TimerTask;
import javax.swing.JLabel;
public class Run extends TimerTask{
// Stack<JLabel> stack = new Stack<JLabel>();//不愿意定义栈,可以在此调用
Run(){
}
MyFrame myFrame = new MyFrame();
MyStack_Text stack=new MyStack_Text(100);
public void run(){
int i=1,j=1;
stack.push(myFrame.labs[1][1]);
while(!stack.empty()||myFrame.labs[i][j].getBackground() != Color.GREEN){
//��
while(myFrame.labs[i][j-1].getBackground() != Color.BLACK &&myFrame.labs[i][j].getBackground() != Color.GREEN&& myFrame.labs[i][j-1].getBackground() != Color.yellow&& myFrame.labs[i][j-1].getBackground() != Color.RED&& myFrame.labs[i][j-1].getBackground() != Color.pink){
if (myFrame.labs[i][--j].getBackground() != Color.GREEN)
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
myFrame.labs[i][j].setBackground(Color.yellow);
stack.push(myFrame.labs[i][j]);
}
//��
while(myFrame.labs[i][j+1].getBackground() != Color.BLACK&&myFrame.labs[i][1+j].getBackground() != Color.GREEN && myFrame.labs[i][j+1].getBackground() != Color.yellow&& myFrame.labs[i][j+1].getBackground() != Color.RED&& myFrame.labs[i][j+1].getBackground() != Color.pink){
if (myFrame.labs[i][++j].getBackground() != Color.GREEN){
myFrame.labs[i][j].setBackground(Color.yellow);
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
stack.push(myFrame.labs[i][j]);
}
}
//��
while(myFrame.labs[i-1][j].getBackground() != Color.BLACK &&myFrame.labs[i][j].getBackground() != Color.GREEN&& myFrame.labs[i-1][j].getBackground() != Color.yellow&& myFrame.labs[i-1][j].getBackground() != Color.RED&& myFrame.labs[i-1][j].getBackground() != Color.pink){
if (myFrame.labs[--i][j].getBackground() != Color.GREEN){
myFrame.labs[i][j].setBackground(Color.yellow);
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
stack.push(myFrame.labs[i][j]);
}
}
//��
while(myFrame.labs[i+1][j].getBackground() != Color.BLACK &&myFrame.labs[i][j].getBackground() != Color.GREEN&& myFrame.labs[i+1][j].getBackground() != Color.yellow&& myFrame.labs[i+1][j].getBackground() != Color.RED&& myFrame.labs[i+1][j].getBackground() != Color.pink){
if (myFrame.labs[++i][j].getBackground() != Color.GREEN){
myFrame.labs[i][j].setBackground(Color.yellow);
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
stack.push(myFrame.labs[i][j]);
}
}
if (myFrame.labs[i][j+1].getBackground() != Color.GREEN) {
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
JLabel XX= (JLabel) stack.peek();
i= XX.getX()/50;
j = XX.getY()/50;
int pp = 0;
if( myFrame.labs[i+1][j].getBackground() == Color.black||myFrame.labs[i+1][j].getBackground() == Color.pink||myFrame.labs[i+1][j].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i-1][j].getBackground() == Color.black||myFrame.labs[i-1][j].getBackground() == Color.pink||myFrame.labs[i-1][j].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i][j+1].getBackground() == Color.black||myFrame.labs[i][j+1].getBackground() == Color.pink||myFrame.labs[i][j+1].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i][j-1].getBackground() == Color.black||myFrame.labs[i][j-1].getBackground() == Color.pink||myFrame.labs[i][j-1].getBackground() == Color.yellow)
pp++;
if (myFrame.labs[i][j].getBackground() != Color.GREEN)
if(pp==4){
stack.pop();
XX.setBackground(Color.pink);
//System.out.println(i);
// System.out.println(j);
}//if
}//if
}//while
}
public static void main(String[] args) {
// MyFrame myFrame = new MyFrame();
Run R =new Run();
//R.run(1, 1);
R.run();
}
}
分析 :此处黑色代表障碍,白色代表可以走的路径,黄色表示最终路径,而粉色则是进栈之后出栈的标记。
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
用到了进程的休眠,让进栈出栈能够直观的看出来
int pp = 0;
if( myFrame.labs[i+1][j].getBackground() == Color.black||myFrame.labs[i+1][j].getBackground() == Color.pink||myFrame.labs[i+1][j].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i-1][j].getBackground() == Color.black||myFrame.labs[i-1][j].getBackground() == Color.pink||myFrame.labs[i-1][j].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i][j+1].getBackground() == Color.black||myFrame.labs[i][j+1].getBackground() == Color.pink||myFrame.labs[i][j+1].getBackground() == Color.yellow)
pp++;
if( myFrame.labs[i][j-1].getBackground() == Color.black||myFrame.labs[i][j-1].getBackground() == Color.pink||myFrame.labs[i][j-1].getBackground() == Color.yellow)
pp++;
if (myFrame.labs[i][j].getBackground() != Color.GREEN)
if(pp==4){
stack.pop();
XX.setBackground(Color.pink);
//System.out.println(i);
// System.out.println(j);
}//if
这应该是本人的得意之作了,进行判断什么时候出栈。进过分析后发现,出栈是栈顶元素上下左右均不可走,即上下左右被黄色、粉色、黑色这三种颜色中的一种或几种包围了。所以,此处定义一个pp变量,在上下左右有黄色、粉色、黑色三种颜色任意几种的时候,pp++,当pp==4,即上下左右都是这三种颜色的一种或几种时,出栈。我想,我想啊,这应该是就是抽象思维的一种表现形式吧,抽象出四周环境的一致性,就行统一判断,减少了代码量。
以上是关于Java非递归实现迷宫问题的主要内容,如果未能解决你的问题,请参考以下文章