码迷,mamicode.com
首页 > 移动开发 > 详细

马的移动

时间:2017-07-22 15:43:35      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:bfs   位置   set   null   class   国际象棋   memset   time   整数   

问题 F: 马的移动

时间限制: 1 Sec  内存限制: 32 MB

题目描述

zzq很喜欢下国际象棋,一天,他拿着国际象棋中的时突然想到一个问题:
给定两个棋盘上的方格ab,马从a跳到b最少需要多少步?
现请你编程解决这个问题。

提示:国际象棋棋盘为8*8格,马的走子规则为,每步棋先横走或直走一格,然后再往外斜走一格。

输入

输入包含多组测试数据。每组输入由两个方格组成,每个方格包含一个小写字母(a~h),表示棋盘的列号,和一个整数(1~8),表示棋盘的行号。

输出

对于每组输入,输出一行“To get from xx to yy takes n knight moves.”

样例输入

e2 e4

a1 b2

b2 c3

a1 h8

a1 h7

h8 a1

b1 c3

f6 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.

 

题意概括:

每步棋先横走或直走一格,然后再往外斜走一格,算出从一个位置到另一个位置最少需走多少步。

解题分析:

每一个位置可以有八个位置可以走,用广搜遍历每一个方向,找到结束位置即结束搜索并输出最小步数。

 

测试样例:

a1 h8

d5 h2

b7 f7

b7 b7

f3 g4

测试样例输出:

To get from a1 to h8 takes 6 knight moves.

To get from d5 to h2 takes 3 knight moves.

To get from b7 to f7 takes 2 knight moves.

To get from b7 to b7 takes 0 knight moves.

To get from f3 to g4 takes 2 knight moves.

 

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 struct Note{
 5     int x, y;
 6 };
 7 
 8 int book[10][10];
 9 
10 void bfs(Note head, Note tail);
11 
12 int main()
13 {
14     int x, y, tx, ty;
15     char str[9];
16     Note head, tail;
17     while(gets(str) != NULL){
18         memset(book, 0, sizeof(book));
19         head.x = str[0] - a + 1;
20         head.y = str[1] - 0;
21         tail.x = str[3] - a + 1;
22         tail.y = str[4] - 0;
23         bfs(head, tail);
24         printf("To get from %c%c to %c%c takes %d knight moves.\n", str[0],str[1],str[3],
25             str[4],book[tail.x][tail.y]-1);
26     } 
27     return 0;    
28 } 
29 
30 void bfs(Note head, Note tail)
31 {
32     int Next[8][2]  = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
33     int i, j, h,t, tx, ty;
34     Note que[85];
35     h = t = 0;
36     que[t].x = head.x;
37     que[t].y = head.y;
38     t++;
39     book[head.x][head.y] = 1;
40     while(h < t){
41         for(i = 0; i < 8; i++){
42             tx = que[h].x + Next[i][0];
43             ty = que[h].y + Next[i][1];
44             if(tx < 1 || ty < 1 || tx > 8 || ty > 8)
45                 continue;
46             if(!book[tx][ty]){
47                 book[tx][ty] = book[que[h].x][que[h].y] + 1;
48                 
49                 que[t].x = tx;
50                 que[t].y = ty;
51                 t++; 
52             }
53             if(tx == tail.x && ty == tail.y){
54                 return ;
55             }
56                 
57         }
58         h++;
59     }
60 }

 

马的移动

标签:bfs   位置   set   null   class   国际象棋   memset   time   整数   

原文地址:http://www.cnblogs.com/didideblog/p/7221355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!