zoukankan      html  css  js  c++  java
  • Knight Moves

    Knight Moves

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 581 Accepted Submission(s): 421
     
    Problem 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 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.
     
     
    Source
    University of Ulm Local Contest 1996
     
    Recommend
    Eddy
    #include<bits/stdc++.h>
    #define N 10
    using namespace std;
    int dir[8][2]={
        {1,2},
        {2,1},
        {-1,2},
        {-2,1},
        {1,-2},
        {2,-1},
        {-1,-2},
        {-2,-1}
    };
    char beg[3],en[3];
    bool vis[N][N];
    int X1,X2,Y1,Y2;
    struct node
    {
        int x,y,step;
        node(){}
        node(int X,int Y,int S)
        {
            x=X;
            y=Y;
            step=S;
        }
    };
    bool ok(int x,int y)
    {
        if(x<1||x>8||y<1||y>8||vis[x][y])
            return false;
        return true;
    }
    int bfs(int x,int y)
    {
        queue<node>q;
        q.push(node(x,y,0));
        vis[x][y]=true;
        while(!q.empty())
        {
            node fr=q.front();
            q.pop();
            if(fr.x==X2&&fr.y==Y2)
                return fr.step;
            for(int i=0;i<8;i++)
            {
                int fx=fr.x+dir[i][0];
                int fy=fr.y+dir[i][1];
                if(ok(fx,fy))
                {
                    q.push(node(fx,fy,fr.step+1));
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        while(scanf("%s%s",&beg,&en)!=EOF)
        {
            memset(vis,false,sizeof vis);
            X1=(int)(beg[0]-'a'+1);
            Y1=(int)(beg[1]-'0');
            X2=(int)(en[0]-'a'+1);
            Y2=(int)(en[1]-'0');
            printf("To get from %s to %s takes %d knight moves.
    ",beg,en,bfs(X1,Y1));
        }
        return 0;
    }
  • 相关阅读:
    java 技术社区!!
    有关 gridview的 (做东西 容易忘记 记下!)
    【C#复习之虚方法vs抽象类】08.4.3 上午
    高分求解答!!!
    【学习笔记】asp.net MVC!
    asp.net ajax 尝试 之很好很强大!08.4.4
    Linux流量监控工具 – iftop (最全面的iftop教程)
    linux下查看某目录占用的空间大小
    基于CentOS的Linux基本网络配置,包括网卡eth0、DNS、Host等
    ubuntu安装ssh服务
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6038393.html
Copyright © 2011-2022 走看看