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非递归实现迷宫问题的更多相关文章
- Java非递归的方式获取目录中所有文件(包括目录)
零.思路解析 对于给出的文件查看其下面的所有目录,将这个目录下的所有目录放入待遍历的目录集合中,每次取出该集合中的目录遍历,如果是目录再次放入该目录中进行遍历. 一.代码 /** * 非递归的方式获取 ...
- Binary Search(Java)(非递归)
public static int rank(int[] array, int k) { int front = 0, rear = array.length - 1; while(front < ...
- Java 非递归实现 二叉树的前中后遍历以及层级遍历
class MyBinaryTree<T> { BinaryNode<T> root; public MyBinaryTree() { root = new BinaryNod ...
- 算法笔记_013:汉诺塔问题(Java递归法和非递归法)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...
- 排序算法练习--JAVA(插入、直接选择、冒泡、快速排序、非递归快速排序)
排序算法是数据结构中的经典算法知识点,也是笔试面试中经常考察的问题,平常学的不扎实笔试时候容易出洋相,回来恶补,尤其是碰到递归很可能被问到怎么用非递归实现... package sort; impor ...
- 自己写算法---java的堆的非递归遍历
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { S ...
- 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化
上两篇博客 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,数据结构“栈”实现 研究了递归方法实现回溯,解决N皇后问题,下面我们来 ...
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- 二叉树3种递归和非递归遍历(Java)
import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...
随机推荐
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解
一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...
- Machine Learning and Data Mining(机器学习与数据挖掘)
Problems[show] Classification Clustering Regression Anomaly detection Association rules Reinforcemen ...
- python3关于date和time的标准库
python3中关于日期和时间的标准库datetime和time,之前都是用的时候随用随查,今天系统的看一下用这两个库可以做些什么. 1.time标准库 #首先添加一个time对象,看一下该对象的属性 ...
- php代码审计4审计代码执行漏洞
代码执行漏洞代码执行漏洞是指应用程序本身过滤不严,用户可以通过请求将代码注入到应用中执行,当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,造成 ...
- 【转】C#控件——DataGridView单元格文本自动换行
源地址:https://www.cnblogs.com/wangshenhe/archive/2012/07/25/2608324.html DataGridView是.NET开发中常用的控件,在开发 ...
- oracle转义用单引号
参考:https://blog.csdn.net/learning_oracle_lh/article/details/46639507
- LNMP(linux+nginx+mysql+php)服务器环境配置【转载】
本文转载自 园友David_Tang的博客,如有侵权请联系本人及时删除,原文地址: http://www.cnblogs.com/mchina/archive/2012/05/17/2507102.h ...
- Spark大数据处理 之 从WordCount看Spark大数据处理的核心机制(1)
大数据处理肯定是分布式的了,那就面临着几个核心问题:可扩展性,负载均衡,容错处理.Spark是如何处理这些问题的呢?接着上一篇的"动手写WordCount",今天要做的就是透过这个 ...
- JDBC_时间处理_Date_Time_Timestamp区别_随机日期生成
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;import ...
- Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)
Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...