2243 Knight Moves

作者: smatrcHendsa | 来源:发表于2019-03-21 17:02 被阅读0次

    双向BFS

    普通的BFS也行 写了玩一下
    脑子一抽忘了初始化数组 还有变量名写重复了 搞得调试了好久
    一开始写了 while(scanf("%s %s", c1, c2))导致了我的程序 Output Limit Exceeded
    换成 while(scanf("%s %s", c1, c2) != EOF)就好了
    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.

    #include <stdio.h>
    #include <iostream>
    #include <queue>
    #include <algorithm>
    int dx[8] = {-2, -2, 2, 2, 1, 1, -1, -1};
    int dy[8] = {1, -1, 1, -1, 2, -2, 2, -2};
    const int si = 9;
    int svis[si][si];
    int evis[si][si];
    int a, b, c, d;
    using namespace std;
    struct Node {
        int x, y, cnt;
        Node (int s, int t, int r) {
            x = s; y = t; cnt = r;
        }
    };
    
    int bfs() {
        queue<Node> sq, eq;
        sq.push(Node(a, b, 1));
        eq.push(Node(c, d, 1));
        while (1) {
            if (!sq.empty()) {
                Node n = sq.front(); sq.pop();
                int x = n.x, y = n.y, cnt = n.cnt;
                if (!svis[x][y]) {
                    svis[x][y] = cnt;
                    if (evis[x][y]) return svis[x][y] + evis[x][y] - 2;
                    for (int i = 0; i < 8; i++) {
                        int xx = x + dx[i];
                        int yy = y + dy[i];
                        if (xx < 0 || yy < 0 || xx >= 8 || yy >= 8) continue;
                        if (svis[xx][yy]) continue;
                        if (evis[xx][yy]) return evis[xx][yy] + cnt - 1;
                        sq.push(Node(xx, yy, cnt + 1));
                    }
                }
            }
            if (!eq.empty()) {
                Node n = eq.front(); eq.pop();
                int x = n.x, y = n.y, cnt = n.cnt;
                if (!evis[x][y]) {
                    evis[x][y] = cnt;
                    if (svis[x][y]) return svis[x][y] + evis[x][y] - 2;
                    for (int i = 0; i < 8; i++) {
                        int xx = x + dx[i];
                        int yy = y + dy[i];
                        if (xx < 0 || yy < 0 || xx >= 8 || yy >= 8) continue;
                        if (evis[xx][yy]) continue;
                        if (svis[xx][yy]) return svis[xx][yy] + cnt - 1;
                        eq.push(Node(xx, yy, cnt + 1));
                    }
                }
            }
        }
        return 0;
    }
    int main() {
        char c1[5], c2[5];
        while (scanf("%s %s", c1, c2) != EOF) {
            fill(svis[0], svis[0] + si * si, 0);
            fill(evis[0], evis[0] + si * si, 0);
            a = c1[0] - 'a';
            b = c1[1] - '1';
            c = c2[0] - 'a';
            d = c2[1] - '1';
            int ans = bfs();
            printf("To get from %s to %s takes %d knight moves.\n", c1, c2, ans);
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:2243 Knight Moves

        本文链接:https://www.haomeiwen.com/subject/ydecvqtx.html