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

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

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

    piliskys

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

    今天在網上看到一個{找跳馬最短路徑的算法} 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);
    ????????}

    ????}

    ??????
    /** ? */ /**
    ???????*?以下為構造一個位置類
    ???????
    */

    ????
    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 霹靂火 閱讀(1240) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲综合国产精品| 亚洲VA中文字幕无码一二三区| 亚洲国产日产无码精品| 男的把j放进女人下面视频免费| 亚洲愉拍99热成人精品热久久| 特级毛片免费播放| 久久乐国产精品亚洲综合| 日韩在线观看免费| a在线免费观看视频| 亚洲综合熟女久久久30p| 亚洲区视频在线观看| 午夜宅男在线永久免费观看网| 久久精品国产亚洲AV大全| 国拍在线精品视频免费观看| 亚洲日韩AV一区二区三区中文| 免费无码又爽又刺激高潮的视频| 在线亚洲v日韩v| 亚洲日本一区二区三区在线不卡| 拍拍拍无挡免费视频网站| 亚洲伊人tv综合网色| 和日本免费不卡在线v| 国产亚洲福利一区二区免费看| 亚洲国产精品无码久久久久久曰| 免费无码又爽又刺激网站| 久久久久亚洲Av无码专| 成人au免费视频影院| 亚洲成AV人片一区二区密柚| 麻花传媒剧在线mv免费观看 | 久久91亚洲人成电影网站| 日韩免费的视频在线观看香蕉| 亚洲成人高清在线观看| 国产成人高清精品免费软件| 免费在线观影网站| 亚洲色欲色欲www| 国产亚洲精品无码拍拍拍色欲| 99免费观看视频| 香蕉视频免费在线| 亚洲黄色片免费看| 四虎永久成人免费影院域名| 嫩草成人永久免费观看| 亚洲AV无码资源在线观看|