zoukankan      html  css  js  c++  java
  • 马的移动

    问题 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.
    ", 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 }
  • 相关阅读:
    js 格式为2018-08-25 11:46:29 的日期比较方法
    ssh linux免密登录。。。。生产共钥到另一台主机
    linux shell脚本调用java main方法 代码传值
    linux shell脚本调用java main方法 代码
    java FTP各种操作
    java通过http服务执行shell命令
    linux ssh 应用
    java通过反射调用有参数的方法
    TranslucentTB汉化版安装教程及资源
    几行Python代码生成动态二维码 myqr的使用教程
  • 原文地址:https://www.cnblogs.com/didideblog/p/7221355.html
Copyright © 2011-2022 走看看