数据结构 迷宫走法
1>找到最短路径
定义一个 Postion 类 用于定义方向
package cn.zuoye;
public class Postion {
int x;//行坐标
int y;//列坐标
Postion parent;
public Postion(int x, int y )
{
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
//通过坐标判断是否相等
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Postion other = (Postion) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString() {
return "Postion [x=" + x + ", y=" + y + ","+ "]";
}
}
定义一个 Mg 类进行 迷宫地图的定义 及 开始坐标(1,1) 终点的坐标 (4,4)
package cn.zuoye;
import java.util.LinkedList;
public class Mg {
int a[][]={
{
1,1,1,1,1,1},
{
1,0,1,0,0,1},
{
1,0,1,1,0,1},
{
1,0,0,0,0,1},
{
1,1,0,0,0,1},
{
1,1,1,1,1,1}
};//地图
int startX=1;//入口位置x
int startY=1;//入口位置y
int endX=4;//出口位置x
int endY=4;//出口位置y
LinkedList<Postion> quequ = new LinkedList<Postion>();
public void go() {
quequ.addLast(new Postion(startX,startY));
while(!quequ.isEmpty()