java迷宫算法
来源:互联网 发布:c数值算法 编辑:程序博客网 时间:2024/05/17 23:13
本章使用栈来实现迷宫算法
基本思路为:
对入口节点进行标记
入口节点入栈
while(栈不空){
查看栈顶元素;
if(栈顶元素为出口节点){
循环输出栈中元素
}
else{
if(查看该节点的下方节点是否符合条件){
对下方节点标记;
下方节点入栈;
continue;
}
if(查看该节点的右方节点是否符合条件){
对右方节点进行标记;
右方节点入栈;
continue;
}
if(查看该节点的上方节点是否符合条件){
对上方节点进行标记;
上方节点入栈;
continue;
}
if(查看该节点的左方节点是否符合条件){
对左方节点进行标记;
左方节点入栈;
continue;
}
都不符合条件,当前节点出栈;}
}
定义节点类:
class Point{int x;int y;int color=0;public Point(int x,int y){this.x = x;this.y = y;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Point other = (Point) obj;if (x != other.x)return false;if (y != other.y)return false;return true;}@Overridepublic String toString() {return "Point [x=" + x + ", y=" + y + "]";}public int getColor() {return color;}public Point setColor(int color) {this.color = color;return this;}}主程序:
public static void main(String[] args) {Stack<Point> s = new Stack<Point>();Point[][] roat=new Point[][]{{new Point(0,0),new Point(0,1).setColor(1),new Point(0,2),new Point(0,3),new Point(0,4)},{new Point(1,0),new Point(1,1).setColor(1),new Point(1,2),new Point(1,3).setColor(1),new Point(1,4)},{new Point(2,0),new Point(2,1),new Point(2,2),new Point(2,3).setColor(1),new Point(2,4)},{new Point(3,0).setColor(1),new Point(3,1).setColor(1),new Point(3,2).setColor(1),new Point(3,3).setColor(1),new Point(3,4)},{new Point(4,0).setColor(1),new Point(4,1).setColor(1),new Point(4,2).setColor(1),new Point(4,3).setColor(1),new Point(4,4)}};Point p = new Point(0,0);p.color =2;Point p1 = new Point(4,4);findRoat(s,p,p1,roat);}public static void findRoat(Stack<Point> s,Point rukou,Point chukou,Point[][] roat){Stack<Point> out = new Stack<Point>();int xx=roat[0].length;int yy=roat.length;rukou.color = 2;s.push(rukou);while(!s.isEmpty()){System.out.println("s.isEmpty()");Point p = s.peek();if(p.equals(chukou)){while(!s.isEmpty()){//System.out.println(s.pop());out.push(s.pop());}while(!out.isEmpty()){System.out.println(out.pop());}}else{if(( p.x+1<xx )&&( roat[p.x+1][p.y].color==0)){roat[p.x+1][p.y].color = 2;//标记走过s.push(roat[p.x+1][p.y]);System.out.println("1:"+(p.x+1)+","+(p.y));continue;}if(p.y+1<yy && (roat[p.x][p.y+1].color == 0)){roat[p.x][p.y+1].color = 2;s.push(roat[p.x][p.y+1]);System.out.println("2:"+(p.x)+","+(p.y+1));continue;}if(p.x -1 >=0 && roat[p.x-1][p.y].color==0){roat[p.x-1][p.y].color = 2;s.push(roat[p.x-1][p.y]);System.out.println("3:"+(p.x-1)+","+(p.y));continue;}if(p.y-1 >= 0 && roat[p.x][p.y-1].color == 0){roat[p.x][p.y-1].color =2;s.push(roat[p.x][p.y-1]);System.out.println("4:"+(p.x)+","+(p.y-1));continue;}s.pop();System.out.println("s.pop()");}}}public static void print(Stack<Point> s){if(!s.isEmpty()){print(s);System.out.println(s.pop());}}
y轴
————————
| 0 1 0 0 0 |
x轴 | 0 1 0 1 0 |
| 0 0 0 1 0 |
| 1 1 1 1 0 |
| 1 1 1 1 0 |
—————————
输出结果
Point [x=0, y=0]
Point [x=1, y=0]
Point [x=2, y=0]
Point [x=2, y=1]
Point [x=2, y=2]
Point [x=1, y=2]
Point [x=0, y=2]
Point [x=0, y=3]
Point [x=0, y=4]
Point [x=1, y=4]
Point [x=2, y=4]
Point [x=3, y=4]
Point [x=4, y=4]
- java实现迷宫算法
- java迷宫算法
- java堆栈迷宫算法
- java实现迷宫算法
- 迷宫算法(JAVA实现)
- java实现迷宫算法--转
- 算法编程(JAVA)--迷宫问题
- java实现走迷宫算法
- 【未完】【java算法】迷宫求解
- java如何实现迷宫算法
- 迷宫算法(java实现)
- 一个JAVA编写的迷宫算法。。自动找迷宫出口
- 严蔚敏《数据结构》中迷宫算法java实现
- Java实现A*算法解决迷宫问题
- 使用栈的迷宫算法java版
- 走迷宫回溯算法(Java实现)
- Java 递归算法解迷宫问题
- hdu-1269-迷宫城堡-Tarjan算法-java
- IEnumerable和IEnumerator 详解
- 采购预算管理系统
- 浅谈凸优化问题中的Bregman迭代算法
- APP界面设计之尺寸篇
- 多线程编程(四)--线程同步
- java迷宫算法
- 黑马程序员——java基础之异常
- java-web-j2e学习建议路线
- 多线程编程(五)--线程池
- 【再思考】PATBasic——1033. 旧键盘打字(20)
- OJ第二批——Problem I:改错题:类中私有成员的访问
- 边缘检测子综述(A review of classic edge detectors[J]. Image Processing On Line, 2012.)
- MongoDB:逐渐变得无关紧要
- Android In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing