zoukankan      html  css  js  c++  java
  • HDOJ_ACM_Knight Moves

    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.
     

     

    Idea

    Well, I really have no word for this question. I use the common BFS, then it accepted.

    I think I can improve the algorithm by trimming, at least it have no need to search back.

    Code

    View Code
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <queue>
     4 using namespace std;
     5 struct Node
     6 {
     7     int x;
     8     int y;
     9     int step;
    10 };
    11 int main()
    12 {
    13     int dx[10] = {2, 2, -2, -2, 1, 1, -1, -1};
    14     int dy[10] = {1, -1, 1, -1, 2, -2, 2, -2};
    15     char begin[5], end[5];
    16     int i, result;
    17     Node beginN, endN, pre, cur;
    18     while (scanf("%s %s", begin, end) != EOF)
    19     {
    20         queue<Node> q;
    21         //translate the input
    22         beginN.x = begin[0] - 'a';
    23         beginN.y = begin[1] - '1';
    24         beginN.step = 0;
    25         endN.x = end[0] - 'a';
    26         endN.y = end[1] - '1';
    27         //push begin Node into the queue
    28         q.push(beginN);
    29         result = 0;
    30         int flag = 0;
    31         if (beginN.x == endN.x && beginN.y == endN.y)
    32             flag = 1;
    33         while (!q.empty() && flag == 0)
    34         {
    35             pre = q.front();
    36             /*
    37             if (abs(endN.x - pre.x) + abs(endN.y - pre.y) == 1)
    38             {
    39                 result = pre.step + 3;
    40                 break;
    41             }
    42             else if (abs(endN.x - pre.x) == 2 || abs(endN.y - pre.y) == 2)
    43             {
    44                 result = pre.step + 2;
    45                 break;
    46             }
    47             */
    48             q.pop();
    49             //find the eight dirctory
    50             for (i = 0; i < 8; i++)
    51             {
    52                 cur.x = pre.x + dx[i];
    53                 cur.y = pre.y + dy[i];
    54                 cur.step = pre.step + 1;
    55                 /*
    56                 if (cur.x < 0 || cur.x >= 8 || cur.y < 0 || cur.y >= 8
    57                     || (abs(endN.x - pre.x) + abs(endN.y - pre.y) < abs(endN.x - cur.x) + abs(endN.y - cur.y)
    58                     && !(abs(endN.x - pre.x) == 1 && abs(endN.y - pre.y) == 1)))
    59                     break;
    60                     */
    61                 //if the point is out of index, then u can ignore
    62                 if (cur.x < 0 || cur.x >= 8 || cur.y < 0 || cur.y >= 8)
    63                     continue;
    64                 q.push(cur);
    65                 //if u can find the it, then mark the flag
    66                 if (cur.x == endN.x && cur.y == endN.y)
    67                 {
    68                     flag = 1;
    69                     result = cur.step;
    70                     break;
    71                 }
    72             }
    73         }
    74         printf("To get from %s to %s takes %d knight moves.\n", begin, end, result);
    75     }
    76     return 0;
    77 }
     
  • 相关阅读:
    面向对象七大基本原则
    JS面向对象的七大基本原则(里氏替换/依赖倒置)
    JS面向对象的七大基本原则(单一/开闭)
    flex均匀布局
    Vue在IDEA的简约安装
    事务及数据表设计
    关于序列化与反序列化
    SQL查询关键字用法
    IO流
    cookie与session
  • 原文地址:https://www.cnblogs.com/chuanlong/p/3012923.html
Copyright © 2011-2022 走看看