<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    piliskys

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      25 隨筆 :: 0 文章 :: 40 評論 :: 0 Trackbacks

    今天在網(wǎng)上看到一個{找跳馬最短路徑的算法} 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寫了一下,沒有考慮邊界,以下程序只在無限二維中成立
    代碼如下

    /**
    ?*?
    @author ?:?<a?href="piliskys@163.com">piliskys</a>
    ?*?Date:?2006-2-22
    ?*?Time:?13:50:56
    ?*?找跳馬最短路徑的算法
    ?*?
    ?
    */

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

    ????}

    ??????
    /** ? */ /**
    ???????*?以下為構(gòu)造一個位置類
    ???????
    */

    ????
    static ? class ?HorsePosition? {
    ????????HorsePosition(
    int ?a,? int ?b)? {
    ????????????
    this .positionX? = ?a;
    ????????????
    this .positionY? = ?b;
    ????????}

    ????????
    int ?positionX;
    ????????
    int ?positionY;
    ????????
    public ?String?toString()? {
    ????????????
    return ? " [ " ? + ? this .positionX? + ? " , " ? + ? this .positionY? + ? " ] " ;
    ????????}

    ????}


    ????
    public ? static ?HorsePosition?getNext(HorsePosition?a,?HorsePosition?b)? {
    ????????
    int ?x,?y,?z;
    ????????x?
    = ?b.positionX? - ?a.positionX;
    ????????y?
    = ?b.positionY? - ?a.positionY;
    ????????z?
    = ?Math.abs(x)? + ?Math.abs(y);
    ????????
    if ?(z? >= ? 3 )? {
    ????????????
    if ?(Math.abs(x)? > ?Math.abs(y))? {
    ????????????????
    int ?yy;
    ????????????????
    if ?(y? == ? 0 )??yy? = ? 1 ;
    ????????????????
    else
    ????????????????????yy?
    = ?y? / ?Math.abs(y);

    ????????????????
    return ?( new ?HorsePosition( 2 ? * ?x? / ?Math.abs(x),?yy));

    ????????????}
    ? else
    ????????????
    {
    ????????????????
    int ?xx;
    ????????????????
    if ?(x? == ? 0 )??xx? = ? 1 ;
    ????????????????
    else
    ????????????????????xx?
    = ?x? / ?Math.abs(x);
    ????????????????
    return ?( new ?HorsePosition(xx,? 2 ? * ?y? / ?Math.abs(y)));
    ????????????}

    ????????}
    ? else
    ?????????
    if ?(z? == ? 2 ) {

    ????????????
    if (x == 0 ) {
    ????????????????
    return ???? new ?HorsePosition( 2 ,y / 2 ?);
    ????????????}

    ????????????
    if (y == 0 ) {
    ????????????????
    return ???? new ?HorsePosition(x / 2 , 1 ?);
    ????????????}

    ??????????
    if (x * y != 0 ) {
    ?????????????
    return ???? new ?HorsePosition( 2 * x, - y?);
    ??????????}

    ?????????}

    ?????????
    else ? if (z == 1 ) {? // 說明z==1的情況了
    ????????????? if (x == 0 ) {
    ???????????????
    return ????? new ?HorsePosition( 1 , 2 * y?);
    ?????????????}

    ?????????????
    else
    ????????
    return ????? new ?HorsePosition( 2 * x, 1 ?);
    ?????????}


    ??????
    // 以下說明完成了
    ??????? return ?? new ???HorsePosition( 0 , 0 ?);

    ????}



    }

    posted on 2006-03-20 14:36 霹靂火 閱讀(1241) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 最近中文字幕2019高清免费 | 我要看免费的毛片| 在线免费不卡视频| 亚洲精品乱码久久久久久蜜桃| 国产又大又长又粗又硬的免费视频| 国产又大又粗又硬又长免费 | 亚洲人色婷婷成人网站在线观看| 精品无码无人网站免费视频| 黄页免费在线观看 | 久久精品无码精品免费专区| 中国xxxxx高清免费看视频| 性盈盈影院免费视频观看在线一区| 免费国产综合视频在线看| 亚洲欧洲国产精品香蕉网| 亚洲国产精品成人午夜在线观看| 亚洲午夜久久久久妓女影院| 亚洲国产精品免费在线观看| 九九久久国产精品免费热6| 久久国产乱子伦精品免费不卡| 国产公开免费人成视频| 亚洲综合一区二区精品导航 | 久久国产亚洲精品麻豆| 亚洲丰满熟女一区二区哦| 久久精品国产免费一区| 免费一级毛片在播放视频| 亚洲毛片基地4455ww| 色欲A∨无码蜜臀AV免费播| 亚洲日韩在线中文字幕第一页| 国产精品亚洲精品青青青| 蜜桃视频在线观看免费视频网站WWW | 亚洲综合色丁香麻豆| 最近的中文字幕大全免费8| 国产成人精品久久亚洲高清不卡 | 亚洲成A人片在线播放器| 无码国产精品一区二区免费式影视 | 亚洲Av无码乱码在线观看性色| 亚洲熟妇无码av另类vr影视| 91香蕉国产线在线观看免费| 国产av无码专区亚洲av桃花庵| 黄色免费网站在线看| 亚洲人成无码久久电影网站|