• hdu 1372Knight Moves


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

    Knight Moves

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


    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.
     
    简单bfs,弱爆了,照着人家代码写的,放在这,方便温习。
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3     char schx,echx;
     4     int sy,ey;
     5     int sx,ex;
     6     int map[8][8];
     7     int queue[70];
     8     int vis[8][8];
     9     int count[8][8];//记录步数 
    10     int dir[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2}; 
    11 void bfs(int x,int y)
    12 {
    13     
    14     int front=0,rear=0,top;
    15     int i,xx,yy;
    16     vis[x][y]=1;
    17     queue[rear]=9*x+y;
    18     rear=(rear+1)%70;
    19     while(front<rear)
    20     {
    21         
    22         top=queue[front];
    23         front=(front+1)%70;
    24         x=top/9;
    25         y=top%9;
    26         
    27         if(x==ex&&y==ey) return;
    28         for(i=0;i<8;i++)
    29         {
    30             xx=x+dir[i][0];
    31             yy=y+dir[i][1];
    32             if(xx>=0&&xx<=7&&yy>=0&&yy<=7&&!vis[xx][yy])
    33             {
    34                 vis[xx][yy]=1;
    35                 queue[rear]=9*xx+yy;
    36                 rear=(rear+1)%70;
    37                 count[xx][yy]=count[x][y]+1;
    38                 //printf("%d\n",count[xx][yy]);
    39             }
    40         }
    41         
    42     }    
    43 }
    44 int main()
    45 {
    46     char str1[3],str2[3];
    47     while(~scanf("%s %s",str1,str2))
    48     {
    49         sx=str1[0]-'a';
    50         sy=str1[1]-'1';
    51         ex=str2[0]-'a';
    52         ey=str2[1]-'1';
    53         memset(vis,0,sizeof(vis));
    54         memset(count,0,sizeof(count));
    55         bfs(sx,sy);
    56         printf("To get from %s to %s takes %d knight moves.\n",str1,str2,count[ex][ey]);
    57     }
    58 }
  • 相关阅读:
    从服务器上下载下来的代码,部署到本地时,Url自动带www前缀
    个人说明
    名词解释
    Bandizip-解压缩软件
    uTools-工具插件集
    Geek-软件卸载工具
    Microsoft商店软件推荐
    Docker入门第九章
    Docker入门第八章
    IDM-下载工具
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2622268.html
Copyright © 2020-2023  润新知