找跳马最短路径的算法

今天在网上看到一个{找跳马最短路径的算法} Find shortest way of a Chinese chess horse that jump from (0, 0) to (m, n) in a grid area of m*n, and output the step number and way points. For example, in a grid area of (3 * 2), a horse can jump as (0, 0)->(1, 2)->(2, 0)->(3,2). And step number is 3. 感觉在一个m*n的矩阵中跳要考虑边界问题,在此用java写了一下,没有考虑边界,以下程序只在无限二维中成立
代码如下
None.gif
ExpandedBlockStart.gifContractedBlock.gif /** */ /**
InBlock.gif * @author : piliskys
InBlock.gif * Date: 2006-2-22
InBlock.gif * Time: 13:50:56
InBlock.gif * 找跳马最短路径的算法
InBlock.gif * 
ExpandedBlockEnd.gif */

ExpandedBlockStart.gifContractedBlock.gif public   class  HorsePro  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] arg) dot.gif{
InBlock.gif        HorsePosition start = new HorsePosition(0, 0);
InBlock.gif        HorsePosition end = new HorsePosition(0,1);
InBlock.gif        int index=0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        while (true) dot.gif{
InBlock.gif               index++;
InBlock.gif            HorsePosition her = getNext(start, end);
InBlock.gif            if (her.positionX == 0 && her.positionY == 0||index==7)
InBlock.gif                break;
InBlock.gif            start = new HorsePosition(start.positionX + her.positionX, start.positionY + her.positionY);
InBlock.gif            System.out.println("第["+index+"]步=>"+start);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif      /** *//**
InBlock.gif       * 以下为构造一个位置类
ExpandedSubBlockEnd.gif       */

ExpandedSubBlockStart.gifContractedSubBlock.gif    static class HorsePosition dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        HorsePosition(int a, int b) dot.gif{
InBlock.gif            this.positionX = a;
InBlock.gif            this.positionY = b;
ExpandedSubBlockEnd.gif        }

InBlock.gif        int positionX;
InBlock.gif        int positionY;
ExpandedSubBlockStart.gifContractedSubBlock.gif        public String toString() dot.gif{
InBlock.gif            return "[" + this.positionX + "," + this.positionY + "]";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    public static HorsePosition getNext(HorsePosition a, HorsePosition b) dot.gif{
InBlock.gif        int x, y, z;
InBlock.gif        x = b.positionX - a.positionX;
InBlock.gif        y = b.positionY - a.positionY;
InBlock.gif        z = Math.abs(x) + Math.abs(y);
ExpandedSubBlockStart.gifContractedSubBlock.gif        if (z >= 3) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            if (Math.abs(x) > Math.abs(y)) dot.gif{
InBlock.gif                int yy;
InBlock.gif                if (y == 0)  yy = 1;
InBlock.gif                else
InBlock.gif                    yy = y / Math.abs(y);
InBlock.gif
InBlock.gif                return (new HorsePosition(2 * x / Math.abs(x), yy));
InBlock.gif
ExpandedSubBlockEnd.gif            }
 else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                int xx;
InBlock.gif                if (x == 0)  xx = 1;
InBlock.gif                else
InBlock.gif                    xx = x / Math.abs(x);
InBlock.gif                return (new HorsePosition(xx, 2 * y / Math.abs(y)));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
 else
ExpandedSubBlockStart.gifContractedSubBlock.gif         if (z == 2)dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            if(x==0)dot.gif{
InBlock.gif                return    new HorsePosition(2,y/2 );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            if(y==0)dot.gif{
InBlock.gif                return    new HorsePosition(x/2,1 );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif          if(x*y!=0)dot.gif{
InBlock.gif             return    new HorsePosition(2*x,-y );
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif         }

ExpandedSubBlockStart.gifContractedSubBlock.gif         else if(z==1)dot.gif{ //说明z==1的情况了
ExpandedSubBlockStart.gifContractedSubBlock.gif             if(x==0)dot.gif{
InBlock.gif               return     new HorsePosition(1,2*y );
ExpandedSubBlockEnd.gif             }

InBlock.gif             else
InBlock.gif        return     new HorsePosition(2*x,1 );
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif      //以下说明完成了
InBlock.gif       return  new   HorsePosition(0,0 );
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/134308/viewspace-140581/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/134308/viewspace-140581/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值