• HDUOJ1372Knight Moves


    Knight Moves

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3615    Accepted Submission(s): 2248


    Problem Description
    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 file 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.
     1 #include<iostream>
     2 using namespace std;
     3 #include<stdio.h>
     4 #include<queue>
     5 typedef struct
     6 {
     7        int x;
     8        int y;
     9        int bu;
    10 }zuobiao;
    11 zuobiao t; 
    12 int in(int x,int y)
    13 {
    14     if(x<=8&&x>=1&&y<=8&&y>=1)return 1;
    15     return 0;
    16 }
    17 int main()
    18 {
    19     char ch1,ch2;
    20     int y1,y2,x1,x2,i;
    21     int a[8]={-1,1,-2,-2,-1,1,2,2};
    22     int    b[8]={2,2,1,-1,-2,-2,1,-1};
    23     while(scanf("%c%d %c%d",&ch1,&y1,&ch2,&y2)!=EOF)
    24     {
    25         getchar();
    26         queue<zuobiao> q;
    27         x1=ch1-'a'+1;
    28         x2=ch2-'a'+1;
    29         if(x1==x2&&y1==y2)
    30         {
    31             printf("To get from %c%d to %c%d takes %d knight moves.\n",ch1,y1,ch2,y2,0);
    32             continue;
    33         }
    34         t.x=x1,t.y=y1;t.bu=1;
    35         q.push(t);
    36         while(1)
    37         {
    38               for(i=0;i<8;i++)
    39               {
    40                   if(in(q.front().x+a[i],q.front().y+b[i]))
    41                   {
    42                         if(q.front().x+a[i]==x2&&q.front().y+b[i]==y2)
    43                             break;
    44                         t.x=q.front().x+a[i];
    45                         t.y=q.front().y+b[i];
    46                         t.bu=q.front().bu+1;
    47                         q.push(t);
    48                     }
    49               }
    50               if(q.front().x+a[i]==x2&&q.front().y+b[i]==y2)
    51                             break;
    52               q.pop();
    53         }
    54         printf("To get from %c%d to %c%d takes %d knight moves.\n",ch1,y1,ch2,y2,q.front().bu);
    55     }
    56     return 0;
    57     
    58 }
    59               
    60               
    61               
    62        
    63        
    64        
  • 相关阅读:
    RESTful API规范
    SOAP和RESTful 框架的 简介、对比和区别
    @requestBody 与@requestparam详解
    关于“Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.”
    post和get、PostMapping、GetMapping和RequestMapping
    强引用、软引用、弱引用、虚引用及方法区的垃圾回收策略
    jvm运行时数据区域
    HBase入门
    各种http报错的报错的状态码的分析
    maven配置多个镜像
  • 原文地址:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_2012_08_2000.html
Copyright © 2020-2023  润新知