馬的移動

來源:互聯網
上載者:User

標籤:移動   content   break   sample   har   list   next   問題   ++   

題目描述zzq很喜歡下國際象棋,一天,他拿著國際象棋中的“馬”時突然想到一個問題:
給定兩個棋盤上的方格a和b,馬從a跳到b最少需要多少步?
現請你編程解決這個問題。

提示:國際象棋棋盤為8格*8格,馬的走子規則為,每步棋先橫走或直走一格,然後再往外斜走一格。輸入輸入包含多組測試資料。每組輸入由兩個方格組成,每個方格包含一個小寫字母(a~h),表示棋盤的列號,和一個整數(1~8),表示棋盤的行號。輸出對於每組輸入,輸出一行“To get from xx to yy takes n knight moves.”。範例輸入
e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6
範例輸出
To get from e2 to e4 takes 2 knight moves.To get from a1 to b2 takes 4 knight moves.To get from b2 to c3 takes 2 knight moves.To get from a1 to h8 takes 6 knight moves.To get from a1 to h7 takes 5 knight moves.To get from h8 to a1 takes 6 knight moves.To get from b1 to c3 takes 1 knight moves.To get from f6 to f6 takes 0 knight moves.
題意描述:

輸入起點座標和終點座標

計算並輸出按照馬的特殊走法從起點走到終點的最少步數。

解題思路:

用廣度優先搜尋計算圖中兩點間的最短路徑。

代碼:

 1 #include<stdio.h> 2 #include<string.h> 3 struct note 4 { 5     int x,y,s; 6 }; 7 int main() 8 { 9     char list[9];10     int sx,sy,fx,fy,flag,k,tx,ty;11     struct note q[100];12     int head,tail,a[11][11];13     int next[8][2]={14         {1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};15     while(gets(list) != NULL)16     {17         sx=list[0]-‘a‘+1;18         sy=list[1]-‘0‘;19         fx=list[3]-‘a‘+1;20         fy=list[4]-‘0‘;21         if(sx==fx&&sy==fy)22         {23             printf("To get from %c%c to %c%c takes 0 knight moves.\n",list[0],list[1]24             ,list[3],list[4]);25             continue;26         }27         head=1;tail=1;28         q[tail].x=sx;29         q[tail].y=sy;30         q[tail].s=0;31         memset(a,0,sizeof(a));32         a[sx][sy]=1;33         tail++;34         35         flag=0;36         while(head<tail)37         {38             39             for(k=0;k<=7;k++)40             {41                 tx=q[head].x+next[k][0];42                 ty=q[head].y+next[k][1];43                 if(tx<1 || tx>8 || ty<1 || ty>8)44                     continue;45                 if(a[tx][ty]==0)46                 {47                     a[tx][ty]=1;48                     q[tail].x=tx;49                     q[tail].y=ty;50                     q[tail].s=q[head].s+1;51                     tail++;52                 }53                 if(tx==fx&&ty==fy)54                 {55                     flag=1;56                     break;57                 }58             }59             if(flag==1)60                 break;61             head++;62         }63         printf("To get from %c%c to %c%c takes %d knight moves.\n",list[0],list[1]64             ,list[3],list[4],q[tail-1].s);65     }66         return 0;67 }

易錯分析:

1、此題雖然題意未說不能重複走,但是還是不能重複走的,所以記得標記已經走過的路

2、初始化標標記數組

3、注意當起點座標和終點座標相等的時候輸出0

4、棋盤的邊界判斷,關鍵是8*8方格要理解清楚

範例測試:

h1 a4

f1 e6

a1 a2

範例輸出:

To get from h1 to a4 takes 4 knight moves.

To get from f1 to e6 takes 4 knight moves.

To get from a1 to a2 takes 3 knight moves.

馬的移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.