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

    POJ 2243 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. 
    InputThe 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. 
    OutputFor 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求最短路,需要注意的是国际象棋中骑士和中国象棋中的马一样走日字

    代码:

    #include<iostream>
    #include<queue>
    #include<cstdio>
    using namespace std;
    const int N = 10;
    typedef struct {
        int x;
        int y;
    } P;
    int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};
    int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
    int vis[N][N];
    int d[N][N];
    char s[5];
    char e[5];
    int sx, sy, ex, ey;
    int check(int x, int y) {
        return x >= 0 && x < 8 && y >= 0 && y < 8; 
    }
    void bfs() {
        queue<P> que;
        P p;
        p.x = sx;
        p.y = sy;
        que.push(p);
        vis[sx][sy] = 1;
        while(que.size()) {
            P p = que.front();
            que.pop();
            int x = p.x;
            int y = p.y;
            if(x == ex && y == ey) {
                printf("To get from %s to %s takes %d knight moves.
    ", s, e, d[x][y]);
                break;
            }
            for(int i = 0; i < 8; i++) {
                int nx = x + dx[i], ny = y + dy[i];
                if(check(nx, ny) && !vis[nx][ny]) {
                    vis[nx][ny] = 1;
                    d[nx][ny] = d[x][y] + 1;
                    P p;
                    p.x = nx;
                    p.y = ny;
                    que.push(p);
                }
            }
        }
    }
    int main() {
        while(cin >> s >> e) {
            for(int i = 0; i < N; i++) {
                for(int j = 0; j < N; j++) d[i][j] = vis[i][j] = 0;
            }
            sx = s[1] - '0' - 1;
            sy = s[0] - 'a';
            ex = e[1] - '0' - 1;
            ey = e[0] - 'a';
            bfs();
        }
        return 0;
    }
    作者:kindleheart
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Enterprise Library 2.0 Hands On Lab 翻译(9):缓存应用程序块(一)
    Enterprise Library 2.0 Hands On Lab 翻译(5):日志应用程序块(二)
    提供多单词建议的自定义AutoCompleteExtender
    什么是 axios
    Consul是一个分布式高可用的系统
    全栈开发工程师
    XPath路径表达式
    9 个带你阅读源码的开源项目
    前端跨域解决方案
    [WCFDiscovery]让服务自动发送上/下线通知[原理篇]
  • 原文地址:https://www.cnblogs.com/kindleheart/p/9297373.html
Copyright © 2011-2022 走看看