zoukankan      html  css  js  c++  java
  • HDU_1372——骑士移动,BFS非模版

    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 <stdio.h>
     2 #include <string.h>
     3 int a[100][2],d[8][8];
     4 char p[5],q[5];
     5 void bfs()
     6 {
     7     int front = 0,rear = 1,m,n;
     8     d[p[0]-'a'][p[1]-'0'-1] = 0;
     9     a[0][0] = p[0]-'a';
    10     a[0][1] = p[1]-'0'-1;
    11     while(front != rear)
    12     {
    13         m = a[front][0];
    14         n = a[front++][1];
    15         if(m == q[0]-'a' && n == q[1]-'0'-1) return ;
    16         if(m > 1 && n > 0 && !d[m-2][n-1]) a[rear][0] = m-2, a[rear++][1] = n-1, d[m-2][n-1] = d[m][n]+1;
    17         if(m > 0 && n > 1 && !d[m-1][n-2]) a[rear][0] = m-1, a[rear++][1] = n-2, d[m-1][n-2] = d[m][n]+1;
    18         if(m > 1 && n < 7 && !d[m-2][n+1]) a[rear][0] = m-2, a[rear++][1] = n+1, d[m-2][n+1] = d[m][n]+1;
    19         if(m > 0 && n < 6 && !d[m-1][n+2]) a[rear][0] = m-1, a[rear++][1] = n+2, d[m-1][n+2] = d[m][n]+1;
    20         if(m < 7 && n > 1 && !d[m+1][n-2]) a[rear][0] = m+1, a[rear++][1] = n-2, d[m+1][n-2] = d[m][n]+1;
    21         if(m < 6 && n > 0 && !d[m+2][n-1]) a[rear][0] = m+2, a[rear++][1] = n-1, d[m+2][n-1] = d[m][n]+1;
    22         if(m < 7 && n < 6 && !d[m+1][n+2]) a[rear][0] = m+1, a[rear++][1] = n+2, d[m+1][n+2] = d[m][n]+1;
    23         if(m < 6 && n < 7 && !d[m+2][n+1]) a[rear][0] = m+2, a[rear++][1] = n+1, d[m+2][n+1] = d[m][n]+1;
    24     }
    25 }
    26 
    27 int main()
    28 {
    29     while(~scanf("%s %s", p, q))
    30     {
    31         memset(d, 0, sizeof(d));
    32         bfs();
    33         printf("To get from %s to %s takes %d knight moves.\n", p, q, d[q[0]-'a'][q[1]-'0'-1]);
    34     }
    35     return 0;
    36 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    JSONObject,JSONArray,Map,String之间转换
    linux下,一个运行中的程序,究竟占用了多少内存
    利用VMware在虚拟机上安装Zookeeper集群
    30岁后职场改变
    Zookeeper客户端 CuratorFramework使用
    oracle 用户与表空间关系
    Docker Rest API使用入门
    docker 远程rest api 访问配置
    Oracle 用户、角色管理简介
    Oracle 参数文件及相关操作介绍
  • 原文地址:https://www.cnblogs.com/pingge/p/3132725.html
Copyright © 2011-2022 走看看