Java non-recursive implementation of maze problem

Source: Internet
Author: User

This topic is my one course design, is also my first time to finish a small thing independently, to tell the truth, last night that moment was very fulfilling. The whole person was happy to jump up in the study room. Because before has been self-taught Java, never done any project of their own, this course design took me three days, in fact, it should be two days and a half, two days and a half after I made out with the roommate to show off, old brother read to say, if the road before plugging dead, a new way to change. And then he blew it up ....... After finishing I was only happy three seconds, because after the excitement is indeed endless emptiness, do not know to whom to share their achievements, single dog can not hurt ah. Words don't say much, directly on the code

Interface Construction Section
            

Package maze problem;

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 ("Maze problem");
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 < + × x + +) {
for (int y = 0; y < 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 < + × x + +) {
for (int y = 0; y < y++) {
if (x = = 0) {
Labs[x][y].setbackground (Color.Black);
}
}
}
for (int x = 0; x < + × x + +) {
for (int y = 0; y < y++) {
if (x = = 15) {
Labs[x][y].setbackground (Color.Black);
}
}
}
for (int x = 0; x < + × x + +) {
for (int y = 0; y < y++) {
if (y = = 0) {
Labs[x][y].setbackground (Color.Black);
}
}
}
for (int x = 0; x < + × x + +) {
for (int y = 0; y < 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 < 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 <; y++) {
Labs[11][y].setbackground (Color.Black);
}
for (int y = 7;y <; 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);
}//Construction Interface

public static void Main (string[] args) {
MyFrame myframe = new MyFrame ();

}

Analysis:

The red is the entrance, the green is the exit. When access to green, the program ends.

}

Implement the definition of the stack yourself, because our curriculum design does not allow direct invocation of the API

Package maze problem;
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;
}

}

Core algorithms Section

Analysis: No analysis, is the foundation of the construction of data structure stack

Package maze problem;

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> ();//Do not want to define the stack, where you can call

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 ();

}
}

Analysis: Here the black represents the barrier, the white represents the path that can go, yellow indicates the final path, and the pink is the tag that goes out of the stack after the stack.

try{
Thread.Sleep (100);
}
catch (Interruptedexception e) {
E.printstacktrace ();
}

Using the process of hibernation, let the stack out of the stack can be seen intuitively

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

This should be my pride, to judge when the stack. After analysis, it is found that the stack is the top of the stack is not allowed to go up or down, that is, yellow, pink, black, three kinds of color in the surrounding of one or several. Therefore, the definition of a PP variable, in the upper and lower left and right there are yellow, pink, black three colors any number of times, pp++, when the pp==4, that is, about the top and bottom are the three colors of one or several, out of the stack. I think, I think ah, this should be a form of abstract thinking, abstract four weeks of environmental consistency, the unified judgment, reduce the amount of code.

Java non-recursive implementation of maze problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.