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]

0 0
原创粉丝点击