zoukankan      html  css  js  c++  java
  • 骑士的移动

    题目大意:

    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 Specification

    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 Specification

    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广度优先搜索法,来寻找最短的路程。用队列储存找到的结点,先把骑士移动的八个方向表示出来,整个棋盘都初始化0,然后每个方向都搜索一遍,走过的地方设置为1,像撒网一样,把找到的结点放进队列中,再以这个结点为中心,八个方向继续撒网,能走通的地方就继续走,不能走就换方向。
     
    源代码:
     
     
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 #include<string>
     5 using namespace std;
     6 struct tnode{
     7     int x;
     8     int y;
     9     int step;
    10 
    11 }p,new_dot,t;
    12 int chess[10][10];
    13 string a1, a2;
    14 int dest_x,dest_y;
    15 int to[8][2] = { -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2, -2, -1 };  //骑士初始的八个方向
    16 int judge(int a, int b)                                                    //判断骑士是否还在棋盘内
    17 {
    18     if (a >=8 ||a<0||b >=8 ||b<0||chess[a][b])                        
    19         return 1;
    20     return 0;
    21 }
    22 int bfs()
    23 {
    24     
    25     queue<tnode>T;       
    26     memset(chess, 0, sizeof(chess));
    27     p.x = a1[0] - 'a';                                                   //  骑士开始的位置
    28     p.y = a1[1] - '1';                                                  
    29     p.step = 0;
    30     chess[p.x][p.y] = 1;                                                 //把开始的位置设为1
    31     dest_x = a2[0] - 'a';                                                //骑士要去的位置
    32     dest_y = a2[1] - '1';
    33     T.push(p);                                                           //把结点放到队列中
    34     while (!T.empty())
    35     {                                                                           
    36         t = T.front();
    37         T.pop();
    38         if (t.x == dest_x&&t.y == dest_y)                                  //到达目标位置
    39             return t.step;
    40             
    41         for (int i = 0; i < 8; i++)
    42         {
    43             new_dot.x = t.x + to[i][0];                                       //8个方向,每个方向都遍历一遍
    44             new_dot.y = t.y + to[i][1];          
    45             if (new_dot.x == dest_x&&new_dot.y == dest_y)
    46                 return t.step + 1;
    47             if (judge(new_dot.x, new_dot.y))                                 //不是目标点,但是还在棋盘内,继续走
    48                 continue;
    49             chess[new_dot.x][new_dot.y] = 1;
    50             new_dot.step = t.step + 1;
    51             T.push(new_dot);                                                 //把新结点放进队列中
    52             
    53         }
    54     }
    55     
    56     return 0;
    57 }
    58 int main()
    59 {
    60     while (cin >> a1 >> a2)
    61     {
    62         cout << "To get from " << a1 << " to " << a2 << " takes " << bfs() << " knight moves." << endl;
    63     }
    64     system("pause");
    65     return 0;
    66 
    67 }

    心得:

        以前不了解BFS,一种用来找最短的距离,或者最少次数的算法,就像撒网一样,每次新学一点就会觉得很满足,暑假培训两天了,第一次体验这种生活,虽然辛苦,但是学到东西就觉得很值,就像屠老师说的,好好刷题,开始可以跟着别人的代码写,每个人都有一个过程,坚持总比不坚持收获得多!

     
    ------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
  • 相关阅读:
    团队作业8----第二次项目冲刺(beta阶段)5.20
    团队作业8——第二次项目冲刺(Beta阶段) 5.19
    团队作业8——Beta项目(冲刺计划)
    团队作业——Alpha冲刺之事后诸葛亮
    团队作业5——测试与发布(Alpha版本)
    团队作业6——展示博客(Alpha版本)
    团队作业4——第一次项目冲刺(Alpha版本)2017.4.28
    团队作业4——第一次项目冲刺(Alpha版本)2017.4.27
    团队作业4——第一次项目冲刺(Alpha版本)2017.4.26
    团队作业4——第一次项目冲刺(Alpha版本)2017.4.25
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4675237.html
Copyright © 2011-2022 走看看