zoukankan      html  css  js  c++  java
  • POJ 2243 Knight Moves

    Knight Moves
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13222   Accepted: 7418

    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 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.
    

    Source

     
     
     
    解析:题意为求从棋盘a处到b处需要跳动的最少次数。广搜一遍即可,要注意骑士的行走方式。
     
     
     
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 using namespace std;
     5 
     6 struct Point{
     7     int x, y;
     8 }s, t;
     9 //骑士行走的方向
    10 int dir[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1}};
    11 
    12 bool inChess(Point a)
    13 {
    14     return a.x >= 0 && a.y >= 0 && a.x < 8 && a.y < 8;
    15 }
    16 
    17 int bfs()
    18 {
    19     if(s.x == t.x && s.y == t.y)
    20         return 0;
    21     queue <Point> q;
    22     bool visit[10][10];
    23     int dis[10][10];
    24     memset(visit, 0, sizeof(visit));
    25     q.push(s);
    26     visit[s.x][s.y] = true;
    27     dis[s.x][s.y] = 0;
    28     while(!q.empty()){
    29         Point a = q.front();
    30         q.pop();
    31         for(int i = 0; i < 8; ++i){
    32             Point b;
    33             b.x = a.x + dir[i][0];
    34             b.y = a.y + dir[i][1];
    35             if(inChess(b) && !visit[b.x][b.y]){
    36                 visit[b.x][b.y] = true;
    37                 dis[b.x][b.y] = dis[a.x][a.y] + 1;
    38                 q.push(b);
    39                 if(b.x == t.x && b.y == t.y)
    40                     return dis[b.x][b.y];
    41             }
    42         }
    43     }
    44     return -1;
    45 }
    46 
    47 int main()
    48 {
    49     char s1[10], s2[10];
    50     while(~scanf("%s%s", s1, s2)){
    51         s.x = s1[0] - 'a';
    52         s.y = s1[1] - '1';
    53         t.x = s2[0] - 'a';
    54         t.y = s2[1] - '1';
    55         printf("To get from %s to %s takes %d knight moves.
    ", s1, s2, bfs());
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    appcan双击返回退出系统
    实现两个矩阵相乘的C语言程序
    编写简单的windows桌面计算器程序
    使用C++实现图形的旋转、缩放、平移
    使用MFC创建C++程序
    SQL server2017的操作(练习题)
    使用SQL语句在SQL server2017上创建数据库
    使用交互式方式在SQL server2017上创建数据库
    解决VS2017编译后的EXE文件不能在其他电脑上运行的问题
    傅里叶分析之掐死教程(完整版)更新于2014.06.06----转载
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5502863.html
Copyright © 2011-2022 走看看