zoukankan      html  css  js  c++  java
  • 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 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 #include <queue>
     3 #include <cstring>
     4 using namespace std;
     5 string be, en;
     6 
     7 int dir[8][2] = {
     8     {2,1},
     9     {1,2},
    10     {2,-1},
    11     {1,-2},
    12     {-1,-2},
    13     {-2,-1},
    14     {-2,1},
    15     {-1,2},
    16 };
    17 
    18 struct node{
    19     int x, y;
    20     int step;
    21 }pos,q;
    22 int vis[15][15];
    23 
    24 void bfs(int x0, int y0, int x, int y){
    25     memset(vis,0,sizeof(vis));
    26     queue<node> que;
    27     pos.x = x0;
    28     pos.y = y0;
    29     pos.step = 0;
    30     //vis[x0][y0] = 1;
    31     que.push(pos);
    32     while(!que.empty()){
    33         pos = que.front();
    34         que.pop();
    35         vis[pos.x][pos.y] = 1;
    36         if(pos.x == x && pos.y == y){
    37             cout << "To get from " << be << " to " << en << " takes " << pos.step << " knight moves." << endl;
    38             return ;
    39         }
    40         for(int i = 0; i < 8; i++){
    41             int next_x = pos.x + dir[i][0];
    42             int next_y = pos.y + dir[i][1];
    43             if(next_x >= 1 && next_x <= 8 && next_y >= 1 && next_y <= 8 && vis[next_x][next_y] == 0){
    44                 q.x = next_x;
    45                 q.y = next_y;
    46                 q.step = pos.step + 1;
    47                 que.push(q);
    48                 vis[next_x][next_y] = 1;
    49             }
    50         }
    51     }
    52 }
    53 
    54 int main(){
    55 
    56     while(cin >> be >> en){
    57         int x0 = (int)be[0] - 96;
    58         int y0 = be[1] - '0';
    59         int x = (int)en[0] - 96;
    60         int y = en[1] - '0';
    61         //cout << x0 << " " << y0 << "," << x << " " << y << endl;
    62         bfs(x0,y0,x,y);
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    ZKW费用流修正
    BZOJ 1060 [ZJOI2007]时态同步
    BZOJ 1059 [ZJOI2007]矩阵游戏
    腾讯WEB前端开发面试经历,一面二面HR面,面面不到!
    亲历腾讯WEB前端开发三轮面试经历及面试题
    2015大型互联网公司校招都开始了,薪资你准备好了嘛?
    10款最好的 Bootstrap 3.0 免费主题和模板
    python3之urllib基础
    python3下应用requests
    python心得二(编码问题)
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6710873.html
Copyright © 2011-2022 走看看