zoukankan      html  css  js  c++  java
  • HDU 2243 Knight Moves

    题目:
    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
    Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

    Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

    Input

    The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

    Output

    For each test case, print one line saying "To get from xx to yy takes n knight moves.".

    Sample Input

    e2 e4
    a1 b2
    b2 c3
    a1 h8
    a1 h7
    h8 a1
    b1 c3
    f6 f6
    

    Sample Output

    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.


    题意描述:

    输入8*8棋盘样式的起点和终点坐标

    计算并输出棋子走“日”字形路线从起点到终点的最少步数

    解题思路:

    简单的搜索题,可以使用BFS对其进行搜索。

    代码实现:

     1 #include<stdio.h>
     2 #include<string.h>
     3 char map[10][10];
     4 int bfs(int sx,int sy,int ex,int ey);
     5 struct note
     6 {
     7     int x,y,s;
     8 };
     9 int main()
    10 {
    11     char sch,ech;
    12     int sx,sy,ex,ey;
    13     while(scanf("%c%d %c%d
    ",&sch,&sy,&ech,&ey) != EOF)
    14     {
    15         sx=sch-'a'+1;
    16         ex=ech-'a'+1;
    17         if(sx==ex && sy==ey)
    18         printf("To get from %c%d to %c%d takes 0 knight moves.
    ",sch,sy,ech,ey);
    19         else
    20         printf("To get from %c%d to %c%d takes %d knight moves.
    ",sch,sy,ech,ey,bfs(sx,sy,ex,ey));
    21     }
    22     return 0;
    23 }
    24 int bfs(int sx,int sy,int ex,int ey)
    25 {
    26     struct note que[101];
    27     int book[10][10];
    28     int next[8][2]={1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
    29     int head,tail,tx,ty,flag,i,j,k;
    30     head=1;
    31     tail=1;
    32     que[tail].x=sx;
    33     que[tail].y=sy;
    34     que[tail].s=0;
    35     tail++;
    36     memset(book,0,sizeof(book));
    37     book[sx][sy]=1;
    38     flag=0;
    39     while(head<tail)
    40     {
    41         for(k=0;k<=7;k++)//注意方向个数及数组初始化 
    42         {
    43             tx=que[head].x+next[k][0];
    44             ty=que[head].y+next[k][1];
    45             
    46             if(tx < 1||tx > 8||ty < 1||ty > 8)
    47                 continue;
    48             if(book[tx][ty]==0)
    49             {
    50                 book[tx][ty]=1;
    51                 
    52                 que[tail].x=tx;
    53                 que[tail].y=ty;
    54                 que[tail].s=que[head].s+1;
    55                 tail++;
    56             }
    57             if(tx==ex&&ty==ey)
    58                 return que[tail-1].s;
    59         }
    60         head++;
    61     }
    62 }

    易错分析:

    1、注意搜索方向的个数以及数组的初始化

  • 相关阅读:
    IIS负载均衡Application Request Route详解第二篇:创建与配置Server Farm
    负载均衡原理与实践详解 第一篇(重新整理)
    负载均衡原理剖析与实践第一篇介绍篇
    查询优化器内核剖析第八篇:执行引擎之数据访问操作Seek(上)
    提高ASP.NET性能与可伸缩性的几个个常用方法剖析
    IIS负载均衡Application Request Route详解第三篇:使用ARR进行Http请求的负载均衡(上)
    负载均衡第二篇负载均衡基础知识普及
    查询优化器内核剖析第七篇:执行引擎之数据访问操作Scan
    活在当下:人这一辈子,你总的坚持点什么
    LINQ 的演变及其对 C# 设计的影响
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7271413.html
Copyright © 2011-2022 走看看