class
point
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private int x;
private int y;
public point(int x,int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.x=x;
this.y=y;
}
public int getX()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return x;
}
public int getY()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return y;
}
public void setX(int x)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.x=x;
}
public void setY(int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.y=y;
}
}
class
TracePoint
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
point thePoint;
int direction;
public TracePoint()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint=new point(-1,-1);
this.direction=0;
}
public TracePoint(int x,int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint=new point(x,y);
this.direction=0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public point getPoint()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return thePoint;
}
public void setPoint(point t)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint.setX(t.getX());
thePoint.setY(t.getY());
}
public int getDirection()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return direction;
}
public void setDirection(int t)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.direction=t;
}
public void reset()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint.setX(-1);
thePoint.setY(-1);
direction=0;
}
public void setX(int x)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint.setX(x);
}
public void setY(int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
thePoint.setY(y);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int getX()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return thePoint.getX();
}
public int getY()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return thePoint.getY();
}
}
class
Trace
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private int total;
private TracePoint[] grid;
private boolean[][] field;
public Trace()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
grid=new TracePoint[64];
field=new boolean[8][8];
for(int i=0;i<64;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
grid[i]=new TracePoint();
}
for(int i=0;i<8;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int j=0;j<8 ; j++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
field[i][j]=false;
}
}
total=0;
}
public int getTotal()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return total;
}
public void setTotal(int t)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.total=t;
}
public TracePoint[] getGrid()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return this.grid;
}
public TracePoint getElement(int i)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return grid[i];
}
public TracePoint getLastElement()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return grid[total-1];
}
public boolean isSelected(int x,int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return this.field[x][y];
}
public void setSelected(int x,int y,boolean b)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
field[x][y]=b;
}
public void reset(int i)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
field[grid[i].getX()][grid[i].getY()]=false;
grid[i].reset();
}
public void backOneStep()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
reset(total-1);
total--;
}
public void output()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for(int i=0;i<64;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.out.println("("+grid[i].getX()+","+grid[i].getY()+")");
}
}
}
class
point
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private int x;
private int y;
public point(int x,int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.x=x;
this.y=y;
}
public int getX()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return x;
}
public int getY()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return y;
}
public void setX(int x)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.x=x;
}
public void setY(int y)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.y=y;
}
}
class
BianLi
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
private point[] direction;//方向。
private Trace trace;//记录已经走过的路经
public BianLi()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
direction =new point[8];
direction[0]=new point(-2,-1);
direction[1]=new point(-1,-2);
direction[2]=new point(1,-2);
direction[3]=new point(2,-1);
direction[4]=new point(2,1);
direction[5]=new point(1,2);
direction[6]=new point(-1,2);
direction[7]=new point(-2,1);
trace=new Trace();
}
public void starPlay(int x,int y)//从点(x,y)开始
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace.setTotal(1);
trace.getLastElement().setX(x);
trace.getLastElement().setY(y);
trace.setSelected(x,y,true);
while(trace.getTotal()<64)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TracePoint nextPoint=choose();
if(nextPoint==null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(!back())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(!forward(nextPoint))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
}
}
if(trace.getTotal()==64)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace.output();
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.out.println("搜索失败!!!!");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
public TracePoint choose()//选择下一个点
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int x;
int y;
for(int i=trace.getLastElement().getDirection();i<8;i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
x=trace.getLastElement().getX()+direction[i].getX();
y=trace.getLastElement().getY()+direction[i].getY();
if(x>=0&&x<8&&y>=0&&y<8&&!trace.isSelected(x,y))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace.getLastElement().setDirection(i);
return new TracePoint(x,y);
}
}
return null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
public boolean forward(TracePoint t)//前进
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(trace.getTotal()==64)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return false;
}
trace.setTotal(trace.getTotal()+1);
trace.getElement(trace.getTotal()-1).setDirection(t.getDirection());
trace.getElement(trace.getTotal()-1).setX(t.getX());
trace.getElement(trace.getTotal()-1).setY(t.getY());
trace.setSelected(t.getX(),t.getY(),true);
return true;
}
public boolean back()//回退
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
while(trace.getTotal()>=0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(trace.getLastElement().getDirection()==8)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace.backOneStep();
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
}
if(trace.getTotal()>=0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
trace.getLastElement().setDirection(trace.getLastElement().getDirection()+1);//下一个方向
return true;
}
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public static void main(String[] args)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BianLi horse=new BianLi();
horse.starPlay(0,0);
}
}
运行结果:
(0,0)
(2,1)
(4,0)
(6,1)
(7,3)
(5,2)
(3,1)
(1,0)
(2,2)
(0,1)
(2,0)
(4,1)
(6,0)
(7,2)
(5,1)
(3,0)
(4,2)
(5,0)
(7,1)
(6,3)
(7,5)
(5,4)
(3,3)
(1,2)
(2,4)
(0,3)
(1,1)
(3,2)
(5,3)
(7,4)
(6,6)
(4,5)
(6,4)
(7,6)
(5,5)
(6,7)
(4,6)
(3,4)
(1,3)
(0,5)
(1,7)
(2,5)
(0,4)
(1,6)
(3,7)
(5,6)
(7,7)
(6,5)
(5,7)
(3,6)
(4,4)
(2,3)
(0,2)
(1,4)
(0,6)
(2,7)
(1,5)
(0,7)
(2,6)
(4,7)
(3,5)
(4,3)
(6,2)
(7,0)